vickysrm commited on
Commit
d209323
·
1 Parent(s): 64ce06e

fix: Direct httpx Groq call - no package needed

Browse files
Files changed (1) hide show
  1. services/ollama_client.py +22 -28
services/ollama_client.py CHANGED
@@ -1,41 +1,35 @@
1
- import ollama
2
  import os
 
3
 
4
- # Default local model
5
  OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "qwen2:1.5b")
 
 
6
 
7
- # Groq client is initialized lazily to ensure env vars are loaded first
8
- _groq_client = None
9
 
10
- def _get_groq_client():
11
- global _groq_client
12
- if _groq_client is not None:
13
- return _groq_client
14
  api_key = os.getenv("GROQ_API_KEY")
15
- if not api_key:
16
- return None
17
- try:
18
- from groq import Groq
19
- _groq_client = Groq(api_key=api_key)
20
- print("Groq client initialized successfully.")
21
- return _groq_client
22
- except Exception as e:
23
- print(f"Groq init failed: {e}")
24
- return None
25
-
26
 
27
- def _call_llm(prompt: str) -> str:
28
- """Route to Groq if key is available, otherwise Ollama."""
29
- client = _get_groq_client()
30
- if client:
31
- chat_completion = client.chat.completions.create(
32
- messages=[{"role": "user", "content": prompt}],
33
- model="llama3-8b-8192",
34
- temperature=0.3,
 
 
 
 
 
 
35
  )
36
- return chat_completion.choices[0].message.content
 
37
  else:
38
  # Fallback to local Ollama
 
39
  response = ollama.chat(model=OLLAMA_MODEL, messages=[
40
  {'role': 'user', 'content': prompt}
41
  ])
 
 
1
  import os
2
+ import httpx
3
 
 
4
  OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "qwen2:1.5b")
5
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
6
+ GROQ_MODEL = "llama3-8b-8192"
7
 
 
 
8
 
9
+ def _call_llm(prompt: str) -> str:
10
+ """Call Groq API directly via HTTP. Falls back to local Ollama if no key."""
 
 
11
  api_key = os.getenv("GROQ_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ if api_key:
14
+ # Direct HTTP call to Groq no package needed, always works
15
+ response = httpx.post(
16
+ GROQ_API_URL,
17
+ headers={
18
+ "Authorization": f"Bearer {api_key}",
19
+ "Content-Type": "application/json",
20
+ },
21
+ json={
22
+ "model": GROQ_MODEL,
23
+ "messages": [{"role": "user", "content": prompt}],
24
+ "temperature": 0.3,
25
+ },
26
+ timeout=30.0,
27
  )
28
+ response.raise_for_status()
29
+ return response.json()["choices"][0]["message"]["content"]
30
  else:
31
  # Fallback to local Ollama
32
+ import ollama
33
  response = ollama.chat(model=OLLAMA_MODEL, messages=[
34
  {'role': 'user', 'content': prompt}
35
  ])