-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathclient.py
More file actions
182 lines (147 loc) · 5.33 KB
/
client.py
File metadata and controls
182 lines (147 loc) · 5.33 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
"""Unified LLM client for the application."""
import os
import threading
from typing import Any, List, Optional
from types import SimpleNamespace
from urllib.parse import urlparse, urlunparse
import openai
from openai import OpenAI
from tenacity import (
RetryCallState,
retry,
retry_if_exception_type,
stop_after_attempt,
wait_random_exponential,
)
from app.core.utils.cache import get_llm_cache, memoize
from app.core.utils.logger import setup_logger
_global_client: Optional[OpenAI] = None
_client_lock = threading.Lock()
logger = setup_logger("llm_client")
def normalize_base_url(base_url: str) -> str:
"""Normalize API base URL by ensuring /v1 suffix when needed.
Handles various edge cases:
- Removes leading/trailing whitespace
- Only adds /v1 if domain has no path, or path is empty/root
- Removes trailing slashes from /v1 (e.g., /v1/ -> /v1)
- Preserves custom paths (e.g., /custom stays as /custom)
Args:
base_url: Raw base URL string
Returns:
Normalized base URL
Examples:
>>> normalize_base_url("https://api.openai.com")
'https://api.openai.com/v1'
>>> normalize_base_url("https://api.openai.com/v1/")
'https://api.openai.com/v1'
>>> normalize_base_url("https://api.openai.com/custom")
'https://api.openai.com/custom'
>>> normalize_base_url(" https://api.openai.com ")
'https://api.openai.com/v1'
"""
url = base_url.strip()
parsed = urlparse(url)
path = parsed.path.rstrip("/")
if not path:
path = "/v1"
normalized = urlunparse(
(
parsed.scheme,
parsed.netloc,
path,
parsed.params,
parsed.query,
parsed.fragment,
)
)
return normalized
def get_llm_client() -> OpenAI:
"""Get global LLM client instance (thread-safe singleton).
Returns:
Global OpenAI client instance
Raises:
ValueError: If OPENAI_BASE_URL or OPENAI_API_KEY env vars not set
"""
global _global_client
if _global_client is None:
with _client_lock:
# Double-check locking pattern
if _global_client is None:
base_url = os.getenv("OPENAI_BASE_URL", "").strip()
base_url = normalize_base_url(base_url)
api_key = os.getenv("OPENAI_API_KEY", "").strip()
if not base_url or not api_key:
raise ValueError(
"OPENAI_BASE_URL and OPENAI_API_KEY environment variables must be set"
)
_global_client = OpenAI(base_url=base_url, api_key=api_key)
return _global_client
def before_sleep_log(retry_state: RetryCallState) -> None:
logger.warning(
"Rate Limit Error, sleeping and retrying... Please lower your thread concurrency or use better OpenAI API."
)
@memoize(get_llm_cache(), expire=3600, typed=True)
@retry(
stop=stop_after_attempt(10),
wait=wait_random_exponential(multiplier=1, min=5, max=60),
retry=retry_if_exception_type(openai.RateLimitError),
before_sleep=before_sleep_log,
)
def call_llm(
messages: List[dict],
model: str,
temperature: float = 1,
**kwargs: Any,
) -> Any:
"""Call LLM API with automatic caching.
Uses global LLM client configured via environment variables.
Args:
messages: Chat messages list
model: Model name
temperature: Sampling temperature
**kwargs: Additional parameters for API call
Returns:
API response object
Raises:
ValueError: If response is invalid (empty choices or content)
"""
client = get_llm_client()
# Check whether it is the ModelScope platform, as some models require the stream and enable_thinking parameters
if "modelscope" in str(client.base_url):
logger.info("Detected ModelScope API, using stream mode with enable_thinking=True.")
extra_body = {"enable_thinking": True}
response_stream = client.chat.completions.create(
model=model,
messages=messages, # pyright: ignore[reportArgumentType]
temperature=temperature,
stream=True,
extra_body=extra_body,
**kwargs,
)
full_content = ""
for chunk in response_stream:
if chunk.choices and chunk.choices[0].delta.content:
full_content += chunk.choices[0].delta.content
if not full_content:
raise ValueError("ModelScope streaming response yielded no content")
fake_message = SimpleNamespace(content=full_content)
fake_choice = SimpleNamespace(message=fake_message)
response = SimpleNamespace(choices=[fake_choice])
else:
response = client.chat.completions.create(
model=model,
messages=messages, # pyright: ignore[reportArgumentType]
temperature=temperature,
**kwargs,
)
# Validate response (exceptions are not cached by diskcache)
if not (
response
and hasattr(response, "choices")
and response.choices
and len(response.choices) > 0
and hasattr(response.choices[0], "message")
and response.choices[0].message.content
):
raise ValueError("Invalid OpenAI API response: empty choices or content")
return response