NavyDevilDoc commited on
Commit
260d73e
·
verified ·
1 Parent(s): 4efb9fa

Update src/llm_client.py

Browse files
Files changed (1) hide show
  1. src/llm_client.py +49 -7
src/llm_client.py CHANGED
@@ -1,34 +1,76 @@
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)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  from google import genai
3
  from google.genai import types
4
+ from .prompts import get_system_prompt # <--- Essential import
5
 
6
  def ask_llm(query, context, mode="Executive Summary", model_provider="Gemini"):
7
+ """
8
+ Switchable Brain: Defaults to Gemini (Powerful), falls back to Granite (Private).
9
+ """
10
+
11
+ # 1. Get the Persona/Prompt
12
  system_instruction = get_system_prompt(mode)
13
 
14
+ # --- OPTION A: GOOGLE GEMINI (New SDK) ---
15
  if model_provider == "Gemini":
16
  try:
17
  google_key = os.environ.get("GOOGLE_API_KEY")
18
  if not google_key:
19
+ return "Error: GOOGLE_API_KEY not found in Secrets."
20
 
21
+ # DEBUG PRINT: Verify we are running the new code
22
+ print("DEBUG: Initializing new Gemini Client...")
23
+
24
+ # NEW SYNTAX: Instantiate a Client object directly
25
+ # (No genai.configure() calls allowed here!)
26
  client = genai.Client(api_key=google_key)
27
 
28
+ # Construct the prompt
29
  full_prompt = f"USER QUERY: {query}\n\nDOCUMENT CONTEXT:\n{context[:30000]}"
30
 
31
+ # NEW SYNTAX: Call generate_content via the 'models' attribute
32
  response = client.models.generate_content(
33
+ model='gemini-2.0-flash',
34
  contents=full_prompt,
35
  config=types.GenerateContentConfig(
36
  system_instruction=system_instruction,
37
  max_output_tokens=1000,
38
+ temperature=0.3
39
+ )
40
  )
41
 
42
  return response.text
43
 
44
  except Exception as e:
45
+ print(f"DEBUG ERROR: {e}")
46
  return f"Gemini Error: {str(e)}"
47
+
48
+ # --- OPTION B: GRANITE / LOCAL SPACE (Private Option) ---
49
+ else:
50
+ import requests # Imported locally to keep dependencies clean if needed
51
+ hf_token = os.environ.get("HF_TOKEN")
52
+ if not hf_token:
53
+ return "Error: HF_TOKEN is missing."
54
+
55
+ api_url = "https://navydevildoc-private-granite.hf.space/generate"
56
+
57
+ payload = {
58
+ "text": f"USER QUESTION: {query}\n\nDOCUMENT CONTEXT:\n{context[:6000]}",
59
+ "persona": system_instruction,
60
+ "model": "granite4:latest",
61
+ "max_tokens": 1024
62
+ }
63
+
64
+ headers = {
65
+ "Authorization": f"Bearer {hf_token}",
66
+ "Content-Type": "application/json"
67
+ }
68
+
69
+ try:
70
+ response = requests.post(api_url, json=payload, headers=headers, timeout=120)
71
+ if response.status_code == 200:
72
+ return response.json().get("response", "Error: Empty response.")
73
+ else:
74
+ return f"Error {response.status_code}: {response.text}"
75
+ except Exception as e:
76
+ return f"Connection Error: {str(e)}"