qualchat / app.py
sfarrukh
bot
ba0df82
raw
history blame
3.36 kB
import gradio as gr
import os
from dotenv import load_dotenv
load_dotenv()
# Use followin json data to feed to Chroma
import json
with open("data/processed/final_data_for_vectorstore.json",'r') as file:
data4chroma= json.load(file)
# Initiate vector store
# from langchain_community.vectorstores import Chroma
# from langchain_huggingface import HuggingFaceEmbeddings
# embedding_function=HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2')
# vectorstore=Chroma.from_texts(texts=data4chroma['chunks'],
# embedding=embedding_function,
# ids=data4chroma["chunk_ids"],
# metadatas=data4chroma["chunk_metadatas"],
# collection_name='qual_books',
# )
from langchain import hub
prompt= hub.pull("rlm/rag-prompt")
from langchain_huggingface import HuggingFaceEndpoint
llm=HuggingFaceEndpoint(repo_id="meta-llama/Meta-Llama-3.1-70B-Instruct",
max_new_tokens=3000,
top_k=20,
top_p=0.95,
typical_p=0.95,
temperature=0.001,
repetition_penalty=1.03,
huggingfacehub_api_token=os.getenv("huggingfacehub_api_token")
)
chain = prompt | llm
context="The First Crusade (1096-1099) was launched by Pope Urban II in response to the Byzantine Emperor's request for assistance against the Seljuk Turks. It marked the beginning of a series of religious wars aimed at reclaiming Jerusalem and other holy sites in the Near East. Crusaders from various European kingdoms, motivated by religious zeal and the promise of eternal salvation, marched toward the Levant. After capturing several cities, they laid siege to Jerusalem in 1099, eventually capturing the city and establishing several Crusader states. The First Crusade was followed by many others, as the struggle for control of the Holy Land continued for centuries."
def respond(
query: str,
data_type: str = "Preprocessed doc",
llm_chain = chain,
context=context
# vectorstore=vectorstore
):
"""
Generate a response to a user query using document retrieval and language model
completion
Parameters:
chatbot (List): List representing the chatbot's conversation history.
message (str): The user's query.
data_type (str): Type of data used for document retrieval
temperature (float);
Returns:
Tuple: A tuple containing an empty string, the updated chat history,
and reference from retrieved documents
"""
# Retrieve embedding function from code env resources
if data_type=="Preprocessed doc":
# retriever=vectorstore.as_retriever(search_type="mmr",
# search_kwargs={"k":10,"fetch_k":100})
# retrieved_docs=retriever.invoke(query)
# input_2_chain={"context": retrieved_docs, "question":query}
input_2_chain={"context": context, "question":query}
response=llm_chain.invoke(input_2_chain)
return response
demo = gr.Interface(fn=respond, inputs="text", outputs="text")
demo.launch()