Spaces:
Sleeping
Sleeping
Update src/llm_client.py
Browse files- src/llm_client.py +8 -46
src/llm_client.py
CHANGED
|
@@ -1,72 +1,34 @@
|
|
| 1 |
-
import requests
|
| 2 |
import os
|
| 3 |
from google import genai
|
| 4 |
-
from google.genai import types
|
| 5 |
-
from .prompts import get_system_prompt
|
| 6 |
|
| 7 |
def ask_llm(query, context, mode="Executive Summary", model_provider="Gemini"):
|
| 8 |
-
"""
|
| 9 |
-
Switchable Brain: Defaults to Gemini (Powerful), falls back to Granite (Private).
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
# 1. Get the Persona/Prompt
|
| 13 |
system_instruction = get_system_prompt(mode)
|
| 14 |
|
| 15 |
-
# --- OPTION A: GOOGLE GEMINI (New SDK) ---
|
| 16 |
if model_provider == "Gemini":
|
| 17 |
try:
|
| 18 |
google_key = os.environ.get("GOOGLE_API_KEY")
|
| 19 |
if not google_key:
|
| 20 |
-
return "Error: GOOGLE_API_KEY not found
|
| 21 |
|
| 22 |
-
#
|
|
|
|
| 23 |
client = genai.Client(api_key=google_key)
|
| 24 |
|
| 25 |
-
# Construct the prompt
|
| 26 |
full_prompt = f"USER QUERY: {query}\n\nDOCUMENT CONTEXT:\n{context[:30000]}"
|
| 27 |
|
| 28 |
-
#
|
| 29 |
response = client.models.generate_content(
|
| 30 |
-
model='gemini-2.0-flash',
|
| 31 |
contents=full_prompt,
|
| 32 |
config=types.GenerateContentConfig(
|
| 33 |
system_instruction=system_instruction,
|
| 34 |
max_output_tokens=1000,
|
| 35 |
-
temperature=0.3
|
| 36 |
-
)
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# The response object structure is slightly different now
|
| 40 |
return response.text
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
return f"Gemini Error: {str(e)}"
|
| 44 |
-
|
| 45 |
-
# --- OPTION B: GRANITE / LOCAL SPACE (Unchanged) ---
|
| 46 |
-
else:
|
| 47 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 48 |
-
if not hf_token:
|
| 49 |
-
return "Error: HF_TOKEN is missing."
|
| 50 |
-
|
| 51 |
-
api_url = "https://navydevildoc-private-granite.hf.space/generate"
|
| 52 |
-
|
| 53 |
-
payload = {
|
| 54 |
-
"text": f"USER QUESTION: {query}\n\nDOCUMENT CONTEXT:\n{context[:6000]}",
|
| 55 |
-
"persona": system_instruction,
|
| 56 |
-
"model": "granite4:latest",
|
| 57 |
-
"max_tokens": 1024
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
headers = {
|
| 61 |
-
"Authorization": f"Bearer {hf_token}",
|
| 62 |
-
"Content-Type": "application/json"
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
response = requests.post(api_url, json=payload, headers=headers, timeout=120)
|
| 67 |
-
if response.status_code == 200:
|
| 68 |
-
return response.json().get("response", "Error: Empty response.")
|
| 69 |
-
else:
|
| 70 |
-
return f"Error {response.status_code}: {response.text}"
|
| 71 |
-
except Exception as e:
|
| 72 |
-
return f"Connection Error: {str(e)}"
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from google import genai
|
| 3 |
+
from google.genai import types
|
|
|
|
| 4 |
|
| 5 |
def ask_llm(query, context, mode="Executive Summary", model_provider="Gemini"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
system_instruction = get_system_prompt(mode)
|
| 7 |
|
|
|
|
| 8 |
if model_provider == "Gemini":
|
| 9 |
try:
|
| 10 |
google_key = os.environ.get("GOOGLE_API_KEY")
|
| 11 |
if not google_key:
|
| 12 |
+
return "Error: GOOGLE_API_KEY not found."
|
| 13 |
|
| 14 |
+
# NO genai.configure() here.
|
| 15 |
+
# The API key goes directly into the Client.
|
| 16 |
client = genai.Client(api_key=google_key)
|
| 17 |
|
|
|
|
| 18 |
full_prompt = f"USER QUERY: {query}\n\nDOCUMENT CONTEXT:\n{context[:30000]}"
|
| 19 |
|
| 20 |
+
# Using Gemini 2.0 Flash
|
| 21 |
response = client.models.generate_content(
|
| 22 |
+
model='gemini-2.0-flash',
|
| 23 |
contents=full_prompt,
|
| 24 |
config=types.GenerateContentConfig(
|
| 25 |
system_instruction=system_instruction,
|
| 26 |
max_output_tokens=1000,
|
| 27 |
+
temperature=0.3,
|
| 28 |
+
),
|
| 29 |
)
|
| 30 |
|
|
|
|
| 31 |
return response.text
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
return f"Gemini Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|