Spaces:
Sleeping
Sleeping
Update llama_api.py
Browse files- llama_api.py +106 -73
llama_api.py
CHANGED
|
@@ -1,92 +1,125 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
load_dotenv()
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
MODEL_NAME = os.getenv(
|
| 15 |
-
"OPENROUTER_MODEL",
|
| 16 |
-
"meta-llama/llama-3.3-70b-instruct:free", # safer default
|
| 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 |
-
"role": "user",
|
| 45 |
-
"content": prompt,
|
| 46 |
-
}
|
| 47 |
-
],
|
| 48 |
-
"temperature": 0.2,
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
print(f"🚀 Sending request to OpenRouter model: {MODEL_NAME}")
|
| 52 |
-
try:
|
| 53 |
-
resp = requests.post(url, json=payload, headers=headers, timeout=60)
|
| 54 |
except Exception as e:
|
| 55 |
-
print(
|
| 56 |
-
return f"
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
choices = data.get("choices")
|
| 74 |
-
if not choices:
|
| 75 |
-
# <== THIS is your current situation
|
| 76 |
-
print("⚠️ No choices returned from OpenRouter. Full payload:")
|
| 77 |
-
print(data)
|
| 78 |
-
return (
|
| 79 |
-
"⚠️ I couldn't generate a response from the language model. "
|
| 80 |
-
"Please try again in a moment."
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
-
print(
|
| 87 |
-
|
| 88 |
-
return (
|
| 89 |
-
"⚠️ I received an unexpected response structure from the language model."
|
| 90 |
-
)
|
| 91 |
|
| 92 |
-
return content.strip()
|
|
|
|
| 1 |
+
# import ollama
|
| 2 |
+
|
| 3 |
+
# # Synchronous ask (kept for caching or non-stream calls)
|
| 4 |
+
# def ask_ollama(prompt: str, model_name: str = "llama3"):
|
| 5 |
+
# response = ollama.chat(
|
| 6 |
+
# model=model_name,
|
| 7 |
+
# messages=[
|
| 8 |
+
# {"role": "system", "content": "You are a helpful assistant for college queries."},
|
| 9 |
+
# {"role": "user", "content": prompt}
|
| 10 |
+
# ]
|
| 11 |
+
# )
|
| 12 |
+
# return response.get("message", {}).get("content", "")
|
| 13 |
+
|
| 14 |
+
# # Streaming generator: yields incremental text chunks
|
| 15 |
+
# def ask_ollama_stream(prompt: str, model_name: str = "llama3"):
|
| 16 |
+
# stream = ollama.chat(
|
| 17 |
+
# model=model_name,
|
| 18 |
+
# messages=[
|
| 19 |
+
# {"role": "system", "content": "You are a helpful assistant for college queries."},
|
| 20 |
+
# {"role": "user", "content": prompt}
|
| 21 |
+
# ],
|
| 22 |
+
# stream=True
|
| 23 |
+
# )
|
| 24 |
+
# buffer = ""
|
| 25 |
+
# for chunk in stream:
|
| 26 |
+
# # chunk may contain partial content; combine
|
| 27 |
+
# text = chunk.get("message", {}).get("content", "")
|
| 28 |
+
# if text:
|
| 29 |
+
# # yield incremental text (could be full or partial)
|
| 30 |
+
# yield text
|
| 31 |
+
|
| 32 |
from dotenv import load_dotenv
|
| 33 |
+
import os
|
| 34 |
+
from openai import OpenAI
|
| 35 |
|
| 36 |
+
# Load environment variables
|
| 37 |
load_dotenv()
|
| 38 |
|
| 39 |
+
OPENROUTER_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 40 |
+
|
| 41 |
+
if not OPENROUTER_KEY:
|
| 42 |
+
raise ValueError(" Missing OPENROUTER_API_KEY in .env")
|
| 43 |
|
| 44 |
+
print("Loaded key prefix:", OPENROUTER_KEY[:15])
|
| 45 |
+
|
| 46 |
+
client = OpenAI(
|
| 47 |
+
base_url="https://openrouter.ai/api/v1",
|
| 48 |
+
api_key=OPENROUTER_KEY,
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
|
| 52 |
+
|
| 53 |
+
def ask_ollama(prompt: str, model_name: str = "meta-llama/llama-3.3-70b-instruct:free"):
|
| 54 |
"""
|
| 55 |
+
Sends a prompt to OpenRouter (Llama-3.3-70B-Instruct) and returns the response text.
|
| 56 |
+
Handles missing fields, errors, and empty responses gracefully.
|
| 57 |
"""
|
| 58 |
+
try:
|
| 59 |
+
print(f"🚀 Sending request to OpenRouter model: {model_name}")
|
| 60 |
+
completion = client.chat.completions.create(
|
| 61 |
+
extra_headers={
|
| 62 |
+
"HTTP-Referer": "https://ifheindia.org",
|
| 63 |
+
"X-Title": "IFHE Chatbot",
|
| 64 |
+
},
|
| 65 |
+
model=model_name,
|
| 66 |
+
messages=[
|
| 67 |
+
{"role": "system", "content": "You are a helpful academic assistant for IFHE University. Quote only factual content from context."},
|
| 68 |
+
{"role": "user", "content": prompt},
|
| 69 |
+
],
|
| 70 |
)
|
| 71 |
|
| 72 |
+
|
| 73 |
+
if not hasattr(completion, "choices") or not completion.choices:
|
| 74 |
+
print(" No choices returned from OpenRouter.")
|
| 75 |
+
return " No valid response received from the model."
|
| 76 |
+
|
| 77 |
+
message = getattr(completion.choices[0].message, "content", None)
|
| 78 |
+
if not message or not message.strip():
|
| 79 |
+
print(" Empty message content in completion.")
|
| 80 |
+
return " The model did not return any text."
|
| 81 |
+
|
| 82 |
+
print(" Model raw response:", message[:250])
|
| 83 |
+
return message.strip()
|
| 84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
+
print(" OpenRouter / Llama API Error:", e)
|
| 87 |
+
return f" Error communicating with the model: {e}"
|
| 88 |
+
|
| 89 |
|
| 90 |
+
|
| 91 |
+
def ask_ollama_stream(prompt: str, model_name: str = "meta-llama/llama-3.3-70b-instruct:free"):
|
| 92 |
+
"""
|
| 93 |
+
Streams response token-by-token for real-time output.
|
| 94 |
+
Includes detailed logging for debugging.
|
| 95 |
+
"""
|
| 96 |
try:
|
| 97 |
+
print(f" Connecting to OpenRouter model: {model_name}")
|
| 98 |
+
stream = client.chat.completions.create(
|
| 99 |
+
extra_headers={
|
| 100 |
+
"HTTP-Referer": "https://ifheindia.org",
|
| 101 |
+
"X-Title": "IFHE Chatbot",
|
| 102 |
+
},
|
| 103 |
+
model=model_name,
|
| 104 |
+
messages=[
|
| 105 |
+
{"role": "system", "content": "You are a helpful academic assistant for IFHE University."},
|
| 106 |
+
{"role": "user", "content": prompt},
|
| 107 |
+
],
|
| 108 |
+
stream=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
+
for chunk in stream:
|
| 112 |
+
# Log structure of each chunk
|
| 113 |
+
print(f" Chunk received: {chunk}")
|
| 114 |
+
if hasattr(chunk.choices[0].delta, "content"):
|
| 115 |
+
text = chunk.choices[0].delta.content
|
| 116 |
+
if text:
|
| 117 |
+
print(f" Token: {text!r}")
|
| 118 |
+
yield text
|
| 119 |
+
|
| 120 |
+
print(" Streaming complete.")
|
| 121 |
+
|
| 122 |
except Exception as e:
|
| 123 |
+
print(" Streaming error (inside llama_api):", e)
|
| 124 |
+
yield f" Error while streaming: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 125 |
|
|
|