-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathllm_services.py
More file actions
326 lines (262 loc) · 10.6 KB
/
llm_services.py
File metadata and controls
326 lines (262 loc) · 10.6 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
LLM service factory for creating configured LLM clients.
Includes a compatibility layer for OpenAI-compatible API proxies that may
return slightly non-standard responses (e.g. choices[].index = None).
Supports multiple providers: openai-compatible, anthropic, bedrock, azure-openai.
"""
from __future__ import annotations
import logging
from typing import Any
from openai.types import chat
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
from pydantic_ai.models.openai import OpenAIModelSettings
from pydantic_ai.models.fallback import FallbackModel
from openai import OpenAI
from codewiki.src.config import Config
logger = logging.getLogger(__name__)
def _should_use_max_completion_tokens(model_name: str, base_url: str) -> bool:
"""
Determine whether to use max_completion_tokens instead of max_tokens.
Newer OpenAI models (o1, o3, gpt-4o, etc.) require max_completion_tokens.
Anthropic and other providers still use max_tokens.
"""
model_lower = model_name.lower()
# OpenAI models that require max_completion_tokens
new_openai_patterns = ("o1", "o3", "gpt-4o", "gpt-4-turbo")
if any(pattern in model_lower for pattern in new_openai_patterns):
return True
# If base_url points to OpenAI directly, newer models may need it
if base_url and "api.openai.com" in base_url:
return True
return False
def _build_model_settings(config: Config, model_name: str) -> OpenAIModelSettings:
"""Build model settings with the correct token parameter."""
if _should_use_max_completion_tokens(model_name, config.llm_base_url):
return OpenAIModelSettings(
temperature=0.0,
max_completion_tokens=config.max_tokens
)
return OpenAIModelSettings(
temperature=0.0,
max_tokens=config.max_tokens
)
def _get_litellm_model_name(model_name: str, provider: str) -> str:
"""
Get the litellm-compatible model name for a given provider.
For Bedrock, prefixes the model name with 'bedrock/' if not already prefixed.
For Anthropic, prefixes with 'anthropic/' if not already prefixed.
"""
if provider == "bedrock":
if not model_name.startswith("bedrock/"):
return f"bedrock/{model_name}"
elif provider == "anthropic":
if not model_name.startswith("anthropic/"):
return f"anthropic/{model_name}"
return model_name
class CompatibleOpenAIModel(OpenAIModel):
"""OpenAIModel subclass that patches non-standard API proxy responses.
Some OpenAI-compatible proxies return responses with fields like
choices[].index set to None instead of an integer. This subclass
fixes those fields before pydantic validation runs.
"""
def _validate_completion(self, response: chat.ChatCompletion) -> chat.ChatCompletion:
# Patch choices[].index: None -> sequential integer (0, 1, 2, ...)
if response.choices:
for i, choice in enumerate(response.choices):
if choice.index is None:
choice.index = i
return super()._validate_completion(response)
def _anthropic_api_model_name(model_name: str) -> str:
"""Strip LiteLLM-style prefix for the native Anthropic Messages API."""
m = model_name.strip()
if m.lower().startswith("anthropic/"):
return m.split("/", 1)[1]
return m
def _anthropic_provider_base_url(config: Config) -> str | None:
"""
Base URL for AnthropicProvider; None uses Anthropic's default host.
Local LiteLLM OpenAI-compatible URLs are not valid for the Anthropic SDK.
"""
raw = (config.llm_base_url or "").strip()
if not raw:
return None
norm = raw.rstrip("/")
lowered = norm.lower()
if any(
hint in lowered
for hint in ("0.0.0.0:4000", "127.0.0.1:4000", "localhost:4000")
):
logger.warning(
"provider=anthropic but llm_base_url looks like a local OpenAI-compatible proxy (%r). "
"Using Anthropic's default API host. For LiteLLM, use provider=openai-compatible.",
raw,
)
return None
return norm or None
def _create_anthropic_model(config: Config, model_name: str) -> Any:
"""pydantic-ai Anthropic (Messages API), not OpenAI-compatible HTTP."""
try:
from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings
from pydantic_ai.providers.anthropic import AnthropicProvider
except ImportError as e: # pragma: no cover
raise ImportError(
'Native Anthropic requires the anthropic extra. Install with: '
'pip install "pydantic-ai[anthropic]"'
) from e
provider = AnthropicProvider(
api_key=config.llm_api_key or None,
base_url=_anthropic_provider_base_url(config),
)
return AnthropicModel(
_anthropic_api_model_name(model_name),
provider=provider,
settings=AnthropicModelSettings(temperature=0.0, max_tokens=config.max_tokens),
)
def _create_litellm_openai_client(config: Config) -> OpenAI:
"""
Create an OpenAI-compatible client backed by litellm's proxy.
litellm translates OpenAI API calls to Bedrock, Anthropic, etc.
"""
import litellm
# Configure litellm for the provider
if config.provider == "bedrock":
import os
os.environ.setdefault("AWS_DEFAULT_REGION", config.aws_region)
os.environ.setdefault("AWS_REGION_NAME", config.aws_region)
# litellm exposes an OpenAI-compatible Router we can use,
# but the simplest path is to use litellm.completion() directly.
# For pydantic-ai integration, we create a proxy client.
return OpenAI(
api_key=config.llm_api_key or "not-needed-for-bedrock",
base_url=config.llm_base_url or "https://api.openai.com/v1",
)
def create_main_model(config: Config) -> Any:
"""Create the main LLM model from configuration."""
if (config.provider or "").strip().lower() == "anthropic":
return _create_anthropic_model(config, config.main_model)
return CompatibleOpenAIModel(
model_name=config.main_model,
provider=OpenAIProvider(
base_url=config.llm_base_url,
api_key=config.llm_api_key
),
settings=_build_model_settings(config, config.main_model)
)
def create_fallback_model(config: Config) -> Any:
"""Create the fallback LLM model from configuration."""
if (config.provider or "").strip().lower() == "anthropic":
return _create_anthropic_model(config, config.fallback_model)
return CompatibleOpenAIModel(
model_name=config.fallback_model,
provider=OpenAIProvider(
base_url=config.llm_base_url,
api_key=config.llm_api_key
),
settings=_build_model_settings(config, config.fallback_model)
)
def create_fallback_models(config: Config) -> FallbackModel:
"""Create fallback models chain from configuration."""
main = create_main_model(config)
fallback = create_fallback_model(config)
return FallbackModel(main, fallback)
def create_openai_client(config: Config) -> OpenAI:
"""Create OpenAI client from configuration."""
return OpenAI(
base_url=config.llm_base_url,
api_key=config.llm_api_key
)
def call_llm(
prompt: str,
config: Config,
model: str = None,
temperature: float = 0.0
) -> str:
"""
Call LLM with the given prompt.
Supports openai-compatible, anthropic, and bedrock providers.
For bedrock/anthropic, uses litellm to translate the API call.
Args:
prompt: The prompt to send
config: Configuration containing LLM settings
model: Model name (defaults to config.main_model)
temperature: Temperature setting
Returns:
LLM response text
"""
if model is None:
model = config.main_model
provider = getattr(config, "provider", "openai-compatible")
if provider in ("bedrock", "anthropic"):
return _call_llm_via_litellm(prompt, config, model, temperature)
if provider == "azure-openai":
return _call_llm_via_azure(prompt, config, model, temperature)
# Default: OpenAI-compatible
client = create_openai_client(config)
# Use the correct token parameter based on model/provider
token_kwargs = {}
if _should_use_max_completion_tokens(model, config.llm_base_url):
token_kwargs["max_completion_tokens"] = config.max_tokens
logger.debug("Using max_completion_tokens=%d for model %s", config.max_tokens, model)
else:
token_kwargs["max_tokens"] = config.max_tokens
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
**token_kwargs
)
return response.choices[0].message.content
def _call_llm_via_litellm(
prompt: str,
config: Config,
model: str,
temperature: float = 0.0
) -> str:
"""
Call LLM via litellm for Bedrock/Anthropic providers.
litellm handles the provider-specific API translation automatically.
"""
import litellm
import os
litellm_model = _get_litellm_model_name(model, config.provider)
if config.provider == "bedrock":
os.environ.setdefault("AWS_DEFAULT_REGION", config.aws_region)
os.environ.setdefault("AWS_REGION_NAME", config.aws_region)
logger.debug("Calling Bedrock model %s in region %s", litellm_model, config.aws_region)
elif config.provider == "anthropic":
logger.debug("Calling Anthropic model %s via litellm", litellm_model)
response = litellm.completion(
model=litellm_model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
max_tokens=config.max_tokens,
api_key=config.llm_api_key if config.provider != "bedrock" else None,
)
return response.choices[0].message.content
def _call_llm_via_azure(
prompt: str,
config: Config,
model: str,
temperature: float = 0.0
) -> str:
"""
Call LLM via Azure OpenAI.
Uses the AzureOpenAI client from the openai package with
azure_endpoint, api_version, and deployment name.
"""
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=config.llm_api_key,
api_version=config.api_version,
azure_endpoint=config.llm_base_url,
)
deployment = config.azure_deployment or model
logger.debug("Calling Azure OpenAI deployment %s (api_version=%s)", deployment, config.api_version)
response = client.chat.completions.create(
model=deployment,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
max_tokens=config.max_tokens,
)
return response.choices[0].message.content