Spaces:
Paused
Paused
Update llm_client.py
Browse files- llm_client.py +33 -16
llm_client.py
CHANGED
|
@@ -64,6 +64,7 @@ def setup_llama_binaries():
|
|
| 64 |
|
| 65 |
# --- Custom LangChain LLM Wrapper for Hybrid Approach ---
|
| 66 |
class HybridLLM(LLM):
|
|
|
|
| 67 |
api_url: str = ""
|
| 68 |
local_server_url: str = "http://localhost:8080"
|
| 69 |
|
|
@@ -72,7 +73,18 @@ class HybridLLM(LLM):
|
|
| 72 |
return "hybrid_llm"
|
| 73 |
|
| 74 |
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs: Any) -> str:
|
| 75 |
-
# 1. Try
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if self.api_url:
|
| 77 |
try:
|
| 78 |
print(f"🌐 Calling Colab API: {self.api_url}")
|
|
@@ -88,13 +100,13 @@ class HybridLLM(LLM):
|
|
| 88 |
except Exception as e:
|
| 89 |
print(f"⚠️ API Connection Failed: {e}")
|
| 90 |
|
| 91 |
-
#
|
| 92 |
print("💻 Using Local llama-server Fallback...")
|
| 93 |
try:
|
| 94 |
# OpenAI-compatible completion endpoint
|
| 95 |
payload = {
|
| 96 |
"prompt": prompt,
|
| 97 |
-
"n_predict":
|
| 98 |
"temperature": 0.3,
|
| 99 |
"stop": (stop or []) + ["<|im_end|>", "Input:", "Context:"]
|
| 100 |
}
|
|
@@ -114,7 +126,11 @@ class HybridLLM(LLM):
|
|
| 114 |
|
| 115 |
@property
|
| 116 |
def _identifying_params(self) -> Mapping[str, Any]:
|
| 117 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
class LLMClient:
|
| 120 |
def __init__(self, vector_store=None):
|
|
@@ -125,28 +141,28 @@ class LLMClient:
|
|
| 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
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
-
if
|
| 134 |
try:
|
| 135 |
from langchain_groq import ChatGroq
|
| 136 |
-
print(f"⚡ Initializing Groq
|
| 137 |
-
self.
|
| 138 |
temperature=0.3,
|
| 139 |
-
model=
|
| 140 |
-
groq_api_key=
|
| 141 |
max_tokens=1024,
|
| 142 |
model_kwargs={"stop": ["<|im_end|>", "Input:", "Context:"]}
|
| 143 |
)
|
| 144 |
-
print("✅ Groq
|
| 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 (
|
| 150 |
try:
|
| 151 |
# Setup Binary
|
| 152 |
self.server_bin, self.lib_path = setup_llama_binaries()
|
|
@@ -168,8 +184,9 @@ class LLMClient:
|
|
| 168 |
except Exception as e:
|
| 169 |
print(f"⚠️ Could not setup local fallback: {e}")
|
| 170 |
|
| 171 |
-
# Create Hybrid LangChain Wrapper
|
| 172 |
self.llm = HybridLLM(
|
|
|
|
| 173 |
api_url=self.api_url,
|
| 174 |
local_server_url=f"http://localhost:{self.server_port}"
|
| 175 |
)
|
|
|
|
| 64 |
|
| 65 |
# --- Custom LangChain LLM Wrapper for Hybrid Approach ---
|
| 66 |
class HybridLLM(LLM):
|
| 67 |
+
groq_client: Any = None
|
| 68 |
api_url: str = ""
|
| 69 |
local_server_url: str = "http://localhost:8080"
|
| 70 |
|
|
|
|
| 73 |
return "hybrid_llm"
|
| 74 |
|
| 75 |
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs: Any) -> str:
|
| 76 |
+
# 1. Try Groq API (Highest Priority)
|
| 77 |
+
if self.groq_client:
|
| 78 |
+
try:
|
| 79 |
+
print("⚡ Using Groq API...")
|
| 80 |
+
# ChatGroq returns a message object, we need the content string
|
| 81 |
+
response = self.groq_client.invoke(prompt)
|
| 82 |
+
return response.content
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"⚠️ Groq API Failed: {e}")
|
| 85 |
+
# Continue to next fallback
|
| 86 |
+
|
| 87 |
+
# 2. Try Colab API
|
| 88 |
if self.api_url:
|
| 89 |
try:
|
| 90 |
print(f"🌐 Calling Colab API: {self.api_url}")
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
print(f"⚠️ API Connection Failed: {e}")
|
| 102 |
|
| 103 |
+
# 3. Fallback to Local Server
|
| 104 |
print("💻 Using Local llama-server Fallback...")
|
| 105 |
try:
|
| 106 |
# OpenAI-compatible completion endpoint
|
| 107 |
payload = {
|
| 108 |
"prompt": prompt,
|
| 109 |
+
"n_predict": 1024,
|
| 110 |
"temperature": 0.3,
|
| 111 |
"stop": (stop or []) + ["<|im_end|>", "Input:", "Context:"]
|
| 112 |
}
|
|
|
|
| 126 |
|
| 127 |
@property
|
| 128 |
def _identifying_params(self) -> Mapping[str, Any]:
|
| 129 |
+
return {
|
| 130 |
+
"groq_enabled": self.groq_client is not None,
|
| 131 |
+
"api_url": self.api_url,
|
| 132 |
+
"local_server_url": self.local_server_url
|
| 133 |
+
}
|
| 134 |
|
| 135 |
class LLMClient:
|
| 136 |
def __init__(self, vector_store=None):
|
|
|
|
| 141 |
self.api_url = os.environ.get("COLAB_API_URL", "")
|
| 142 |
self.server_process = None
|
| 143 |
self.server_port = 8080
|
| 144 |
+
self.groq_client = None
|
| 145 |
|
| 146 |
+
# 1. Setup Groq Client
|
| 147 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 148 |
+
groq_model = "qwen/qwen3-32b"
|
| 149 |
|
| 150 |
+
if groq_api_key:
|
| 151 |
try:
|
| 152 |
from langchain_groq import ChatGroq
|
| 153 |
+
print(f"⚡ Initializing Groq Client ({groq_model})...")
|
| 154 |
+
self.groq_client = ChatGroq(
|
| 155 |
temperature=0.3,
|
| 156 |
+
model=groq_model,
|
| 157 |
+
groq_api_key=groq_api_key,
|
| 158 |
max_tokens=1024,
|
| 159 |
model_kwargs={"stop": ["<|im_end|>", "Input:", "Context:"]}
|
| 160 |
)
|
| 161 |
+
print("✅ Groq Client ready.")
|
|
|
|
| 162 |
except Exception as e:
|
| 163 |
print(f"⚠️ Groq Init Failed: {e}")
|
| 164 |
|
| 165 |
+
# 2. Setup Local Fallback (Always setup as requested)
|
| 166 |
try:
|
| 167 |
# Setup Binary
|
| 168 |
self.server_bin, self.lib_path = setup_llama_binaries()
|
|
|
|
| 184 |
except Exception as e:
|
| 185 |
print(f"⚠️ Could not setup local fallback: {e}")
|
| 186 |
|
| 187 |
+
# Create Hybrid LangChain Wrapper
|
| 188 |
self.llm = HybridLLM(
|
| 189 |
+
groq_client=self.groq_client,
|
| 190 |
api_url=self.api_url,
|
| 191 |
local_server_url=f"http://localhost:{self.server_port}"
|
| 192 |
)
|