Chaitu2112 commited on
Commit
2e4d7aa
·
verified ·
1 Parent(s): b799e5e

Update llama_api.py

Browse files
Files changed (1) hide show
  1. llama_api.py +106 -73
llama_api.py CHANGED
@@ -1,92 +1,125 @@
1
- import os
2
- import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from dotenv import load_dotenv
 
 
4
 
 
5
  load_dotenv()
6
 
7
- OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
 
 
 
8
 
9
- # You can change this to any valid OpenRouter model ID
10
- # Avoid the ":free" suffix unless you're sure it's valid.
11
- # Example options (depending on your account/plan):
12
- # "meta-llama/llama-3.1-8b-instruct"
13
- # "meta-llama/llama-3.1-70b-instruct"
14
- MODEL_NAME = os.getenv(
15
- "OPENROUTER_MODEL",
16
- "meta-llama/llama-3.3-70b-instruct:free", # safer default
17
  )
18
 
19
 
20
- def ask_ollama(prompt: str) -> str:
 
21
  """
22
- Call OpenRouter chat completion API and return the assistant's message text.
23
- If there's an error or no choices, return a readable error message and log details.
24
  """
25
- if not OPENROUTER_API_KEY:
26
- return (
27
- "⚠️ The language model is not configured. "
28
- "OPENROUTER_API_KEY is missing on the server."
 
 
 
 
 
 
 
 
29
  )
30
 
31
- url = "https://openrouter.ai/api/v1/chat/completions"
32
- headers = {
33
- "Authorization": f"Bearer {OPENROUTER_API_KEY}",
34
- "Content-Type": "application/json",
35
- # Optional but recommended
36
- "HTTP-Referer": "https://icfai-chatbot.local",
37
- "X-Title": "ICFAI Digital MBA Assistant",
38
- }
39
-
40
- payload = {
41
- "model": MODEL_NAME,
42
- "messages": [
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(f" Network error calling OpenRouter: {e}")
56
- return f"⚠️ Error contacting the language model: {e}"
 
57
 
58
- print(f"🔹 OpenRouter status: {resp.status_code}")
59
- # Always print the raw text once if something looks off
 
 
 
 
60
  try:
61
- data = resp.json()
62
- except Exception as e:
63
- print(f"❌ Failed to parse JSON from OpenRouter: {e}")
64
- print("Raw response text:", resp.text[:500])
65
- return "⚠️ Received an invalid response from the language model."
66
-
67
- # If OpenRouter returned an error object
68
- if "error" in data:
69
- print("⚠️ OpenRouter error payload:", data["error"])
70
- msg = data["error"].get("message") or str(data["error"])
71
- return f"⚠️ LLM error from OpenRouter: {msg}"
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
- try:
84
- content = choices[0]["message"]["content"]
 
 
 
 
 
 
 
 
 
85
  except Exception as e:
86
- print(f"⚠️ Unexpected response structure: {e}")
87
- print("Full payload:", data)
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