-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcall_llm.py
More file actions
343 lines (290 loc) · 11 KB
/
call_llm.py
File metadata and controls
343 lines (290 loc) · 11 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from google import genai
import os
import logging
import json
import boto3
from datetime import datetime
# Configure logging
log_directory = os.getenv("LOG_DIR", "logs")
os.makedirs(log_directory, exist_ok=True)
log_file = os.path.join(
log_directory, f"llm_calls_{datetime.now().strftime('%Y%m%d')}.log"
)
# Set up logger
logger = logging.getLogger("llm_logger")
logger.setLevel(logging.INFO)
logger.propagate = False # Prevent propagation to root logger
file_handler = logging.FileHandler(log_file, encoding='utf-8')
file_handler.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
logger.addHandler(file_handler)
# Simple cache configuration
cache_file = "llm_cache.json"
# By default, we Google Gemini 2.5 pro, as it shows great performance for code understanding
def call_llm(prompt: str, use_cache: bool = True) -> str:
# Log the prompt
logger.info(f"PROMPT: {prompt}")
# Check cache if enabled
if use_cache:
# Load cache from disk
cache = {}
if os.path.exists(cache_file):
try:
with open(cache_file, "r", encoding="utf-8") as f:
cache = json.load(f)
except:
logger.warning(f"Failed to load cache, starting with empty cache")
# Return from cache if exists
if prompt in cache:
logger.info(f"RESPONSE: {cache[prompt]}")
return cache[prompt]
# # Call the LLM if not in cache or cache disabled
# client = genai.Client(
# vertexai=True,
# # TODO: change to your own project id and location
# project=os.getenv("GEMINI_PROJECT_ID", "your-project-id"),
# location=os.getenv("GEMINI_LOCATION", "us-central1")
# )
# You can comment the previous line and use the AI Studio key instead:
client = genai.Client(
api_key=os.getenv("GEMINI_API_KEY", ""),
)
model = os.getenv("GEMINI_MODEL", "gemini-2.5-pro-exp-03-25")
# model = os.getenv("GEMINI_MODEL", "gemini-2.5-flash-preview-04-17")
response = client.models.generate_content(model=model, contents=[prompt])
response_text = response.text
# Log the response
logger.info(f"RESPONSE: {response_text}")
# Update cache if enabled
if use_cache:
# Load cache again to avoid overwrites
cache = {}
if os.path.exists(cache_file):
try:
with open(cache_file, "r", encoding="utf-8") as f:
cache = json.load(f)
except:
pass
# Add to cache and save
cache[prompt] = response_text
try:
with open(cache_file, "w", encoding="utf-8") as f:
json.dump(cache, f)
except Exception as e:
logger.error(f"Failed to save cache: {e}")
return response_text
# # Use Azure OpenAI
# def call_llm(prompt, use_cache: bool = True):
# from openai import AzureOpenAI
# endpoint = "https://<azure openai name>.openai.azure.com/"
# deployment = "<deployment name>"
# subscription_key = "<azure openai key>"
# api_version = "<api version>"
# client = AzureOpenAI(
# api_version=api_version,
# azure_endpoint=endpoint,
# api_key=subscription_key,
# )
# r = client.chat.completions.create(
# model=deployment,
# messages=[{"role": "user", "content": prompt}],
# response_format={
# "type": "text"
# },
# max_completion_tokens=40000,
# reasoning_effort="medium",
# store=False
# )
# return r.choices[0].message.content
# # Use Anthropic Claude 3.7 Sonnet Extended Thinking
# def call_llm(prompt, use_cache: bool = True):
# from anthropic import Anthropic
# client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY", "your-api-key"))
# response = client.messages.create(
# model="claude-3-7-sonnet-20250219",
# max_tokens=21000,
# thinking={
# "type": "enabled",
# "budget_tokens": 20000
# },
# messages=[
# {"role": "user", "content": prompt}
# ]
# )
# return response.content[1].text
# # Use Anthropic Claude via AWS Bedrock
# def call_llm(prompt: str, use_cache: bool = True) -> str:
# import boto3
# # Log the prompt
# logger.info(f"PROMPT: {prompt}")
# # Check cache if enabled
# if use_cache:
# # Load cache from disk
# cache = {}
# if os.path.exists(cache_file):
# try:
# with open(cache_file, "r", encoding="utf-8") as f:
# cache = json.load(f)
# except:
# logger.warning(f"Failed to load cache, starting with empty cache")
# # Return from cache if exists
# if prompt in cache:
# logger.info(f"RESPONSE: {cache[prompt]}")
# return cache[prompt]
# # Configure AWS Bedrock client
# # You can set AWS credentials via environment variables:
# # AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN (optional)
# # Or use AWS profiles/IAM roles
# # Support for AWS profiles
# aws_profile = os.getenv('AWS_PROFILE')
# aws_region = os.getenv('AWS_REGION', 'us-west-2')
# if aws_profile:
# # Use specific profile
# session = boto3.Session(profile_name=aws_profile)
# bedrock_runtime = session.client(
# service_name='bedrock-runtime',
# region_name=aws_region
# )
# else:
# # Use default credentials (env vars, default profile, IAM role, etc.)
# bedrock_runtime = boto3.client(
# service_name='bedrock-runtime',
# region_name=aws_region
# )
# # Model ID or Inference Profile ARN - you can use different Claude models available in Bedrock
# # Direct model IDs: anthropic.claude-3-sonnet-20240229-v1:0, anthropic.claude-3-haiku-20240307-v1:0
# # Inference profiles for newer models: us.anthropic.claude-3-5-sonnet-20241022-v2:0
# model_id = os.getenv('BEDROCK_MODEL_ID', 'us.anthropic.claude-3-5-sonnet-20241022-v2:0')
# # Prepare the request body for Anthropic models in Bedrock
# request_body = {
# "anthropic_version": "bedrock-2023-05-31",
# "max_tokens": int(os.getenv('BEDROCK_MAX_TOKENS', '200000')),
# "messages": [
# {
# "role": "user",
# "content": prompt
# }
# ],
# "temperature": float(os.getenv('BEDROCK_TEMPERATURE', '0.7')),
# "top_p": float(os.getenv('BEDROCK_TOP_P', '0.9'))
# }
# try:
# # Invoke the model
# response = bedrock_runtime.invoke_model(
# modelId=model_id,
# contentType='application/json',
# accept='application/json',
# body=json.dumps(request_body)
# )
# # Parse the response
# response_body = json.loads(response['body'].read())
# response_text = response_body['content'][0]['text']
# # Log the response
# logger.info(f"RESPONSE: {response_text}")
# # Update cache if enabled
# if use_cache:
# # Load cache again to avoid overwrites
# cache = {}
# if os.path.exists(cache_file):
# try:
# with open(cache_file, "r", encoding="utf-8") as f:
# cache = json.load(f)
# except:
# pass
# # Add to cache and save
# cache[prompt] = response_text
# try:
# with open(cache_file, "w", encoding="utf-8") as f:
# json.dump(cache, f)
# except Exception as e:
# logger.error(f"Failed to save cache: {e}")
# return response_text
# except Exception as e:
# error_msg = f"Bedrock API call failed: {str(e)}"
# logger.error(error_msg)
# raise Exception(error_msg)
# # Use OpenAI o1
# def call_llm(prompt, use_cache: bool = True):
# from openai import OpenAI
# client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
# r = client.chat.completions.create(
# model="o1",
# messages=[{"role": "user", "content": prompt}],
# response_format={
# "type": "text"
# },
# reasoning_effort="medium",
# store=False
# )
# return r.choices[0].message.content
# Use OpenRouter API
# def call_llm(prompt: str, use_cache: bool = True) -> str:
# import requests
# # Log the prompt
# logger.info(f"PROMPT: {prompt}")
# # Check cache if enabled
# if use_cache:
# # Load cache from disk
# cache = {}
# if os.path.exists(cache_file):
# try:
# with open(cache_file, "r", encoding="utf-8") as f:
# cache = json.load(f)
# except:
# logger.warning(f"Failed to load cache, starting with empty cache")
# # Return from cache if exists
# if prompt in cache:
# logger.info(f"RESPONSE: {cache[prompt]}")
# return cache[prompt]
# # OpenRouter API configuration
# api_key = os.getenv("OPENROUTER_API_KEY", "")
# model = os.getenv("OPENROUTER_MODEL", "google/gemini-2.0-flash-exp:free")
# headers = {
# "Authorization": f"Bearer {api_key}",
# }
# data = {
# "model": model,
# "messages": [{"role": "user", "content": prompt}]
# }
# response = requests.post(
# "https://openrouter.ai/api/v1/chat/completions",
# headers=headers,
# json=data
# )
# if response.status_code != 200:
# error_msg = f"OpenRouter API call failed with status {response.status_code}: {response.text}"
# logger.error(error_msg)
# raise Exception(error_msg)
# try:
# response_text = response.json()["choices"][0]["message"]["content"]
# except Exception as e:
# error_msg = f"Failed to parse OpenRouter response: {e}; Response: {response.text}"
# logger.error(error_msg)
# raise Exception(error_msg)
# # Log the response
# logger.info(f"RESPONSE: {response_text}")
# # Update cache if enabled
# if use_cache:
# # Load cache again to avoid overwrites
# cache = {}
# if os.path.exists(cache_file):
# try:
# with open(cache_file, "r", encoding="utf-8") as f:
# cache = json.load(f)
# except:
# pass
# # Add to cache and save
# cache[prompt] = response_text
# try:
# with open(cache_file, "w", encoding="utf-8") as f:
# json.dump(cache, f)
# except Exception as e:
# logger.error(f"Failed to save cache: {e}")
# return response_text
if __name__ == "__main__":
test_prompt = "Hello, how are you?"
# First call - should hit the API
print("Making call...")
response1 = call_llm(test_prompt, use_cache=False)
print(f"Response: {response1}")