-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathllm.py
More file actions
301 lines (263 loc) · 11.1 KB
/
llm.py
File metadata and controls
301 lines (263 loc) · 11.1 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
import os
import json
import time
from datetime import datetime
from typing import Optional, Tuple, Dict, List
# Provider configurations: base_url and env_var for API key
PROVIDER_CONFIGS: Dict[str, Dict[str, str]] = {
"openrouter": {
"base_url": "https://openrouter.ai/api/v1",
"env_var": "OPENROUTER_API_KEY",
"base_url_env_var": "OPENROUTER_API_BASE_URL",
},
"qwen": {
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"env_var": "QWEN_API_KEY",
"base_url_env_var": "QWEN_API_BASE_URL",
},
"deepseek": {
"base_url": "https://api.deepseek.com",
"env_var": "DEEPSEEK_API_KEY",
"base_url_env_var": "DEEPSEEK_API_BASE_URL",
},
"openai": {
"base_url": "https://api.openai.com/v1",
"env_var": "OPENAI_API_KEY",
"base_url_env_var": "OPENAI_API_BASE_URL",
},
"gemini": {
"base_url": None, # Uses native SDK
"env_var": "GEMINI_API_KEY",
"base_url_env_var": "GEMINI_API_BASE_URL",
},
"zhipu": {
"base_url": "https://open.bigmodel.cn/api/paas/v4/",
"env_var": "ZHIPUAI_API_KEY",
"base_url_env_var": "ZHIPUAI_API_BASE_URL",
},
}
class TokenUsageTracker:
def __init__(self, log_file: Optional[str] = None):
self.log_file = log_file
self.usage_records: List[Dict] = []
self.total_stats = {
"total_prompt_tokens": 0,
"total_completion_tokens": 0,
"total_tokens": 0,
"total_calls": 0,
}
if log_file:
os.makedirs(os.path.dirname(log_file) if os.path.dirname(log_file) else ".", exist_ok=True)
def add_record(self,
provider: str,
model: str,
prompt_tokens: int,
completion_tokens: int,
total_tokens: int,
agent_name: str = "unknown"):
record = {
"timestamp": datetime.now().isoformat(),
"provider": provider,
"model": model,
"agent_name": agent_name,
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": total_tokens,
}
self.usage_records.append(record)
self.total_stats["total_prompt_tokens"] += prompt_tokens
self.total_stats["total_completion_tokens"] += completion_tokens
self.total_stats["total_tokens"] += total_tokens
self.total_stats["total_calls"] += 1
def export_to_file(self, file_path: Optional[str] = None):
output_file = file_path or self.log_file
export_data = {
"export_time": datetime.now().isoformat(),
"summary": self.total_stats,
"records": self.usage_records,
}
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(export_data, f, ensure_ascii=False, indent=2)
print(f"Token usage statistics exported to: {output_file}")
return output_file
def print_summary(self):
print("\n" + "="*60)
print("Token Usage Summary")
print("="*60)
print(f"Total API calls: {self.total_stats['total_calls']}")
print(f"Total input tokens: {self.total_stats['total_prompt_tokens']:,}")
print(f"Total output tokens: {self.total_stats['total_completion_tokens']:,}")
print(f"Total tokens: {self.total_stats['total_tokens']:,}")
print("="*60 + "\n")
class LLMClient:
def __init__(
self,
api_key: str,
provider: str = "openrouter",
base_url: Optional[str] = None,
default_model: str = "google/gemini-3-flash-preview",
request_timeout: int = 600,
token_tracker: Optional[TokenUsageTracker] = None,
site_url: Optional[str] = None,
site_name: Optional[str] = None,
max_retries: int = 3,
retry_delay: float = 2.0,
) -> None:
self.provider = provider.lower()
self.default_model = default_model
self.request_timeout = request_timeout
self.token_tracker = token_tracker
self.current_agent_name = "unknown"
self.max_retries = max_retries
self.retry_delay = retry_delay
# Get provider config
config = PROVIDER_CONFIGS.get(self.provider, PROVIDER_CONFIGS["openrouter"])
if self.provider == "gemini":
# Use native Google Generative AI SDK
import google.generativeai as genai
genai.configure(api_key=api_key)
self._genai = genai
self._client = None
self._http_client = None
else:
# Use OpenAI-compatible API
import httpx
from openai import OpenAI
self._genai = None
# Set up headers (OpenRouter needs special headers)
extra_headers = {}
if self.provider == "openrouter":
extra_headers = {
"HTTP-Referer": site_url or "http://localhost",
"X-Title": site_name or "Rebuttal Assistant",
}
self._http_client = httpx.Client(
trust_env=True,
timeout=request_timeout,
headers=extra_headers
)
base_url_env_var = config.get("base_url_env_var")
env_base_url = os.environ.get(base_url_env_var) if base_url_env_var else None
effective_base_url = base_url or env_base_url or config["base_url"]
self._client = OpenAI(
base_url=effective_base_url,
api_key=api_key,
http_client=self._http_client,
)
def generate(
self,
instructions: Optional[str],
input_text: str,
model: Optional[str] = None,
enable_reasoning: bool = True,
temperature: float = 0.6,
agent_name: Optional[str] = None,
) -> Tuple[str, str]:
model_name = model or self.default_model
if agent_name:
self.current_agent_name = agent_name
final_text = ""
reasoning_text = ""
last_error = None
for attempt in range(self.max_retries + 1):
try:
if self.provider == "gemini":
# Use native Gemini SDK
final_text, reasoning_text = self._generate_gemini(
instructions, input_text, model_name, temperature
)
else:
# Use OpenAI-compatible API
final_text, reasoning_text = self._generate_openai_compatible(
instructions, input_text, model_name, temperature
)
rate_limit_keywords = ["并发", "rate limit", "too many requests", "quota exceeded", "限流"]
if any(keyword in final_text.lower() for keyword in rate_limit_keywords):
raise Exception(f"Rate limit detected in response: {final_text[:100]}...")
return final_text or "", reasoning_text or ""
except Exception as e:
last_error = e
if attempt < self.max_retries:
wait_time = self.retry_delay * (2 ** attempt) # Exponential backoff: 2s, 4s, 8s
print(f"[Retry] {self.current_agent_name} attempt {attempt + 1}/{self.max_retries} failed: {type(e).__name__}")
print(f"[Retry] Waiting {wait_time:.1f}s before retry...")
time.sleep(wait_time)
else:
print(f"[{self.provider.upper()} Error] All {self.max_retries + 1} attempts failed: {type(e).__name__}: {e}")
return f"Error calling {self.provider} after {self.max_retries + 1} attempts: {str(last_error)}", ""
def _generate_gemini(
self,
instructions: Optional[str],
input_text: str,
model_name: str,
temperature: float,
) -> Tuple[str, str]:
"""Generate using native Google Generative AI SDK"""
# Create model with system instruction
generation_config = {
"temperature": temperature,
}
model = self._genai.GenerativeModel(
model_name=model_name,
system_instruction=instructions or "You are a helpful AI assistant.",
generation_config=generation_config,
)
response = model.generate_content(input_text)
final_text = ""
if response.text:
final_text = response.text
# Track token usage if available
if self.token_tracker and hasattr(response, "usage_metadata"):
usage = response.usage_metadata
prompt_tokens = getattr(usage, "prompt_token_count", 0)
completion_tokens = getattr(usage, "candidates_token_count", 0)
total_tokens = getattr(usage, "total_token_count", 0)
self.token_tracker.add_record(
provider="gemini",
model=model_name,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=total_tokens,
agent_name=self.current_agent_name
)
print(f"[Token] {self.current_agent_name}: in={prompt_tokens}, out={completion_tokens}")
return final_text, ""
def _generate_openai_compatible(
self,
instructions: Optional[str],
input_text: str,
model_name: str,
temperature: float,
) -> Tuple[str, str]:
"""Generate using OpenAI-compatible API"""
messages = [
{"role": "system", "content": (instructions or "You are a helpful AI assistant.")},
{"role": "user", "content": input_text},
]
response = self._client.chat.completions.create(
model=model_name,
messages=messages,
temperature=temperature,
stream=False,
)
final_text = ""
if getattr(response, "choices", None):
choice0 = response.choices[0]
message = getattr(choice0, "message", None)
if message is not None:
final_text = getattr(message, "content", None) or ""
if self.token_tracker and hasattr(response, "usage"):
usage = response.usage
prompt_tokens = getattr(usage, "prompt_tokens", 0)
completion_tokens = getattr(usage, "completion_tokens", 0)
total_tokens = getattr(usage, "total_tokens", 0)
self.token_tracker.add_record(
provider=self.provider,
model=model_name,
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=total_tokens,
agent_name=self.current_agent_name
)
print(f"[Token] {self.current_agent_name}: in={prompt_tokens}, out={completion_tokens}")
return final_text, ""