chatbot / app.py
tdecae's picture
Update app.py
f4d77f2 verified
raw
history blame
9.52 kB
# import os
# import sys
# import requests
# from langchain.chains import ConversationalRetrievalChain
# from langchain.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader
# from langchain.text_splitter import CharacterTextSplitter
# from langchain.vectorstores import Chroma
# from langchain.embeddings import HuggingFaceEmbeddings
# from langchain.llms.base import LLM
# import gradio as gr
# # workaround for sqlite in HF spaces
# __import__('pysqlite3')
# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
# # πŸ“„ Load documents
# docs = []
# for f in os.listdir("multiple_docs"):
# if f.endswith(".pdf"):
# loader = PyPDFLoader(os.path.join("multiple_docs", f))
# docs.extend(loader.load())
# elif f.endswith(".docx") or f.endswith(".doc"):
# loader = Docx2txtLoader(os.path.join("multiple_docs", f))
# docs.extend(loader.load())
# elif f.endswith(".txt"):
# loader = TextLoader(os.path.join("multiple_docs", f))
# docs.extend(loader.load())
# # πŸ”— Split into chunks
# splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
# docs = splitter.split_documents(docs)
# texts = [doc.page_content for doc in docs]
# metadatas = [{"id": i} for i in range(len(texts))]
# # 🧠 Embeddings
# embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# # πŸ—ƒοΈ Vectorstore
# vectorstore = Chroma(
# persist_directory="./db",
# embedding_function=embedding_function
# )
# vectorstore.add_texts(texts=texts, metadatas=metadatas)
# vectorstore.persist()
# # πŸ” Get DeepSeek API key from env
# DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
# if DEEPSEEK_API_KEY is None:
# raise ValueError("DEEPSEEK_API_KEY environment variable is not set.")
# # 🌟 DeepSeek API endpoint
# DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
# # πŸ”· Wrap DeepSeek API into LangChain LLM
# class DeepSeekLLM(LLM):
# """LLM that queries DeepSeek's API."""
# api_key: str = DEEPSEEK_API_KEY
# def _call(self, prompt, stop=None, run_manager=None, **kwargs):
# headers = {
# "Authorization": f"Bearer {self.api_key}",
# "Content-Type": "application/json"
# }
# payload = {
# "model": "deepseek-chat", # adjust if you have a specific model name
# "messages": [
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": prompt}
# ],
# "temperature": 0.7,
# "max_tokens": 512
# }
# response = requests.post(DEEPSEEK_API_URL, headers=headers, json=payload)
# response.raise_for_status()
# data = response.json()
# return data["choices"][0]["message"]["content"].strip()
# @property
# def _llm_type(self) -> str:
# return "deepseek_api"
# llm = DeepSeekLLM()
# # πŸ”— Conversational chain
# chain = ConversationalRetrievalChain.from_llm(
# llm,
# retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
# return_source_documents=True,
# verbose=False
# )
# # πŸ’¬ Gradio UI
# chat_history = []
# with gr.Blocks() as demo:
# chatbot = gr.Chatbot(
# [("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my experience, where I'm eligible to work, skills etc you can chat with me directly in multiple languages")],
# avatar_images=["./multiple_docs/Guest.jpg", "./multiple_docs/Thierry Picture.jpg"]
# )
# msg = gr.Textbox(placeholder="Type your question here...")
# clear = gr.Button("Clear")
# def user(query, chat_history):
# chat_history_tuples = [(m[0], m[1]) for m in chat_history]
# result = chain({"question": query, "chat_history": chat_history_tuples})
# chat_history.append((query, result["answer"]))
# return gr.update(value=""), chat_history
# msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
# clear.click(lambda: None, None, chatbot, queue=False)
# demo.launch(debug=True) # remove share=True if running in HF Spaces
import os
import sys
import requests
from langchain.chains import ConversationalRetrievalChain, LLMChain
from langchain.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms.base import LLM
from langchain.prompts import PromptTemplate
from langchain.chains.question_answering import load_qa_chain
import gradio as gr
# workaround for sqlite in HF spaces
__import__('pysqlite3')
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
# πŸ“„ Load documents
docs = []
for f in os.listdir("multiple_docs"):
if f.endswith(".pdf"):
loader = PyPDFLoader(os.path.join("multiple_docs", f))
docs.extend(loader.load())
elif f.endswith(".docx") or f.endswith(".doc"):
loader = Docx2txtLoader(os.path.join("multiple_docs", f))
docs.extend(loader.load())
elif f.endswith(".txt"):
loader = TextLoader(os.path.join("multiple_docs", f))
docs.extend(loader.load())
# πŸ”— Split into chunks
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
docs = splitter.split_documents(docs)
texts = [doc.page_content for doc in docs]
metadatas = [{"id": i} for i in range(len(texts))]
# 🧠 Embeddings
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# πŸ—ƒοΈ Vectorstore
vectorstore = Chroma(
persist_directory="./db",
embedding_function=embedding_function
)
vectorstore.add_texts(texts=texts, metadatas=metadatas)
vectorstore.persist()
# πŸ” Get DeepSeek API key from env
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
if DEEPSEEK_API_KEY is None:
raise ValueError("DEEPSEEK_API_KEY environment variable is not set.")
# 🌟 DeepSeek API endpoint
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
# πŸ”· Wrap DeepSeek API into LangChain LLM
class DeepSeekLLM(LLM):
"""LLM that queries DeepSeek's API."""
api_key: str = DEEPSEEK_API_KEY
def _call(self, prompt, stop=None, run_manager=None, **kwargs):
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
payload = {
"model": "deepseek-chat", # adjust if you have a specific model name
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
"temperature": 0.7,
"max_tokens": 512
}
response = requests.post(DEEPSEEK_API_URL, headers=headers, json=payload)
response.raise_for_status()
data = response.json()
return data["choices"][0]["message"]["content"].strip()
@property
def _llm_type(self) -> str:
return "deepseek_api"
llm = DeepSeekLLM()
# ✨ Custom prompt template
template = """
You are Thierry Decae's chatbot. Your role is to answer questions about his career, experience, availability β€” in other words
any recruitment-related question.
Use the following context to answer the user's question as fully and accurately as possible.
If you don't know the answer, say "I'm not sure about that."
Always answer as if you were Thierry Decae β€” do not refer to him as 'he', use 'I' instead.
Context:
{context}
Question: {question}
Answer:
"""
prompt = PromptTemplate(
input_variables=["context", "question"],
template=template,
)
# πŸ”— QA chain with custom prompt
qa_chain = load_qa_chain(llm, chain_type="stuff", prompt=prompt)
# πŸ”· Question rephraser chain for follow-up questions β†’ standalone
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(
"""
Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:
"""
)
question_generator = LLMChain(
llm=llm,
prompt=CONDENSE_QUESTION_PROMPT
)
# πŸ”· Finally: build the ConversationalRetrievalChain manually
chain = ConversationalRetrievalChain(
retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
question_generator=question_generator,
combine_docs_chain=qa_chain,
return_source_documents=True,
verbose=False
)
# πŸ’¬ Gradio UI
chat_history = []
with gr.Blocks() as demo:
chatbot = gr.Chatbot(
[("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my experience, where I'm eligible to work, skills etc. You can chat with me directly in multiple languages.")],
avatar_images=["./multiple_docs/Guest.jpg", "./multiple_docs/Thierry Picture.jpg"]
)
msg = gr.Textbox(placeholder="Type your question here...")
clear = gr.Button("Clear")
def user(query, chat_history):
chat_history_tuples = [(m[0], m[1]) for m in chat_history]
result = chain({"question": query, "chat_history": chat_history_tuples})
chat_history.append((query, result["answer"]))
return gr.update(value=""), chat_history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch(debug=True) # remove share=True if running in HF Spaces