deveshc23 commited on
Commit
8cd81e0
·
verified ·
1 Parent(s): 24ae774

Upload quant_llm.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. quant_llm.py +28 -45
quant_llm.py CHANGED
@@ -1,76 +1,59 @@
1
  """
2
  QuantAgent OS - LLM Adapter Layer
3
  Provides unified multi-model intelligence:
4
- - Google Gemini (gemini-2.0-flash, gemini-1.5-flash via google.genai / google.generativeai)
5
  - Fallback Autonomous Expert Quantitative Rule-Engine (Zero-configuration out of the box)
6
  """
7
 
8
  import os
9
  import re
 
10
  import logging
 
11
  from typing import Optional, Dict, Any
12
 
13
  logger = logging.getLogger(__name__)
14
 
15
 
16
  class LLMAdapter:
17
- """Unified LLM client with intelligent fallback."""
18
 
19
  def __init__(self, api_key: Optional[str] = None, model_name: str = "gemini-2.0-flash"):
20
  self.api_key = api_key or os.environ.get("GEMINI_API_KEY")
21
  self.model_name = model_name
22
- self._client = None
23
- self._init_client()
24
-
25
- def _init_client(self):
26
- if not self.api_key:
27
- return
28
-
29
- # Try modern google.genai first
30
- try:
31
- from google import genai
32
- self._client = genai.Client(api_key=self.api_key)
33
- self._backend = "genai"
34
- logger.info("Initialized modern google.genai client.")
35
- return
36
- except Exception:
37
- pass
38
-
39
- # Try google.generativeai fallback
40
- try:
41
- import google.generativeai as gai
42
- gai.configure(api_key=self.api_key)
43
- self._client = gai.GenerativeModel(self.model_name)
44
- self._backend = "generativeai"
45
- logger.info("Initialized google.generativeai client.")
46
- return
47
- except Exception as e:
48
- logger.warning(f"Could not initialize Gemini SDK: {e}")
49
- self._client = None
50
- self._backend = "expert_heuristics"
51
 
52
  def update_key(self, api_key: str):
53
  self.api_key = api_key.strip()
54
- self._init_client()
55
 
56
  def is_live_llm(self) -> bool:
57
- return self._client is not None
58
 
59
  def generate(self, prompt: str, temperature: float = 0.3) -> str:
60
- """Generates text completion with automated fallback."""
61
- if self._client is not None:
62
  try:
63
- if self._backend == "genai":
64
- response = self._client.models.generate_content(
65
- model=self.model_name,
66
- contents=prompt,
67
- )
68
- return response.text
69
- elif self._backend == "generativeai":
70
- response = self._client.generate_content(prompt)
71
- return response.text
 
 
 
 
 
 
 
 
 
 
 
72
  except Exception as e:
73
- logger.error(f"Gemini API generation error: {e}. Falling back to internal engine.")
74
 
75
  # Autonomous Quant Expert Engine Fallback
76
  return self._autonomous_quant_fallback(prompt)
 
1
  """
2
  QuantAgent OS - LLM Adapter Layer
3
  Provides unified multi-model intelligence:
4
+ - Google Gemini (gemini-2.0-flash, gemini-1.5-flash via lightweight direct REST API)
5
  - Fallback Autonomous Expert Quantitative Rule-Engine (Zero-configuration out of the box)
6
  """
7
 
8
  import os
9
  import re
10
+ import json
11
  import logging
12
+ import requests
13
  from typing import Optional, Dict, Any
14
 
15
  logger = logging.getLogger(__name__)
16
 
17
 
18
  class LLMAdapter:
19
+ """Unified LLM client with direct REST connection and intelligent fallback."""
20
 
21
  def __init__(self, api_key: Optional[str] = None, model_name: str = "gemini-2.0-flash"):
22
  self.api_key = api_key or os.environ.get("GEMINI_API_KEY")
23
  self.model_name = model_name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  def update_key(self, api_key: str):
26
  self.api_key = api_key.strip()
 
27
 
28
  def is_live_llm(self) -> bool:
29
+ return bool(self.api_key)
30
 
31
  def generate(self, prompt: str, temperature: float = 0.3) -> str:
32
+ """Generates text completion via Google Gemini REST API with automated fallback."""
33
+ if self.api_key:
34
  try:
35
+ # Direct Google AI Studio REST endpoint (no heavy dependencies)
36
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/{self.model_name}:generateContent?key={self.api_key}"
37
+ headers = {"Content-Type": "application/json"}
38
+ payload = {
39
+ "contents": [{"parts": [{"text": prompt}]}],
40
+ "generationConfig": {
41
+ "temperature": temperature,
42
+ "maxOutputTokens": 2048
43
+ }
44
+ }
45
+ response = requests.post(url, headers=headers, json=payload, timeout=20)
46
+ if response.status_code == 200:
47
+ data = response.json()
48
+ candidates = data.get("candidates", [])
49
+ if candidates:
50
+ parts = candidates[0].get("content", {}).get("parts", [])
51
+ if parts:
52
+ return parts[0].get("text", "")
53
+ else:
54
+ logger.warning(f"Gemini API returned status {response.status_code}: {response.text}")
55
  except Exception as e:
56
+ logger.error(f"Gemini API REST error: {e}. Falling back to internal expert engine.")
57
 
58
  # Autonomous Quant Expert Engine Fallback
59
  return self._autonomous_quant_fallback(prompt)