Spaces:
Paused
Paused
Update llm_client.py
Browse files- llm_client.py +27 -7
llm_client.py
CHANGED
|
@@ -77,9 +77,9 @@ class HybridLLM(LLM):
|
|
| 77 |
try:
|
| 78 |
print(f"๐ Calling Colab API: {self.api_url}")
|
| 79 |
response = requests.post(
|
| 80 |
-
f"{self.api_url}/
|
| 81 |
json={"prompt": prompt, "max_tokens": 512},
|
| 82 |
-
timeout=
|
| 83 |
)
|
| 84 |
if response.status_code == 200:
|
| 85 |
return response.json()["response"]
|
|
@@ -125,13 +125,33 @@ class LLMClient:
|
|
| 125 |
self.api_url = os.environ.get("COLAB_API_URL", "")
|
| 126 |
self.server_process = None
|
| 127 |
self.server_port = 8080
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
# Setup Local Fallback
|
| 130 |
try:
|
| 131 |
-
#
|
| 132 |
self.server_bin, self.lib_path = setup_llama_binaries()
|
| 133 |
|
| 134 |
-
#
|
| 135 |
print("๐ Loading Local Qwen3-0.6B (GGUF)...")
|
| 136 |
model_repo = "Qwen/Qwen3-0.6B-GGUF"
|
| 137 |
filename = "Qwen3-0.6B-Q8_0.gguf"
|
|
@@ -142,13 +162,13 @@ class LLMClient:
|
|
| 142 |
)
|
| 143 |
print(f"โ
Model downloaded to: {self.model_path}")
|
| 144 |
|
| 145 |
-
#
|
| 146 |
self.start_local_server()
|
| 147 |
|
| 148 |
except Exception as e:
|
| 149 |
print(f"โ ๏ธ Could not setup local fallback: {e}")
|
| 150 |
|
| 151 |
-
# Create Hybrid LangChain Wrapper
|
| 152 |
self.llm = HybridLLM(
|
| 153 |
api_url=self.api_url,
|
| 154 |
local_server_url=f"http://localhost:{self.server_port}"
|
|
|
|
| 77 |
try:
|
| 78 |
print(f"๐ Calling Colab API: {self.api_url}")
|
| 79 |
response = requests.post(
|
| 80 |
+
f"{self.api_url}/generate",
|
| 81 |
json={"prompt": prompt, "max_tokens": 512},
|
| 82 |
+
timeout=30
|
| 83 |
)
|
| 84 |
if response.status_code == 200:
|
| 85 |
return response.json()["response"]
|
|
|
|
| 125 |
self.api_url = os.environ.get("COLAB_API_URL", "")
|
| 126 |
self.server_process = None
|
| 127 |
self.server_port = 8080
|
| 128 |
+
|
| 129 |
+
# 1. Setup Groq (Highest Priority)
|
| 130 |
+
self.groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 131 |
+
self.groq_model = "qwen-2.5-32b" # Default, user can override via env if needed
|
| 132 |
+
|
| 133 |
+
if self.groq_api_key:
|
| 134 |
+
try:
|
| 135 |
+
from langchain_groq import ChatGroq
|
| 136 |
+
print(f"โก Initializing Groq API with model: {self.groq_model}...")
|
| 137 |
+
self.llm = ChatGroq(
|
| 138 |
+
temperature=0.3,
|
| 139 |
+
model=self.groq_model,
|
| 140 |
+
groq_api_key=self.groq_api_key,
|
| 141 |
+
max_tokens=1024,
|
| 142 |
+
model_kwargs={"stop": ["<|im_end|>", "Input:", "Context:"]}
|
| 143 |
+
)
|
| 144 |
+
print("โ
Groq API ready! Skipping local setup.")
|
| 145 |
+
return # Exit early, no need for local server
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"โ ๏ธ Groq Init Failed: {e}")
|
| 148 |
|
| 149 |
+
# 2. Setup Local Fallback (Only if Groq fails/missing)
|
| 150 |
try:
|
| 151 |
+
# Setup Binary
|
| 152 |
self.server_bin, self.lib_path = setup_llama_binaries()
|
| 153 |
|
| 154 |
+
# Download Model (Qwen3-0.6B)
|
| 155 |
print("๐ Loading Local Qwen3-0.6B (GGUF)...")
|
| 156 |
model_repo = "Qwen/Qwen3-0.6B-GGUF"
|
| 157 |
filename = "Qwen3-0.6B-Q8_0.gguf"
|
|
|
|
| 162 |
)
|
| 163 |
print(f"โ
Model downloaded to: {self.model_path}")
|
| 164 |
|
| 165 |
+
# Start Server
|
| 166 |
self.start_local_server()
|
| 167 |
|
| 168 |
except Exception as e:
|
| 169 |
print(f"โ ๏ธ Could not setup local fallback: {e}")
|
| 170 |
|
| 171 |
+
# Create Hybrid LangChain Wrapper (Fallback)
|
| 172 |
self.llm = HybridLLM(
|
| 173 |
api_url=self.api_url,
|
| 174 |
local_server_url=f"http://localhost:{self.server_port}"
|