forked from pyconjp/pyconjp2025-camp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
116 lines (85 loc) · 3 KB
/
models.py
File metadata and controls
116 lines (85 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import enum
from typing import Any, TypedDict
from pydantic import BaseModel, Field
class AVAILABLE_MODELS(str, enum.Enum):
"""利用可能なモデルの列挙型"""
GEMINI_2_0_FLASH = "gemini-2.0-flash"
GEMINI_1_5_FLASH = "gemini-1.5-flash"
GEMINI_2_5_FLASH = "gemini-2.5-flash-preview-05-20"
class QueryArgs(TypedDict):
"""DeepSeek APIの戻り値を表す型"""
query: str
role: str
model_name: AVAILABLE_MODELS
temperature: float
max_tokens: int | None
class Options(BaseModel):
"""LLMへのオプション設定"""
model: AVAILABLE_MODELS = Field(
AVAILABLE_MODELS.GEMINI_2_0_FLASH,
title="モデル名",
description="使用するモデルの名前",
)
max_tokens: int = Field(
1024,
title="最大トークン数",
description="生成する回答の最大トークン数",
ge=128,
le=4096,
)
class SingleRequest(BaseModel):
"""単一の問い合わせリクエスト"""
key: str = Field(..., description="認証キー")
q: str = Field(..., description="質問文字列")
options: Options | None = Field(
None, title="オプション設定", description="モデルやトークン数の設定"
)
class MultiOptions(BaseModel):
"""複数問い合わせのオプション設定"""
models: tuple[AVAILABLE_MODELS, ...] = Field(
(AVAILABLE_MODELS.GEMINI_2_0_FLASH,),
title="モデル名リスト",
description="使用するモデルの名前のリスト",
)
roles: tuple[str, ...] = Field(
("あなたは親切なアシスタントです。",),
title="役割リスト",
description="AIの役割を指定する文字列のリスト(例:初心者向け、弁護士風など)",
)
max_tokens: int = Field(
1024,
title="最大トークン数",
description="生成する回答の最大トークン数",
ge=128,
le=4096,
)
class MultiRequest(BaseModel):
"""複数の問い合わせリクエスト"""
key: str = Field(..., description="認証キー")
q: str = Field(..., description="質問文字列")
options: MultiOptions = Field(
default_factory=lambda: MultiOptions(
models=(AVAILABLE_MODELS.GEMINI_2_0_FLASH,),
roles=("あなたは親切なアシスタントです。",),
max_tokens=1024,
),
title="オプション設定",
description="モデル、役割、トークン数の設定",
)
class QueryResponse(BaseModel):
"""問い合わせの応答"""
result: str
args: QueryArgs
class MultiQueryItem(BaseModel):
"""複数問い合わせの応答の各アイテム"""
id: int
result: str
args: QueryArgs
class MultiQueryResponse(BaseModel):
"""複数問い合わせの応答"""
data: list[MultiQueryItem]
meta: dict[str, Any]
class ApiResponse(BaseModel):
"""API応答の基本形式"""
data: QueryResponse
meta: dict[str, Any]