Spaces:
Build error
Build error
Update pipeline.py
Browse files- pipeline.py +64 -33
pipeline.py
CHANGED
|
@@ -20,7 +20,7 @@ except LookupError:
|
|
| 20 |
english_words = set(words.words())
|
| 21 |
|
| 22 |
# LangChain / Groq / LLM imports
|
| 23 |
-
from langchain_groq import ChatGroq
|
| 24 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 25 |
from langchain_community.vectorstores import FAISS
|
| 26 |
from langchain.chains import RetrievalQA, LLMChain
|
|
@@ -29,8 +29,10 @@ 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
|
|
|
|
| 33 |
# Custom chain imports
|
|
|
|
| 34 |
from classification_chain import get_classification_chain
|
| 35 |
from refusal_chain import get_refusal_chain
|
| 36 |
from tailor_chain import get_tailor_chain
|
|
@@ -41,7 +43,7 @@ from tailor_chain_wellnessBrand import get_tailor_chain_wellnessBrand
|
|
| 41 |
from mistralai import Mistral
|
| 42 |
|
| 43 |
# Google Gemini LLM
|
| 44 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 45 |
|
| 46 |
# Web search
|
| 47 |
# from smolagents import DuckDuckGoSearchTool, ManagedAgent, HfApiModel, CodeAgent
|
|
@@ -191,44 +193,73 @@ CACHE_SIZE_LIMIT = 1000
|
|
| 191 |
# logger.error(f"Failed to initialize ChatGroq: {e}")
|
| 192 |
# raise RuntimeError("ChatGroq initialization failed.") from e
|
| 193 |
|
| 194 |
-
|
| 195 |
-
from langchain_core.callbacks import CallbackManager
|
| 196 |
-
from langchain_core.callbacks.base import BaseCallbackHandler
|
| 197 |
-
from langchain_core.caches import BaseCache
|
| 198 |
-
|
| 199 |
-
# Initialize ChatGroq with the callback manager
|
| 200 |
try:
|
| 201 |
-
|
| 202 |
-
class NoCache(BaseCache):
|
| 203 |
-
"""Simple no-op cache implementation."""
|
| 204 |
-
def __init__(self):
|
| 205 |
-
pass
|
| 206 |
-
|
| 207 |
-
def lookup(self, prompt, llm_string):
|
| 208 |
-
return None
|
| 209 |
-
|
| 210 |
-
def update(self, prompt, llm_string, return_val):
|
| 211 |
-
pass
|
| 212 |
-
|
| 213 |
-
def clear(self):
|
| 214 |
-
pass
|
| 215 |
-
|
| 216 |
-
# Call model_rebuild() before initializing
|
| 217 |
-
ChatGroq.model_rebuild()
|
| 218 |
-
|
| 219 |
-
# Then initialize the model
|
| 220 |
fallback_groq_api_key = os.environ.get("GROQ_API_KEY_FALLBACK", os.environ.get("GROQ_API_KEY"))
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
model=GROQ_MODELS["default"],
|
| 223 |
temperature=0.7,
|
| 224 |
groq_api_key=fallback_groq_api_key,
|
| 225 |
-
max_tokens=2048
|
| 226 |
-
callback_manager=CallbackManager([]) # Use CallbackManager instead of raw list
|
| 227 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
|
| 234 |
# -------------------------------------------------------
|
|
|
|
| 20 |
english_words = set(words.words())
|
| 21 |
|
| 22 |
# LangChain / Groq / LLM imports
|
| 23 |
+
# from langchain_groq import ChatGroq
|
| 24 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 25 |
from langchain_community.vectorstores import FAISS
|
| 26 |
from langchain.chains import RetrievalQA, LLMChain
|
|
|
|
| 29 |
|
| 30 |
# from langchain.callbacks.base import BaseCallbacks # Updated import
|
| 31 |
# from langchain.callbacks.manager import CallbackManager
|
| 32 |
+
# from langchain.callbacks import StdOutCallbackHandler
|
| 33 |
+
|
| 34 |
# Custom chain imports
|
| 35 |
+
from groq_client import GroqClient
|
| 36 |
from classification_chain import get_classification_chain
|
| 37 |
from refusal_chain import get_refusal_chain
|
| 38 |
from tailor_chain import get_tailor_chain
|
|
|
|
| 43 |
from mistralai import Mistral
|
| 44 |
|
| 45 |
# Google Gemini LLM
|
| 46 |
+
# from langchain_google_genai import ChatGoogleGenerativeAI
|
| 47 |
|
| 48 |
# Web search
|
| 49 |
# from smolagents import DuckDuckGoSearchTool, ManagedAgent, HfApiModel, CodeAgent
|
|
|
|
| 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 |
+
return None
|
| 203 |
+
|
| 204 |
+
# Initialize the Groq client as fallback
|
| 205 |
+
groq_fallback_llm = GroqClient(
|
| 206 |
model=GROQ_MODELS["default"],
|
| 207 |
temperature=0.7,
|
| 208 |
groq_api_key=fallback_groq_api_key,
|
| 209 |
+
max_tokens=2048
|
|
|
|
| 210 |
)
|
| 211 |
+
|
| 212 |
+
# Verify connection by checking models (optional)
|
| 213 |
+
try:
|
| 214 |
+
groq_fallback_llm.list_models()
|
| 215 |
+
logger.info("Fallback Groq LLM initialized successfully")
|
| 216 |
+
except Exception as e:
|
| 217 |
+
logger.error(f"Fallback Groq LLM connection test failed: {e}")
|
| 218 |
+
return None
|
| 219 |
+
|
| 220 |
+
|
| 221 |
|
| 222 |
except Exception as e:
|
| 223 |
+
logger.error(f"Failed to initialize fallback Groq LLM: {e}")
|
| 224 |
+
return None
|
| 225 |
+
|
| 226 |
+
# from langchain_core.callbacks import CallbackManager
|
| 227 |
+
# from langchain_core.callbacks.base import BaseCallbackHandler
|
| 228 |
+
# from langchain_core.caches import BaseCache
|
| 229 |
+
|
| 230 |
+
# # Initialize ChatGroq with the callback manager
|
| 231 |
+
# try:
|
| 232 |
+
# # First, ensure BaseCache is defined (if needed)
|
| 233 |
+
# class NoCache(BaseCache):
|
| 234 |
+
# """Simple no-op cache implementation."""
|
| 235 |
+
# def __init__(self):
|
| 236 |
+
# pass
|
| 237 |
+
|
| 238 |
+
# def lookup(self, prompt, llm_string):
|
| 239 |
+
# return None
|
| 240 |
+
|
| 241 |
+
# def update(self, prompt, llm_string, return_val):
|
| 242 |
+
# pass
|
| 243 |
+
|
| 244 |
+
# def clear(self):
|
| 245 |
+
# pass
|
| 246 |
+
|
| 247 |
+
# # Call model_rebuild() before initializing
|
| 248 |
+
# ChatGroq.model_rebuild()
|
| 249 |
+
|
| 250 |
+
# # Then initialize the model
|
| 251 |
+
# fallback_groq_api_key = os.environ.get("GROQ_API_KEY_FALLBACK", os.environ.get("GROQ_API_KEY"))
|
| 252 |
+
# groq_fallback_llm = ChatGroq(
|
| 253 |
+
# model=GROQ_MODELS["default"],
|
| 254 |
+
# temperature=0.7,
|
| 255 |
+
# groq_api_key=fallback_groq_api_key,
|
| 256 |
+
# max_tokens=2048,
|
| 257 |
+
# callback_manager=CallbackManager([]) # Use CallbackManager instead of raw list
|
| 258 |
+
# )
|
| 259 |
+
|
| 260 |
+
# except Exception as e:
|
| 261 |
+
# logger.error(f"Failed to initialize ChatGroq: {e}")
|
| 262 |
+
# raise RuntimeError("ChatGroq initialization failed.") from e
|
| 263 |
|
| 264 |
|
| 265 |
# -------------------------------------------------------
|