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