Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,8 @@ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
|
| 19 |
tavily = TavilyClient(api_key=TAVILY_API_KEY)
|
| 20 |
|
| 21 |
# --- Helper Functions ---
|
|
|
|
|
|
|
| 22 |
def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3500, temperature=0.7):
|
| 23 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 24 |
headers = {
|
|
@@ -34,15 +36,18 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
|
|
| 34 |
"stream": True
|
| 35 |
}
|
| 36 |
with requests.post(url, headers=headers, json=data, stream=True) as response:
|
| 37 |
-
content = ""
|
| 38 |
-
for line in response.iter_lines():
|
| 39 |
if line:
|
| 40 |
decoded = line.decode("utf-8")
|
| 41 |
if decoded.startswith("data: "):
|
| 42 |
piece = decoded.replace("data: ", "").strip()
|
| 43 |
if piece != "[DONE]":
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
yield content
|
| 47 |
except:
|
| 48 |
continue
|
|
|
|
| 19 |
tavily = TavilyClient(api_key=TAVILY_API_KEY)
|
| 20 |
|
| 21 |
# --- Helper Functions ---
|
| 22 |
+
import json
|
| 23 |
+
|
| 24 |
def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3500, temperature=0.7):
|
| 25 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
| 26 |
headers = {
|
|
|
|
| 36 |
"stream": True
|
| 37 |
}
|
| 38 |
with requests.post(url, headers=headers, json=data, stream=True) as response:
|
|
|
|
|
|
|
| 39 |
if line:
|
| 40 |
decoded = line.decode("utf-8")
|
| 41 |
if decoded.startswith("data: "):
|
| 42 |
piece = decoded.replace("data: ", "").strip()
|
| 43 |
if piece != "[DONE]":
|
| 44 |
try:
|
| 45 |
+
parsed = json.loads(piece)
|
| 46 |
+
delta = parsed.get("choices", [{}])[0].get("delta", {})
|
| 47 |
+
token = delta.get("content", "")
|
| 48 |
+
if token:
|
| 49 |
+
content += token
|
| 50 |
+
yield content
|
| 51 |
yield content
|
| 52 |
except:
|
| 53 |
continue
|