Spaces:
Sleeping
Sleeping
File size: 11,095 Bytes
29fbb51 53001af 29fbb51 53001af 4d6298c 53001af 29fbb51 350fb44 4d6298c 29fbb51 350fb44 4d6298c 350fb44 53001af 29fbb51 53001af b0dd1a2 350fb44 29fbb51 350fb44 53001af 350fb44 53001af 350fb44 53001af 29fbb51 4d6298c 350fb44 29fbb51 53001af f0e36ad 53001af 29fbb51 350fb44 339f42a 82e746e 273204e 339f42a 350fb44 53001af 29fbb51 350fb44 53001af 29fbb51 350fb44 29fbb51 350fb44 29fbb51 339f42a 29fbb51 350fb44 29fbb51 350fb44 29fbb51 350fb44 29fbb51 53001af 29fbb51 350fb44 29fbb51 350fb44 b0dd1a2 7274b39 350fb44 53001af 350fb44 53001af 350fb44 53001af 29fbb51 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | import os
import chromadb
from dotenv import load_dotenv
from langchain_core.documents import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.llms import OpenAI
from langchain.chains.question_answering import load_qa_chain
from langchain_community.vectorstores import Chroma
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from config import Config
load_dotenv()
# ChromaDB configuration
CHROMA_HOST = Config.RAG_CHROMA_HOST
CHROMA_PORT = Config.RAG_CHROMA_PORT
COLLECTION_NAME = Config.RAG_COLLECTION_NAME
# LLM Provider Configuration
LLM_PROVIDER = Config.RAG_LLM_PROVIDER
LLM_API_KEY = Config.RAG_LLM_API_KEY
LLM_MODEL = Config.RAG_LLM_MODEL
LLM_TEMPERATURE = Config.RAG_LLM_TEMPERATURE
LLM_MAX_TOKENS = Config.RAG_LLM_MAX_TOKENS
# Provider-specific configurations
PROVIDER_CONFIGS = {
"openai": {
"api_base": "https://api.openai.com/v1",
"default_model": "gpt-3.5-turbo"
},
"groq": {
"api_base": "https://api.groq.com/openai/v1",
"default_model": "llama-3.3-70b-versatile"
},
"openrouter": {
"api_base": "https://openrouter.ai/api/v1",
"default_model": "mistralai/mistral-small-3.2-24b-instruct:free"
}
}
vector_store = None
company_qa_chain = None
query_router_chain = None
cybersecurity_chain = None
llm = None
def get_llm_config():
"""Get the appropriate LLM configuration based on the provider."""
if LLM_PROVIDER not in PROVIDER_CONFIGS:
raise ValueError(f"Unsupported LLM provider: {LLM_PROVIDER}. Supported: {list(PROVIDER_CONFIGS.keys())}")
config = PROVIDER_CONFIGS[LLM_PROVIDER].copy()
# Use provided model or fall back to default
model = LLM_MODEL if LLM_MODEL != "gpt-3.5-turbo" else config["default_model"]
return {
"model": model,
"openai_api_key": LLM_API_KEY,
"openai_api_base": config["api_base"],
"temperature": LLM_TEMPERATURE,
"max_tokens": LLM_MAX_TOKENS,
}
def initialize_llm():
"""Initialize the LLM based on the configured provider."""
if not LLM_API_KEY:
raise ValueError(f"LLM_API_KEY environment variable is required for {LLM_PROVIDER}")
config = get_llm_config()
print(f"Initializing {LLM_PROVIDER.upper()} with model: {config['model']}")
return ChatOpenAI(**config)
def initialize_pipelines():
"""Initializes all required models, chains, and the vector store."""
global vector_store, company_qa_chain, query_router_chain, cybersecurity_chain, llm
try:
# Initialize LLM
llm = initialize_llm()
# Initialize embeddings
embeddings = HuggingFaceEmbeddings(
model_name="all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'},
encode_kwargs={'normalize_embeddings': True}
)
# Initialize ChromaDB client
try:
chroma_client = chromadb.HttpClient(host=CHROMA_HOST, port=CHROMA_PORT)
chroma_client.heartbeat()
except Exception as e:
raise ConnectionError("Failed to connect to ChromaDB.") from e
# Initialize vector store
vector_store = Chroma(
client=chroma_client,
collection_name=COLLECTION_NAME,
embedding_function=embeddings,
)
# Query Router Chain
router_template = """You are a query classifier. Classify the following query into one of these categories:
- COMPANY: Questions about our company, its products, services, or general information
- CYBERSECURITY: Questions about cybersecurity, security threats, best practices, or vulnerabilities
- OFF_TOPIC: Questions that don't fit the above categories
Query: {query}
Respond with only the category name (COMPANY, CYBERSECURITY, or OFF_TOPIC):"""
router_prompt = PromptTemplate(
input_variables=["query"],
template=router_template
)
query_router_chain = LLMChain(
llm=llm,
prompt=router_prompt
)
# Custom Company QA Chain
company_qa_template = """You are a helpful assistant for CyberAlertNepal. Answer the following question about our company using the information provided and links if only available. Give a natural, direct and polite response.
Question: {question}
Information:
{context}
Answer:"""
company_qa_prompt = PromptTemplate(
input_variables=["question", "context"],
template=company_qa_template
)
company_qa_chain = LLMChain(
llm=llm,
prompt=company_qa_prompt
)
# Cybersecurity Chain
cybersecurity_template = """You are a cybersecurity professional. Answer the following question truthfully and concisely.
If you are not 100% sure about the answer, simply respond with: "I am not sure about the answer."
Do not add extra explanations or assumptions. Do not provide false or speculative information.
Question: {question}
Provide a comprehensive and accurate answer about cybersecurity:"""
cybersecurity_prompt = PromptTemplate(
input_variables=["question"],
template=cybersecurity_template
)
cybersecurity_chain = LLMChain(
llm=llm,
prompt=cybersecurity_prompt
)
print(f"Successfully initialized pipelines with {LLM_PROVIDER.upper()}")
except Exception as e:
print(f"Error initializing pipelines: {e}")
raise
def add_document_to_rag(text: str, metadata: dict):
"""Splits a document and adds it to the ChromaDB index."""
global vector_store
if not vector_store:
initialize_pipelines()
try:
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
docs = text_splitter.create_documents([text], metadatas=[metadata])
if not docs:
print("Document was empty after splitting, not adding to ChromaDB.")
return False
vector_store.add_documents(docs)
print("Successfully added documents.")
return True
except Exception as e:
print(f"Error adding document to RAG: {e}")
return False
def route_and_process_query(query: str):
"""Routes the query and processes it using the appropriate pipeline."""
global query_router_chain, vector_store, company_qa_chain, cybersecurity_chain
if not all([query_router_chain, vector_store, company_qa_chain, cybersecurity_chain]):
initialize_pipelines()
try:
# 1. Classify the query
route_result = query_router_chain.run(query)
route = route_result.strip().upper()
# 2. Route to appropriate logic
if "CYBERSECURITY" in route:
answer = cybersecurity_chain.run(question=query)
return {
"answer": answer,
"source": "Cybersecurity Knowledge Base",
"route": "CYBERSECURITY",
"provider": LLM_PROVIDER.upper(),
"model": get_llm_config()["model"]
}
elif "COMPANY" in route:
# Perform similarity search on ChromaDB
docs = vector_store.similarity_search(query, k=3)
if not docs:
return {
"answer": "I could not find any relevant information to answer your question.",
"source": "Company Documents",
"route": "COMPANY",
"provider": LLM_PROVIDER.upper(),
"model": get_llm_config()["model"]
}
# Combine document content for context
context = "\n\n".join([doc.page_content for doc in docs])
# Run the custom QA chain
answer = company_qa_chain.run(question=query, context=context)
sources = list(set([doc.metadata.get("source", "Unknown") for doc in docs]))
return {
"answer": answer,
"source": "Company Documents",
"documents": sources,
"route": "COMPANY",
"provider": LLM_PROVIDER.upper(),
"model": get_llm_config()["model"]
}
else: # OFF_TOPIC
return {
"answer": "I am a specialized assistant of CyberAlertNepal. I cannot answer questions outside of cybersecurity topics.",
"source": "N/A",
"route": "OFF_TOPIC",
"provider": LLM_PROVIDER.upper(),
"model": get_llm_config()["model"]
}
except Exception as e:
print(f"Error processing query: {e}")
return {
"answer": "I encountered an error while processing your query. Please try again.",
"source": "Error",
"route": None,
"documents": None,
"provider": LLM_PROVIDER.upper(),
"error": str(e)
}
def check_system_health():
"""Check if all components are properly initialized."""
try:
# Test ChromaDB connection
if vector_store:
vector_store._client.heartbeat()
# Test if all chains are initialized
components = {
"vector_store": vector_store is not None,
"company_qa_chain": company_qa_chain is not None,
"query_router_chain": query_router_chain is not None,
"cybersecurity_chain": cybersecurity_chain is not None,
"llm": llm is not None
}
return {
"status": "healthy" if all(components.values()) else "unhealthy",
"components": components,
"provider": LLM_PROVIDER.upper(),
"model": get_llm_config()["model"] if llm else "Not initialized"
}
except Exception as e:
return {
"status": "unhealthy",
"error": str(e),
"provider": LLM_PROVIDER.upper()
}
def test_llm_connection():
"""Test the LLM API connection."""
try:
if not llm:
initialize_pipelines()
# Simple test query
test_response = llm("Say 'Hello, LLM is working!'")
return {
"success": True,
"provider": LLM_PROVIDER.upper(),
"model": get_llm_config()["model"],
"response": str(test_response)
}
except Exception as e:
return {
"success": False,
"provider": LLM_PROVIDER.upper(),
"error": str(e)
}
# Initialize pipelines on module import
try:
initialize_pipelines()
except Exception as e:
print(f"Failed to initialize pipelines on startup: {e}") |