Repository navigation
Expand file tree
/
Copy pathllm_client.py
More file actions
267 lines (240 loc) · 7.96 KB
/
Copy pathllm_client.py
File metadata and controls
267 lines (240 loc) · 7.96 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from __future__ import annotations
import asyncio
import json
from typing import (
AsyncGenerator,
Dict,
List,
Optional,
Type,
TypeVar,
)
from openai import APITimeoutError, AsyncOpenAI, RateLimitError
from pydantic import BaseModel, ValidationError
from src.core.config import settings
from src.core.logging_settings import logger
T = TypeVar("T", bound=BaseModel)
RETRYABLE = (RateLimitError, APITimeoutError)
class LLMClient:
def __init__(self) -> None:
self._chat = AsyncOpenAI(
api_key=settings.LLM_API_KEY,
base_url=settings.LLM_BASE_URL,
timeout=settings.REQUEST_TIMEOUT_S,
)
async def chat(
self,
messages: List[Dict[str, str]],
model: Optional[str] = None,
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> str:
target = model or settings.LLM_MODEL_PRIMARY
node_name = kwargs.pop("_metric_node", "llm")
result, error = await self._try_with_retries(
client=self._chat,
model=target,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
**kwargs,
)
if error is None:
return result
raise error
async def chat_stream(
self,
messages: List[Dict[str, str]],
model: Optional[str] = None,
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> AsyncGenerator[str, None]:
target = model or settings.LLM_MODEL_PRIMARY
try:
async for token in self._call_stream(
client=self._chat,
model=target,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
**kwargs,
):
yield token
except Exception as exc:
logger.error("Streaming error on model '{}': {}", target, exc)
yield f"\n\n[Ошибка: {exc}]"
@staticmethod
async def _call(
client: AsyncOpenAI,
model: str,
messages: List[Dict[str, str]],
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> str:
kwargs.pop("temperature", None)
kwargs.pop("max_tokens", None)
response = await client.chat.completions.create(
model=model,
messages=messages,
temperature=(
temperature
if temperature is not None
else settings.LLM_TEMPERATURE
),
max_tokens=(
max_tokens
if max_tokens is not None
else settings.LLM_MAX_TOKENS
),
**kwargs,
)
# Prometheus-метрики: token usage / стоимость LLM-вызовов.
try:
usage = getattr(response, "usage", None)
if usage is not None:
from src.core.metrics import observe_llm_tokens
node = kwargs.pop("_metric_node", "llm")
if getattr(usage, "prompt_tokens", None):
observe_llm_tokens(node, "prompt", usage.prompt_tokens)
if getattr(usage, "completion_tokens", None):
observe_llm_tokens(node, "completion", usage.completion_tokens)
except Exception:
pass
return response.choices[0].message.content or ""
@staticmethod
async def _call_stream(
client: AsyncOpenAI,
model: str,
messages: List[Dict[str, str]],
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> AsyncGenerator[str, None]:
kwargs.pop("temperature", None)
kwargs.pop("max_tokens", None)
stream = await client.chat.completions.create(
model=model,
messages=messages,
temperature=(
temperature
if temperature is not None
else settings.LLM_TEMPERATURE
),
max_tokens=(
max_tokens
if max_tokens is not None
else settings.LLM_MAX_TOKENS
),
stream=True,
**kwargs,
)
async for chunk in stream:
if chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
@staticmethod
async def _try_with_retries(
client: AsyncOpenAI,
model: str,
messages: List[Dict[str, str]],
temperature: Optional[float] = None,
max_tokens: Optional[int] = None,
**kwargs,
) -> tuple[str, Optional[Exception]]:
last_exc: Optional[Exception] = None
for attempt in range(settings.MAX_RETRIES):
try:
result = await LLMClient._call(
client=client,
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
**kwargs,
)
return result, None
except RETRYABLE as exc:
last_exc = exc
wait = min(2**attempt, 10)
logger.warning(
"Retry {}/{} on {} for model '{}', sleep {}s",
attempt + 1,
settings.MAX_RETRIES,
type(exc).__name__,
model,
wait,
)
await asyncio.sleep(wait)
return "", last_exc
async def parse_structured(
llm: LLMClient,
user_prompt: str,
schema: Type[T],
max_retries: int = 3,
system_prompt: Optional[str] = None,
temperature: float = 0.0,
max_tokens: Optional[int] = None,
) -> T:
schema_json = json.dumps(schema.model_json_schema(), ensure_ascii=False, indent=2)
schema_instruction = (
f"\n\nRespond strictly as JSON conforming to this schema:\n{schema_json}\n"
f"No text before or after the JSON."
)
system_content = (
(system_prompt + schema_instruction)
if system_prompt
else (
f"Respond strictly as JSON conforming to the schema:\n{schema_json}\n"
f"No text before or after the JSON."
)
)
messages: List[Dict[str, str]] = [
{"role": "system", "content": system_content},
{"role": "user", "content": user_prompt},
]
last_err: Optional[Exception] = None
for attempt in range(max_retries):
raw = await llm.chat(
messages, temperature=temperature, max_tokens=max_tokens
)
try:
data = json.loads(raw)
except json.JSONDecodeError as exc:
last_err = exc
logger.warning(
"parse_structured attempt {}/{}: invalid JSON: {}",
attempt + 1,
max_retries,
exc,
)
messages.append({"role": "assistant", "content": raw})
messages.append(
{
"role": "user",
"content": f"Invalid JSON: {exc}. Return a valid object that matches the schema.",
}
)
continue
try:
return schema.model_validate(data)
except ValidationError as exc:
last_err = exc
logger.warning(
"parse_structured attempt {}/{}: schema mismatch: {}",
attempt + 1,
max_retries,
exc,
)
messages.append({"role": "assistant", "content": raw})
messages.append(
{
"role": "user",
"content": f"JSON does not match the schema: {exc}. Fix it.",
}
)
continue
if last_err:
raise last_err
raise RuntimeError("Failed to parse structured output")