Spaces:
Paused
Paused
Update llm_clients.py
Browse files- llm_clients.py +114 -5
llm_clients.py
CHANGED
|
@@ -1,21 +1,130 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
| 6 |
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 9 |
gemini_model = genai.GenerativeModel("gemini-1.5-flash")
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
response = groq_client.chat.completions.create(
|
| 13 |
model="llama-3.1-8b-instant",
|
| 14 |
-
messages=
|
|
|
|
| 15 |
)
|
|
|
|
| 16 |
return response.choices[0].message.content
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
from groq import Groq
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
+
# ==========================
|
| 7 |
+
# Setup API Clients
|
| 8 |
+
# ==========================
|
| 9 |
+
|
| 10 |
groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 11 |
|
| 12 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 13 |
gemini_model = genai.GenerativeModel("gemini-1.5-flash")
|
| 14 |
|
| 15 |
+
|
| 16 |
+
# ==========================
|
| 17 |
+
# Llama (Groq)
|
| 18 |
+
# ==========================
|
| 19 |
+
|
| 20 |
+
def call_llama(messages, temperature=0.7):
|
| 21 |
+
"""
|
| 22 |
+
messages = [
|
| 23 |
+
{"role": "system", "content": "..."},
|
| 24 |
+
{"role": "user", "content": "..."}
|
| 25 |
+
]
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
response = groq_client.chat.completions.create(
|
| 29 |
model="llama-3.1-8b-instant",
|
| 30 |
+
messages=messages,
|
| 31 |
+
temperature=temperature
|
| 32 |
)
|
| 33 |
+
|
| 34 |
return response.choices[0].message.content
|
| 35 |
|
| 36 |
+
|
| 37 |
+
# ==========================
|
| 38 |
+
# Gemini
|
| 39 |
+
# ==========================
|
| 40 |
+
|
| 41 |
+
def call_gemini(messages, temperature=0.7):
|
| 42 |
+
"""
|
| 43 |
+
Gemini expects combined prompt.
|
| 44 |
+
We convert messages into a single string.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
formatted_prompt = ""
|
| 48 |
+
for m in messages:
|
| 49 |
+
formatted_prompt += f"{m['role'].upper()}: {m['content']}\n"
|
| 50 |
+
|
| 51 |
+
response = gemini_model.generate_content(
|
| 52 |
+
formatted_prompt,
|
| 53 |
+
generation_config={"temperature": temperature}
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
return response.text
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# ==========================
|
| 60 |
+
# AI Classifier
|
| 61 |
+
# ==========================
|
| 62 |
+
|
| 63 |
+
def classify_prompt(prompt):
|
| 64 |
+
|
| 65 |
+
system_prompt = """
|
| 66 |
+
You are an AI intent classifier.
|
| 67 |
+
|
| 68 |
+
Return ONLY valid JSON in this format:
|
| 69 |
+
|
| 70 |
+
{
|
| 71 |
+
"intent": "chat | reasoning | live_data | greeting",
|
| 72 |
+
"needs_search": true or false,
|
| 73 |
+
"complexity": "low | medium | high"
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
Rules:
|
| 77 |
+
- Logical, math, proof, step explanation -> reasoning
|
| 78 |
+
- Current events, weather, news, price, today -> live_data
|
| 79 |
+
- Hello/hi/how are you -> greeting
|
| 80 |
+
- Everything else -> chat
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
messages = [
|
| 84 |
+
{"role": "system", "content": system_prompt},
|
| 85 |
+
{"role": "user", "content": prompt}
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
raw = call_llama(messages, temperature=0)
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
return json.loads(raw)
|
| 92 |
+
except:
|
| 93 |
+
return {
|
| 94 |
+
"intent": "chat",
|
| 95 |
+
"needs_search": False,
|
| 96 |
+
"complexity": "low"
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# ==========================
|
| 101 |
+
# LLM Judge (for dual answers)
|
| 102 |
+
# ==========================
|
| 103 |
+
|
| 104 |
+
def judge_answers(answer1, answer2):
|
| 105 |
+
|
| 106 |
+
judge_prompt = f"""
|
| 107 |
+
You are an AI judge.
|
| 108 |
+
|
| 109 |
+
Choose which answer is better.
|
| 110 |
+
|
| 111 |
+
Reply ONLY with:
|
| 112 |
+
1 (if answer1 is better)
|
| 113 |
+
2 (if answer2 is better)
|
| 114 |
+
|
| 115 |
+
Answer 1:
|
| 116 |
+
{answer1}
|
| 117 |
+
|
| 118 |
+
Answer 2:
|
| 119 |
+
{answer2}
|
| 120 |
+
"""
|
| 121 |
+
|
| 122 |
+
result = call_llama(
|
| 123 |
+
[{"role": "user", "content": judge_prompt}],
|
| 124 |
+
temperature=0
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
if "2" in result:
|
| 128 |
+
return 2
|
| 129 |
+
return 1
|
| 130 |
|