Spaces:
Running
Running
Update llm_interface.py
Browse files- llm_interface.py +46 -42
llm_interface.py
CHANGED
@@ -24,9 +24,12 @@ class LLMInterface(ABC):
|
|
24 |
class SparkLLM(LLMInterface):
|
25 |
"""Existing Spark integration"""
|
26 |
|
27 |
-
|
|
|
28 |
self.spark_endpoint = spark_endpoint.rstrip("/")
|
29 |
self.spark_token = spark_token
|
|
|
|
|
30 |
|
31 |
async def generate(self, system_prompt: str, user_input: str, context: List[Dict]) -> str:
|
32 |
headers = {
|
@@ -74,49 +77,50 @@ class GPT4oLLM(LLMInterface):
|
|
74 |
self.client = AsyncOpenAI(api_key=api_key)
|
75 |
log(f"✅ Initialized GPT LLM with model: {model}")
|
76 |
|
77 |
-
async def generate(self,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
try:
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
messages.append({"role": role, "content": msg["content"]})
|
93 |
-
|
94 |
-
# Add current user input
|
95 |
-
messages.append({"role": "user", "content": user_input})
|
96 |
-
|
97 |
-
# Debug log - final messages
|
98 |
-
log(f"📝 Total messages to GPT: {len(messages)}")
|
99 |
-
log(f"📝 System prompt preview: {system_prompt[:100]}...")
|
100 |
-
|
101 |
-
# Call OpenAI API
|
102 |
-
response = await self.client.chat.completions.create(
|
103 |
-
model=self.model,
|
104 |
-
messages=messages,
|
105 |
-
temperature=0.3, # Low temperature for consistency
|
106 |
-
max_tokens=150 # Düşürüldü (önceden 512)
|
107 |
-
)
|
108 |
-
|
109 |
-
content = response.choices[0].message.content.strip()
|
110 |
-
log(f"🪄 GPT response (first 120 chars): {content[:120]}")
|
111 |
-
|
112 |
-
# Log token usage for cost tracking
|
113 |
-
if response.usage:
|
114 |
-
log(f"📊 Tokens used - Input: {response.usage.prompt_tokens}, Output: {response.usage.completion_tokens}")
|
115 |
-
|
116 |
-
return content
|
117 |
-
|
118 |
except Exception as e:
|
119 |
-
log(f"❌
|
120 |
raise
|
121 |
|
122 |
async def startup(self, project_config: Dict) -> bool:
|
|
|
24 |
class SparkLLM(LLMInterface):
|
25 |
"""Existing Spark integration"""
|
26 |
|
27 |
+
|
28 |
+
def __init__(self, spark_endpoint: str, spark_token: str, work_mode: str = "cloud"):
|
29 |
self.spark_endpoint = spark_endpoint.rstrip("/")
|
30 |
self.spark_token = spark_token
|
31 |
+
self.work_mode = work_mode
|
32 |
+
log(f"🔌 SparkLLM initialized with endpoint: {self.spark_endpoint}")
|
33 |
|
34 |
async def generate(self, system_prompt: str, user_input: str, context: List[Dict]) -> str:
|
35 |
headers = {
|
|
|
77 |
self.client = AsyncOpenAI(api_key=api_key)
|
78 |
log(f"✅ Initialized GPT LLM with model: {model}")
|
79 |
|
80 |
+
async def generate(self, project_name: str, user_input: str, system_prompt: str, context: List[Dict], version_config: Dict = None) -> str:
|
81 |
+
"""Generate response from LLM with project context"""
|
82 |
+
headers = {
|
83 |
+
"Authorization": f"Bearer {self.spark_token}",
|
84 |
+
"Content-Type": "application/json"
|
85 |
+
}
|
86 |
+
|
87 |
+
# Build payload with all required fields for Spark
|
88 |
+
payload = {
|
89 |
+
"work_mode": self.work_mode,
|
90 |
+
"cloud_token": self.spark_token,
|
91 |
+
"project_name": project_name,
|
92 |
+
"system_prompt": system_prompt,
|
93 |
+
"user_input": user_input,
|
94 |
+
"context": context
|
95 |
+
}
|
96 |
+
|
97 |
+
# Add version-specific config if available
|
98 |
+
if version_config:
|
99 |
+
llm_config = version_config.get("llm", {})
|
100 |
+
payload.update({
|
101 |
+
"project_version": version_config.get("version_id"),
|
102 |
+
"repo_id": llm_config.get("repo_id"),
|
103 |
+
"generation_config": llm_config.get("generation_config"),
|
104 |
+
"use_fine_tune": llm_config.get("use_fine_tune"),
|
105 |
+
"fine_tune_zip": llm_config.get("fine_tune_zip")
|
106 |
+
})
|
107 |
+
|
108 |
try:
|
109 |
+
log(f"📤 Spark request payload keys: {list(payload.keys())}")
|
110 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
111 |
+
response = await client.post(
|
112 |
+
f"{self.spark_endpoint}/generate",
|
113 |
+
json=payload,
|
114 |
+
headers=headers
|
115 |
+
)
|
116 |
+
response.raise_for_status()
|
117 |
+
data = response.json()
|
118 |
+
return data.get("model_answer", data.get("assistant", data.get("text", "")))
|
119 |
+
except httpx.TimeoutException:
|
120 |
+
log("⏱️ Spark timeout")
|
121 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
except Exception as e:
|
123 |
+
log(f"❌ Spark error: {str(e)}")
|
124 |
raise
|
125 |
|
126 |
async def startup(self, project_config: Dict) -> bool:
|