Sameer360/Document_based_chatbot
0
1### chatbot.py ###2from huggingface_hub import InferenceClient3from config import HUGGINGFACEHUB_API_TOKEN4from retriever import retrieve_context5from langchain.memory import ConversationBufferMemory6import time7 8client = InferenceClient(9 model="mistralai/Mistral-7B-Instruct-v0.3", token=HUGGINGFACEHUB_API_TOKEN10)11memory = ConversationBufferMemory()12 13 14def ask(query, db, message_placeholder):15 # print("Debug: ask function started") #start16 try:17 formatted_input, src = retrieve_context(query, db, memory)18 # print(f"Debug: src got in ask function : {src}") # src19 full_response = ""20 for chunk in client.text_generation(formatted_input):21 full_response += chunk22 message_placeholder.markdown(full_response + "▌")23 time.sleep(0.005)24 message_placeholder.markdown(full_response)25 memory.save_context({"input": query}, {"output": full_response})26 # print("Debug: ask function ended") #end27 return full_response, src28 except Exception as e:29 message_placeholder.error("Sorry, there was an error processing your request.")30 # print(f"Debug: Error in ask function: {e}")#error31 return "Error", "Unknown source"32 