Spaces:
Building
Building
Update llm_openai.py
Browse files- llm_openai.py +74 -47
llm_openai.py
CHANGED
@@ -1,73 +1,100 @@
|
|
1 |
"""
|
2 |
OpenAI GPT Implementation
|
3 |
"""
|
4 |
-
|
5 |
from typing import Dict, List, Any
|
6 |
from llm_interface import LLMInterface
|
7 |
from logger import log_info, log_error, log_warning, log_debug
|
8 |
|
9 |
class OpenAILLM(LLMInterface):
|
10 |
-
"""OpenAI GPT integration
|
11 |
|
12 |
-
def __init__(self, api_key: str, model: str, settings: Dict[str, Any] = None):
|
13 |
super().__init__(settings)
|
14 |
-
self.
|
15 |
-
self.model =
|
16 |
-
self.
|
17 |
-
|
18 |
-
log_info(f"
|
19 |
-
|
20 |
-
def _map_model_name(self, model: str) -> str:
|
21 |
-
"""Map provider name to actual model name"""
|
22 |
-
mappings = {
|
23 |
-
"gpt4o": "gpt-4",
|
24 |
-
"gpt4o-mini": "gpt-4o-mini"
|
25 |
-
}
|
26 |
-
return mappings.get(model, model)
|
27 |
|
28 |
async def generate(self, system_prompt: str, user_input: str, context: List[Dict]) -> str:
|
29 |
-
"""Generate response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
try:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
except Exception as e:
|
54 |
-
log_error("
|
55 |
raise
|
56 |
|
57 |
async def startup(self, project_config: Dict) -> bool:
|
58 |
-
"""
|
59 |
-
log_info("
|
60 |
return True
|
61 |
|
62 |
def get_provider_name(self) -> str:
|
63 |
-
|
64 |
-
return self.model
|
65 |
|
66 |
def get_model_info(self) -> Dict[str, Any]:
|
67 |
-
"""Get model information"""
|
68 |
return {
|
69 |
"provider": "openai",
|
70 |
"model": self.model,
|
71 |
-
"
|
72 |
-
"
|
73 |
}
|
|
|
1 |
"""
|
2 |
OpenAI GPT Implementation
|
3 |
"""
|
4 |
+
import openai
|
5 |
from typing import Dict, List, Any
|
6 |
from llm_interface import LLMInterface
|
7 |
from logger import log_info, log_error, log_warning, log_debug
|
8 |
|
9 |
class OpenAILLM(LLMInterface):
|
10 |
+
"""OpenAI GPT integration with improved error handling"""
|
11 |
|
12 |
+
def __init__(self, api_key: str, model: str = "gpt-4", settings: Dict[str, Any] = None):
|
13 |
super().__init__(settings)
|
14 |
+
self.api_key = api_key
|
15 |
+
self.model = model
|
16 |
+
self.timeout = self.settings.get("timeout", DEFAULT_LLM_TIMEOUT)
|
17 |
+
openai.api_key = api_key
|
18 |
+
log_info(f"π OpenAI LLM initialized", model=self.model, timeout=self.timeout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
async def generate(self, system_prompt: str, user_input: str, context: List[Dict]) -> str:
|
21 |
+
"""Generate response with consistent error handling"""
|
22 |
+
|
23 |
+
# Build messages
|
24 |
+
messages = []
|
25 |
+
if system_prompt:
|
26 |
+
messages.append({"role": "system", "content": system_prompt})
|
27 |
+
|
28 |
+
# Add context
|
29 |
+
for msg in context[-10:]: # Last 10 messages
|
30 |
+
role = "assistant" if msg.get("role") == "assistant" else "user"
|
31 |
+
messages.append({"role": role, "content": msg.get("content", "")})
|
32 |
+
|
33 |
+
# Add current input
|
34 |
+
messages.append({"role": "user", "content": user_input})
|
35 |
+
|
36 |
try:
|
37 |
+
with LogTimer(f"OpenAI {self.model} request"):
|
38 |
+
# Use async client
|
39 |
+
client = openai.AsyncOpenAI(
|
40 |
+
api_key=self.api_key,
|
41 |
+
timeout=self.timeout
|
42 |
+
)
|
43 |
+
|
44 |
+
response = await client.chat.completions.create(
|
45 |
+
model=self.model,
|
46 |
+
messages=messages,
|
47 |
+
max_tokens=self.settings.get("max_tokens", 2048),
|
48 |
+
temperature=self.settings.get("temperature", 0.7),
|
49 |
+
stream=False
|
50 |
+
)
|
51 |
+
|
52 |
+
# Extract content
|
53 |
+
content = response.choices[0].message.content
|
54 |
+
|
55 |
+
# Check length
|
56 |
+
if len(content) > MAX_RESPONSE_LENGTH:
|
57 |
+
log_warning(f"Response exceeded max length, truncating",
|
58 |
+
original_length=len(content),
|
59 |
+
max_length=MAX_RESPONSE_LENGTH)
|
60 |
+
content = content[:MAX_RESPONSE_LENGTH] + "..."
|
61 |
+
|
62 |
+
# Log token usage
|
63 |
+
if response.usage:
|
64 |
+
log_info(f"Token usage",
|
65 |
+
prompt_tokens=response.usage.prompt_tokens,
|
66 |
+
completion_tokens=response.usage.completion_tokens,
|
67 |
+
total_tokens=response.usage.total_tokens)
|
68 |
+
|
69 |
+
return content
|
70 |
+
|
71 |
+
except openai.RateLimitError as e:
|
72 |
+
log_warning("OpenAI rate limit", error=str(e))
|
73 |
+
raise
|
74 |
+
except openai.APITimeoutError as e:
|
75 |
+
log_error("OpenAI timeout", error=str(e))
|
76 |
+
raise
|
77 |
+
except openai.APIError as e:
|
78 |
+
log_error("OpenAI API error",
|
79 |
+
status_code=e.status_code if hasattr(e, 'status_code') else None,
|
80 |
+
error=str(e))
|
81 |
+
raise
|
82 |
except Exception as e:
|
83 |
+
log_error("OpenAI unexpected error", error=str(e))
|
84 |
raise
|
85 |
|
86 |
async def startup(self, project_config: Dict) -> bool:
|
87 |
+
"""OpenAI doesn't need startup"""
|
88 |
+
log_info("OpenAI startup called (no-op)")
|
89 |
return True
|
90 |
|
91 |
def get_provider_name(self) -> str:
|
92 |
+
return f"openai-{self.model}"
|
|
|
93 |
|
94 |
def get_model_info(self) -> Dict[str, Any]:
|
|
|
95 |
return {
|
96 |
"provider": "openai",
|
97 |
"model": self.model,
|
98 |
+
"max_tokens": self.settings.get("max_tokens", 2048),
|
99 |
+
"temperature": self.settings.get("temperature", 0.7)
|
100 |
}
|