Spaces:
Sleeping
Sleeping
File size: 9,363 Bytes
9dc639f 74221f2 9dc639f 1eb0002 293661c 9dc639f 1eb0002 293661c 53b33ac fc48f50 db87ae8 293661c db87ae8 78bd826 c947e4c 1eb0002 726773c e8182c5 864c041 74221f2 1eb0002 74221f2 1eb0002 74221f2 1eb0002 54fafa1 1eb0002 b0739e4 1eb0002 db87ae8 19fdb92 1eb0002 19fdb92 1eb0002 19fdb92 1eb0002 0aef3aa 53b33ac 1eb0002 53b33ac 1eb0002 53b33ac 1eb0002 53b33ac 1eb0002 b0739e4 1eb0002 a684f83 1eb0002 19fdb92 e27c8c7 19fdb92 1eb0002 9dc639f 19fdb92 1eb0002 19fdb92 1eb0002 74221f2 1eb0002 449be2c 1eb0002 449be2c |
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 |
import os
import getpass
import spacy
import pandas as pd
from typing import Optional, List, Dict, Any
import subprocess
from langchain.llms.base import LLM
from langchain.docstore.document import Document
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from smolagents import CodeAgent, DuckDuckGoSearchTool, ManagedAgent, LiteLLMModel
from pydantic import BaseModel, Field, ValidationError, validator
from mistralai import Mistral
from langchain.prompts import PromptTemplate
# Import chains and tools
from classification_chain import get_classification_chain
from cleaner_chain import get_cleaner_chain
from refusal_chain import get_refusal_chain
from tailor_chain import get_tailor_chain
from prompts import classification_prompt, refusal_prompt, tailor_prompt
# Initialize Mistral API client
mistral_api_key = os.environ.get("MISTRAL_API_KEY")
client = Mistral(api_key=mistral_api_key)
# Initialize LiteLLM model for web search
pydantic_agent = LiteLLMModel(model_id="gemini/gemini-pro", api_key=os.environ.get("GEMINI_API_KEY"))
# Pydantic models for validation and type safety
class QueryInput(BaseModel):
query: str = Field(..., min_length=1, description="The input query string")
@validator('query')
def check_query_is_string(cls, v):
if not isinstance(v, str):
raise ValueError("Query must be a valid string")
if v.strip() == "":
raise ValueError("Query cannot be empty or just whitespace")
return v.strip()
class ModerationResult(BaseModel):
is_safe: bool = Field(..., description="Whether the content is safe")
categories: Dict[str, bool] = Field(default_factory=dict, description="Detected content categories")
original_text: str = Field(..., description="The original input text")
# Load spaCy model for NER
def install_spacy_model():
try:
spacy.load("en_core_web_sm")
print("spaCy model 'en_core_web_sm' is already installed.")
except OSError:
print("Downloading spaCy model 'en_core_web_sm'...")
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"], check=True)
print("spaCy model 'en_core_web_sm' downloaded successfully.")
install_spacy_model()
nlp = spacy.load("en_core_web_sm")
def extract_main_topic(query: str) -> str:
try:
query_input = QueryInput(query=query)
doc = nlp(query_input.query)
main_topic = None
for ent in doc.ents:
if ent.label_ in ["ORG", "PRODUCT", "PERSON", "GPE", "TIME"]:
main_topic = ent.text
break
if not main_topic:
for token in doc:
if token.pos_ in ["NOUN", "PROPN"]:
main_topic = token.text
break
return main_topic if main_topic else "this topic"
except Exception as e:
print(f"Error extracting main topic: {e}")
return "this topic"
def moderate_text(query: str) -> ModerationResult:
try:
query_input = QueryInput(query=query)
response = client.classifiers.moderate_chat(
model="mistral-moderation-latest",
inputs=[{"role": "user", "content": query_input.query}]
)
is_safe = True
categories = {}
if hasattr(response, 'results') and response.results:
categories = {
"violence": response.results[0].categories.get("violence_and_threats", False),
"hate": response.results[0].categories.get("hate_and_discrimination", False),
"dangerous": response.results[0].categories.get("dangerous_and_criminal_content", False),
"selfharm": response.results[0].categories.get("selfharm", False)
}
is_safe = not any(categories.values())
return ModerationResult(
is_safe=is_safe,
categories=categories,
original_text=query_input.query
)
except ValidationError as e:
raise ValueError(f"Input validation failed: {str(e)}")
except Exception as e:
raise RuntimeError(f"Moderation failed: {str(e)}")
def classify_query(query: str) -> str:
try:
query_input = QueryInput(query=query)
wellness_keywords = ["box breathing", "meditation", "yoga", "mindfulness", "breathing exercises"]
if any(keyword in query_input.query.lower() for keyword in wellness_keywords):
return "Wellness"
class_result = classification_chain.invoke({"query": query_input.query})
classification = class_result.get("text", "").strip()
return classification if classification != "" else "OutOfScope"
except ValidationError as e:
raise ValueError(f"Classification input validation failed: {str(e)}")
except Exception as e:
raise RuntimeError(f"Classification failed: {str(e)}")
def build_or_load_vectorstore(csv_path: str, store_dir: str) -> FAISS:
try:
if os.path.exists(store_dir):
print(f"Loading existing FAISS store from '{store_dir}'")
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1")
return FAISS.load_local(store_dir, embeddings)
print(f"Building new FAISS store from CSV: {csv_path}")
df = pd.read_csv(csv_path)
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
df.columns = df.columns.str.strip()
docs = [
Document(page_content=str(row["Answers"]), metadata={"question": str(row["Question"])})
for _, row in df.iterrows()
]
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1")
vectorstore = FAISS.from_documents(docs, embedding=embeddings)
vectorstore.save_local(store_dir)
return vectorstore
except Exception as e:
raise RuntimeError(f"Error building/loading vector store: {str(e)}")
def build_rag_chain(llm_model: LiteLLMModel, vectorstore: FAISS) -> RetrievalQA:
class GeminiLangChainLLM(LLM):
def _call(self, prompt: str, stop: Optional[list] = None, **kwargs) -> str:
messages = [{"role": "user", "content": prompt}]
return llm_model(messages, stop_sequences=stop)
@property
def _llm_type(self) -> str:
return "custom_gemini"
try:
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 3})
gemini_as_llm = GeminiLangChainLLM()
return RetrievalQA.from_chain_type(
llm=gemini_as_llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True
)
except Exception as e:
raise RuntimeError(f"Error building RAG chain: {str(e)}")
def run_pipeline(query: str) -> str:
try:
query = sanitize_message(query)
moderation_result = moderate_text(query)
if not moderation_result.is_safe:
return "Sorry, this query contains harmful or inappropriate content."
classification = classify_query(moderation_result.original_text)
if classification == "OutOfScope":
refusal_text = refusal_chain.run({"topic": "this topic"})
return tailor_chain.run({"response": refusal_text}).strip()
if classification == "Wellness":
rag_result = wellness_rag_chain({"query": moderation_result.original_text})
csv_answer = rag_result["result"].strip()
web_answer = "" if csv_answer else do_web_search(moderation_result.original_text)
final_merged = merge_responses(csv_answer, web_answer)
return tailor_chain.run({"response": final_merged}).strip()
if classification == "Brand":
rag_result = brand_rag_chain({"query": moderation_result.original_text})
csv_answer = rag_result["result"].strip()
final_merged = merge_responses(csv_answer, "")
return tailor_chain.run({"response": final_merged}).strip()
refusal_text = refusal_chain.run({"topic": "this topic"})
return tailor_chain.run({"response": refusal_text}).strip()
# Initialize chains and vectorstores
try:
classification_chain = get_classification_chain()
refusal_chain = get_refusal_chain()
tailor_chain = get_tailor_chain()
cleaner_chain = get_cleaner_chain()
wellness_csv = "AIChatbot.csv"
brand_csv = "BrandAI.csv"
wellness_store_dir = "faiss_wellness_store"
brand_store_dir = "faiss_brand_store"
wellness_vectorstore = build_or_load_vectorstore(wellness_csv, wellness_store_dir)
brand_vectorstore = build_or_load_vectorstore(brand_csv, brand_store_dir)
gemini_llm = LiteLLMModel(model_id="gemini/gemini-pro", api_key=os.environ.get("GEMINI_API_KEY"))
wellness_rag_chain = build_rag_chain(gemini_llm, wellness_vectorstore)
brand_rag_chain = build_rag_chain(gemini_llm, brand_vectorstore)
print("Pipeline initialized successfully!")
except Exception as e:
print(f"Error initializing pipeline: {str(e)}")
def run_with_chain(query: str) -> str:
return run_pipeline(query)
|