testchatbot / chat_logic.py
mujeebanwar's picture
Upload 7 files
8602315 verified
Raw History Blame Contribute Delete
3.28 kB
# chat_logic.py
import os
from operator import itemgetter
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
EMBEDDING_MODEL = "text-embedding-3-small"
def get_vector_store(chatbot_uuid: str, openai_api_key: str):
"""
Loads the vector store using the server-side OpenAI API key fetched from Redis.
"""
persist_directory = f"/data/chroma_db_store/{chatbot_uuid}"
if not os.path.exists(persist_directory):
return None
embeddings = OpenAIEmbeddings(model=EMBEDDING_MODEL, openai_api_key=openai_api_key)
vector_store = Chroma(collection_name=str(chatbot_uuid), persist_directory=persist_directory, embedding_function=embeddings)
return vector_store
def get_llm(provider: str, model: str, api_key: str):
"""
Dynamically selects the language model using the single API key from the request.
"""
if provider.lower() == "openai":
return ChatOpenAI(model=model, temperature=0.2, openai_api_key=api_key)
elif provider.lower() == "anthropic":
return ChatAnthropic(model=model, temperature=0.2, anthropic_api_key=api_key)
else:
raise ValueError(f"Unsupported API provider: {provider}")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
def format_chat_history(chat_history: list[dict]) -> str:
if not chat_history: return "No previous conversation history."
formatted_history = []
for message in chat_history:
role = "User" if message.get("role") == "user" else "Assistant"
formatted_history.append(f"{role}: {message.get('content')}")
return "\n".join(formatted_history)
def create_rag_chain_with_history(vector_store, llm, behavior: str):
"""
Creates the full RAG chain, now including a dynamic behavior and new fallback message.
"""
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 4})
# --- FIX: New prompt with behavior and fallback message ---
prompt_template = """
{behavior}
Your primary goal is to answer the user's question based on the provided context.
Synthesize the information from the CHAT HISTORY and the CONTEXT to provide a comprehensive answer.
If the context contains information that is relevant to the user's question, use it to form an answer.
If the context does not contain relevant information to answer the question, you must say "I don't know about it." Do not make up information.
CHAT HISTORY:
{chat_history}
CONTEXT:
{context}
USER'S QUESTION:
{question}
"""
prompt = ChatPromptTemplate.from_template(prompt_template)
rag_chain = (
{
"context": itemgetter("question") | retriever | format_docs,
"question": itemgetter("question"),
"chat_history": itemgetter("chat_history"),
"behavior": itemgetter("behavior")
}
| prompt
| llm
| StrOutputParser()
)
return rag_chain