-
Notifications
You must be signed in to change notification settings - Fork 757
Expand file tree
/
Copy pathllm_api.py
More file actions
281 lines (244 loc) · 10.6 KB
/
Copy pathllm_api.py
File metadata and controls
281 lines (244 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
#!/usr/bin/env python3
from google import genai
from openai import OpenAI, AzureOpenAI
from anthropic import Anthropic
import argparse
import os
from dotenv import load_dotenv
from pathlib import Path
import sys
import base64
from typing import Any, Literal, Optional, cast
import mimetypes
Provider = Literal["openai", "azure", "deepseek", "siliconflow", "anthropic", "gemini", "local"]
def load_environment():
"""Load environment variables from .env files in order of precedence"""
# Order of precedence:
# 1. System environment variables (already loaded)
# 2. .env.local (user-specific overrides)
# 3. .env (project defaults)
# 4. .env.example (example configuration)
env_files = ['.env.local', '.env', '.env.example']
env_loaded = False
print("Current working directory:", Path('.').absolute(), file=sys.stderr)
print("Looking for environment files:", env_files, file=sys.stderr)
for env_file in env_files:
env_path = Path('.') / env_file
print(f"Checking {env_path.absolute()}", file=sys.stderr)
if env_path.exists():
print(f"Found {env_file}, loading variables...", file=sys.stderr)
load_dotenv(dotenv_path=env_path)
env_loaded = True
print(f"Loaded environment variables from {env_file}", file=sys.stderr)
# Print loaded keys (but not values for security)
with open(env_path) as f:
keys = [line.split('=')[0].strip() for line in f if '=' in line and not line.startswith('#')]
print(f"Keys loaded from {env_file}: {keys}", file=sys.stderr)
if not env_loaded:
print("Warning: No .env files found. Using system environment variables only.", file=sys.stderr)
print("Available system environment variables:", list(os.environ.keys()), file=sys.stderr)
# Load environment variables at module import
load_environment()
def encode_image_file(image_path: str) -> tuple[str, str]:
"""
Encode an image file to base64 and determine its MIME type.
Args:
image_path (str): Path to the image file
Returns:
tuple: (base64_encoded_string, mime_type)
"""
mime_type, _ = mimetypes.guess_type(image_path)
if not mime_type:
mime_type = 'image/png' # Default to PNG if type cannot be determined
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string, mime_type
def create_llm_client(provider: Provider = "openai"):
if provider == "openai":
api_key = os.getenv('OPENAI_API_KEY')
base_url = os.getenv('OPENAI_BASE_URL', "https://api.openai.com/v1")
if not api_key:
raise ValueError("OPENAI_API_KEY not found in environment variables")
return OpenAI(
api_key=api_key,
base_url=base_url
)
elif provider == "azure":
api_key = os.getenv('AZURE_OPENAI_API_KEY')
if not api_key:
raise ValueError("AZURE_OPENAI_API_KEY not found in environment variables")
return AzureOpenAI(
api_key=api_key,
api_version="2024-08-01-preview",
azure_endpoint="https://msopenai.openai.azure.com"
)
elif provider == "deepseek":
api_key = os.getenv('DEEPSEEK_API_KEY')
if not api_key:
raise ValueError("DEEPSEEK_API_KEY not found in environment variables")
return OpenAI(
api_key=api_key,
base_url="https://api.deepseek.com/v1",
)
elif provider == "siliconflow":
api_key = os.getenv('SILICONFLOW_API_KEY')
if not api_key:
raise ValueError("SILICONFLOW_API_KEY not found in environment variables")
return OpenAI(
api_key=api_key,
base_url="https://api.siliconflow.cn/v1"
)
elif provider == "anthropic":
api_key = os.getenv('ANTHROPIC_API_KEY')
if not api_key:
raise ValueError("ANTHROPIC_API_KEY not found in environment variables")
return Anthropic(
api_key=api_key
)
elif provider == "gemini":
api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
raise ValueError("GOOGLE_API_KEY not found in environment variables")
return genai.Client(api_key=api_key)
elif provider == "local":
return OpenAI(
base_url="http://192.168.180.137:8006/v1",
api_key="not-needed"
)
else:
raise ValueError(f"Unsupported provider: {provider}")
def query_llm(prompt: str, client: Optional[Any] = None, model: Optional[str] = None, provider: Provider ="openai", image_path: Optional[str] = None) -> Optional[str]:
"""
Query an LLM with a prompt and optional image attachment.
Args:
prompt (str): The text prompt to send
client: The LLM client instance
model (str, optional): The model to use
provider (str): The API provider to use
image_path (str, optional): Path to an image file to attach
Returns:
Optional[str]: The LLM's response or None if there was an error
"""
if client is None:
client = create_llm_client(provider)
try:
# Set default model
if model is None:
if provider == "openai":
model = os.getenv('OPENAI_MODEL_DEPLOYMENT', 'gpt-4o')
elif provider == "azure":
model = os.getenv('AZURE_OPENAI_MODEL_DEPLOYMENT', 'gpt-4o-ms') # Get from env with fallback
elif provider == "deepseek":
model = "deepseek-chat"
elif provider == "siliconflow":
model = "deepseek-ai/DeepSeek-R1"
elif provider == "anthropic":
model = "claude-3-7-sonnet-20250219"
elif provider == "gemini":
model = "gemini-2.5-flash"
elif provider == "local":
model = "Qwen/Qwen2.5-32B-Instruct-AWQ"
if provider in ["openai", "local", "deepseek", "azure", "siliconflow"]:
messages = [{"role": "user", "content": []}]
# Add text content
messages[0]["content"].append({
"type": "text",
"text": prompt
})
# Add image content if provided
if image_path:
if provider == "openai":
encoded_image, mime_type = encode_image_file(image_path)
messages[0]["content"] = [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:{mime_type};base64,{encoded_image}"}}
]
kwargs = {
"model": model,
"messages": messages,
"temperature": 0.7,
}
# Add o1-specific parameters
if model == "o1":
kwargs["response_format"] = {"type": "text"}
kwargs["reasoning_effort"] = "low"
del kwargs["temperature"]
response = client.chat.completions.create(**kwargs)
return response.choices[0].message.content
elif provider == "anthropic":
messages = [{"role": "user", "content": []}]
# Add text content
messages[0]["content"].append({
"type": "text",
"text": prompt
})
# Add image content if provided
if image_path:
encoded_image, mime_type = encode_image_file(image_path)
messages[0]["content"].append({
"type": "image",
"source": {
"type": "base64",
"media_type": mime_type,
"data": encoded_image
}
})
response = client.messages.create(
model=model,
max_tokens=1000,
messages=messages
)
return response.content[0].text
elif provider == "gemini":
gemini_client = cast(genai.Client, client)
if image_path:
file = gemini_client.files.upload(
file=image_path,
config=genai.types.UploadFileConfig(mime_type="image/png")
)
chat_session = gemini_client.chats.create(
model=model,
history=[
genai.types.Content(
role="user",
parts=[
genai.types.Part.from_uri(file_uri=str(file.uri), mime_type=file.mime_type),
]
)
]
)
else:
chat_session = gemini_client.chats.create(model=model)
response = chat_session.send_message(message=prompt)
return response.text
except Exception as e:
print(f"Error querying LLM: {e}", file=sys.stderr)
return None
def main():
parser = argparse.ArgumentParser(description='Query an LLM with a prompt')
parser.add_argument('--prompt', type=str, help='The prompt to send to the LLM', required=True)
parser.add_argument('--provider', choices=['openai','anthropic','gemini','local','deepseek','azure','siliconflow'], default='openai', help='The API provider to use')
parser.add_argument('--model', type=str, help='The model to use (default depends on provider)')
parser.add_argument('--image', type=str, help='Path to an image file to attach to the prompt')
args = parser.parse_args()
if not args.model:
if args.provider == 'openai':
args.model = "gpt-4o"
elif args.provider == "deepseek":
args.model = "deepseek-chat"
elif args.provider == "siliconflow":
args.model = "deepseek-ai/DeepSeek-R1"
elif args.provider == 'anthropic':
args.model = "claude-3-7-sonnet-20250219"
elif args.provider == 'gemini':
args.model = "gemini-2.5-flash"
elif args.provider == 'azure':
args.model = os.getenv('AZURE_OPENAI_MODEL_DEPLOYMENT', 'gpt-4o-ms') # Get from env with fallback
client = create_llm_client(args.provider)
response = query_llm(args.prompt, client, model=args.model, provider=args.provider, image_path=args.image)
if response:
print(response)
else:
print("Failed to get response from LLM")
if __name__ == "__main__":
main()