rag-chatbot / app.py
Akilesh
system prompt
c6d2ec1
raw
history blame
3.56 kB
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
import os
import logging
from dotenv import load_dotenv
import gradio as gr
from langchain_community.chat_models import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Qdrant
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.prompts import PromptTemplate
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO)
# Custom system prompt
CUSTOM_SYSTEM_PROMPT = """
I am Akilesh, your assistant for question-answering tasks! I'll respond as if you're chatting directly with me, using a fun, polite, and casual tone. I'll use the provided context to answer your questions concisely in three sentences or less. If I don't know the answer, I'll simply let you know. Think of me as the digital version of myself, always ready to chat and share!
"""
# Create a PromptTemplate with the custom system prompt
system_template = SystemMessagePromptTemplate.from_template(
CUSTOM_SYSTEM_PROMPT)
human_template = HumanMessagePromptTemplate.from_template(
"Context: {context}\n\nQuestion: {question}")
prompt_template = ChatPromptTemplate.from_messages([
system_template,
human_template
])
# Initialize the ChatOpenAI model
llm = ChatOpenAI(model_name="gpt-4o", temperature=0.1)
# Load and process documents
loader = TextLoader("about_me.txt")
documents = loader.load()
# Split documents into manageable chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
# Initialize embeddings
embeddings = OpenAIEmbeddings()
# Initialize Qdrant vector store
vector_store = Qdrant.from_documents(
documents=docs,
embedding=embeddings,
url=os.getenv("QDRANT_URL"),
api_key=os.getenv("QDRANT_API_KEY"),
collection_name="akilesh_docs"
)
# Create retriever from the vector store
retriever = vector_store.as_retriever()
# Create the RetrievalQA chain with the custom prompt
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
chain_type_kwargs={"prompt": prompt_template}
)
# Function to generate a response
def respond(message: str) -> str:
try:
# Generate the response using the LLM
result = qa_chain.invoke({"query": message})
response = result.get('result')
if not response:
raise ValueError("Empty response from LLM")
return response
except Exception as e:
logging.error(f"Error generating response: {e}")
return "I apologize, but I'm having trouble answering that question right now. Could you try rephrasing or asking something else?"
# Function to generate an answer and log the interaction
def generate_answer(message: str, history: list) -> str:
new_response = respond(message)
# Log the interaction
logging.info(f"User: {message}")
logging.info(f"Assistant: {new_response}")
return new_response
# Set up the Gradio chat interface
demo = gr.ChatInterface(
fn=generate_answer,
title="RAG App | Learn More About Me!",
multimodal=False,
retry_btn=None,
undo_btn=None,
clear_btn=None,
examples=[
"What is your name?",
"What do you like to do for fun?",
"Tell me a fun fact about yourself."
]
)
# Launch the Gradio app
demo.launch()