Spaces:
Sleeping
Sleeping
Update src/llm_client.py
Browse files- src/llm_client.py +39 -0
src/llm_client.py
CHANGED
|
@@ -1,5 +1,44 @@
|
|
| 1 |
import requests
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def ask_granite(query, context):
|
| 5 |
"""
|
|
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
import google.generativeai as genai
|
| 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 from our new library
|
| 13 |
+
system_instruction = get_system_prompt(mode)
|
| 14 |
+
|
| 15 |
+
# --- OPTION A: GOOGLE GEMINI (The "Big Gun") ---
|
| 16 |
+
if model_provider == "Gemini":
|
| 17 |
+
try:
|
| 18 |
+
# RETRIEVE SECRET AT RUNTIME
|
| 19 |
+
# In HF Spaces, this pulls directly from the "Secrets" tab
|
| 20 |
+
google_key = os.environ.get("GOOGLE_API_KEY")
|
| 21 |
+
|
| 22 |
+
if not google_key:
|
| 23 |
+
return "Error: GOOGLE_API_KEY not found in Secrets. Please add it in Settings > Secrets."
|
| 24 |
+
|
| 25 |
+
# Configure specifically for this request (safe and stateless)
|
| 26 |
+
genai.configure(api_key=google_key)
|
| 27 |
+
|
| 28 |
+
# Use 'gemini-1.5-flash' for speed/cost, or 'gemini-1.5-pro' for complex reasoning
|
| 29 |
+
model = genai.GenerativeModel(
|
| 30 |
+
model_name='gemini-1.5-flash',
|
| 31 |
+
system_instruction=system_instruction
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Gemini has a massive context window (1M+ tokens), so we can be generous
|
| 35 |
+
full_prompt = f"USER QUERY: {query}\n\nDOCUMENT CONTEXT:\n{context[:30000]}"
|
| 36 |
+
|
| 37 |
+
response = model.generate_content(full_prompt)
|
| 38 |
+
return response.text
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Gemini Error: {str(e)}"
|
| 42 |
|
| 43 |
def ask_granite(query, context):
|
| 44 |
"""
|