Spaces:
Build error
Build error
Tried
Browse files- pipeline.py +20 -53
pipeline.py
CHANGED
|
@@ -26,7 +26,7 @@ from langchain_community.vectorstores import FAISS
|
|
| 26 |
from langchain.chains import RetrievalQA, LLMChain
|
| 27 |
from langchain.prompts import PromptTemplate
|
| 28 |
from langchain.docstore.document import Document
|
| 29 |
-
|
| 30 |
# from langchain.callbacks.base import BaseCallbacks # Updated import
|
| 31 |
# from langchain.callbacks.manager import CallbackManager
|
| 32 |
# from langchain.callbacks import StdOutCallbackHandler
|
|
@@ -192,69 +192,36 @@ CACHE_SIZE_LIMIT = 1000
|
|
| 192 |
# except Exception as e:
|
| 193 |
# logger.error(f"Failed to initialize ChatGroq: {e}")
|
| 194 |
# raise RuntimeError("ChatGroq initialization failed.") from e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
try:
|
| 197 |
-
# Get API key from environment variables
|
| 198 |
fallback_groq_api_key = os.environ.get("GROQ_API_KEY_FALLBACK", os.environ.get("GROQ_API_KEY"))
|
| 199 |
-
|
| 200 |
if not fallback_groq_api_key:
|
| 201 |
logger.warning("No Groq API key found for fallback LLM")
|
| 202 |
-
|
| 203 |
-
# Initialize the Groq client as fallback
|
| 204 |
groq_fallback_llm = ChatGroq(
|
| 205 |
model=GROQ_MODELS["default"],
|
| 206 |
temperature=0.7,
|
| 207 |
groq_api_key=fallback_groq_api_key,
|
| 208 |
-
max_tokens=2048
|
|
|
|
| 209 |
)
|
| 210 |
-
|
| 211 |
-
# # Verify connection by checking models (optional)
|
| 212 |
-
# try:
|
| 213 |
-
# groq_fallback_llm.list_models()
|
| 214 |
-
# logger.info("Fallback Groq LLM initialized successfully")
|
| 215 |
-
# except Exception as e:
|
| 216 |
-
# logger.error(f"Fallback Groq LLM connection test failed: {e}")
|
| 217 |
except Exception as e:
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
# from langchain_core.callbacks import CallbackManager
|
| 221 |
-
# from langchain_core.callbacks.base import BaseCallbackHandler
|
| 222 |
-
# from langchain_core.caches import BaseCache
|
| 223 |
-
|
| 224 |
-
# # Initialize ChatGroq with the callback manager
|
| 225 |
-
# try:
|
| 226 |
-
# # First, ensure BaseCache is defined (if needed)
|
| 227 |
-
# class NoCache(BaseCache):
|
| 228 |
-
# """Simple no-op cache implementation."""
|
| 229 |
-
# def __init__(self):
|
| 230 |
-
# pass
|
| 231 |
-
|
| 232 |
-
# def lookup(self, prompt, llm_string):
|
| 233 |
-
# return None
|
| 234 |
-
|
| 235 |
-
# def update(self, prompt, llm_string, return_val):
|
| 236 |
-
# pass
|
| 237 |
-
|
| 238 |
-
# def clear(self):
|
| 239 |
-
# pass
|
| 240 |
-
|
| 241 |
-
# # Call model_rebuild() before initializing
|
| 242 |
-
# ChatGroq.model_rebuild()
|
| 243 |
-
|
| 244 |
-
# # Then initialize the model
|
| 245 |
-
# fallback_groq_api_key = os.environ.get("GROQ_API_KEY_FALLBACK", os.environ.get("GROQ_API_KEY"))
|
| 246 |
-
# groq_fallback_llm = ChatGroq(
|
| 247 |
-
# model=GROQ_MODELS["default"],
|
| 248 |
-
# temperature=0.7,
|
| 249 |
-
# groq_api_key=fallback_groq_api_key,
|
| 250 |
-
# max_tokens=2048,
|
| 251 |
-
# callback_manager=CallbackManager([]) # Use CallbackManager instead of raw list
|
| 252 |
-
# )
|
| 253 |
-
|
| 254 |
-
# except Exception as e:
|
| 255 |
-
# logger.error(f"Failed to initialize ChatGroq: {e}")
|
| 256 |
-
# raise RuntimeError("ChatGroq initialization failed.") from e
|
| 257 |
-
|
| 258 |
|
| 259 |
# -------------------------------------------------------
|
| 260 |
# Rate-limit & Cache
|
|
|
|
| 26 |
from langchain.chains import RetrievalQA, LLMChain
|
| 27 |
from langchain.prompts import PromptTemplate
|
| 28 |
from langchain.docstore.document import Document
|
| 29 |
+
from langchain_core.caches import BaseCache
|
| 30 |
# from langchain.callbacks.base import BaseCallbacks # Updated import
|
| 31 |
# from langchain.callbacks.manager import CallbackManager
|
| 32 |
# from langchain.callbacks import StdOutCallbackHandler
|
|
|
|
| 192 |
# except Exception as e:
|
| 193 |
# logger.error(f"Failed to initialize ChatGroq: {e}")
|
| 194 |
# raise RuntimeError("ChatGroq initialization failed.") from e
|
| 195 |
+
# Define a simple no-op cache class
|
| 196 |
+
class NoCache(BaseCache):
|
| 197 |
+
"""Simple no-op cache implementation."""
|
| 198 |
+
def __init__(self):
|
| 199 |
+
pass
|
| 200 |
+
|
| 201 |
+
def lookup(self, prompt, llm_string):
|
| 202 |
+
return None
|
| 203 |
+
|
| 204 |
+
def update(self, prompt, llm_string, return_val):
|
| 205 |
+
pass
|
| 206 |
|
| 207 |
+
def clear(self):
|
| 208 |
+
pass
|
| 209 |
+
|
| 210 |
+
# Initialize ChatGroq with cache
|
| 211 |
try:
|
|
|
|
| 212 |
fallback_groq_api_key = os.environ.get("GROQ_API_KEY_FALLBACK", os.environ.get("GROQ_API_KEY"))
|
|
|
|
| 213 |
if not fallback_groq_api_key:
|
| 214 |
logger.warning("No Groq API key found for fallback LLM")
|
|
|
|
|
|
|
| 215 |
groq_fallback_llm = ChatGroq(
|
| 216 |
model=GROQ_MODELS["default"],
|
| 217 |
temperature=0.7,
|
| 218 |
groq_api_key=fallback_groq_api_key,
|
| 219 |
+
max_tokens=2048,
|
| 220 |
+
cache=NoCache() # Set cache explicitly
|
| 221 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
except Exception as e:
|
| 223 |
+
logger.error(f"Failed to initialize fallback Groq LLM: {e}")
|
| 224 |
+
raise RuntimeError("ChatGroq initialization failed.") from e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# -------------------------------------------------------
|
| 227 |
# Rate-limit & Cache
|