abhivsh commited on
Commit
ed960ea
·
verified ·
1 Parent(s): b52fa15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -45,22 +45,44 @@ llm_name = "gpt-3.5-turbo-0301"
45
  vectordb = initialize.initialize()
46
 
47
 
48
- history = "Hello !"
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  def chat_query(question, history):
51
 
52
- query = f"As an experienced Electrical Engineer, answer pointwise to the Question : {question} and consider your previous reply as : {history[:-1]}, if any."
 
 
 
 
 
53
  #llm = ChatOpenAI(model=llm_name, temperature=0.1, api_key = OPENAI_API_KEY)
54
  llm = GoogleGenerativeAI(model="gemini-1.0-pro", google_api_key=GEMINI_API_KEY)
55
 
56
  # Conversation Retrival Chain with Memory
57
- memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
58
  retriever=vectordb.as_retriever()
59
- qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)
60
 
61
  # Replace input() with question variable for Gradio
62
- result = qa({"question": query})
63
- return result['answer']
 
 
 
 
 
 
64
 
65
  # Chatbot only answers based on Documents
66
  # qa = VectorDBQA.from_chain_type(llm=OpenAI(openai_api_key = OPENAI_API_KEY, ), chain_type="stuff", vectorstore=vectordb)
 
45
  vectordb = initialize.initialize()
46
 
47
 
48
+
49
+ chat_history = []
50
+
51
+ # For getting source documents
52
+ def get_file(source_documents):
53
+ files = set()
54
+ for doc in source_documents:
55
+ file = os.path.basename(doc.metadata['source'])
56
+ files.add(file)
57
+ # Print unique filenames
58
+ return list(set(files))
59
+
60
 
61
  def chat_query(question, history):
62
 
63
+
64
+ query = f"""As an experienced Electrical Engineer, please provide an elaborate, precise, and pointwise answer politely to the question: {question}.
65
+ Also, Please consider the provided chat history: {history[:-1]}
66
+ Ensure that your response is detailed, accurate, and addresses each aspect of the question thoroughly.
67
+ If the context of the question doesn't align with the chat history, please provide your response in a fresh manner. """
68
+
69
  #llm = ChatOpenAI(model=llm_name, temperature=0.1, api_key = OPENAI_API_KEY)
70
  llm = GoogleGenerativeAI(model="gemini-1.0-pro", google_api_key=GEMINI_API_KEY)
71
 
72
  # Conversation Retrival Chain with Memory
73
+ #memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
74
  retriever=vectordb.as_retriever()
75
+ qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, return_source_documents=True)
76
 
77
  # Replace input() with question variable for Gradio
78
+ result = qa({"question": query, "chat_history" : history[:-1]})
79
+
80
+
81
+ source_docs = result["source_documents"]
82
+ file_name = get_file(source_docs)[0]
83
+
84
+
85
+ return result["answer"] + "\n\nSource : " + file_name
86
 
87
  # Chatbot only answers based on Documents
88
  # qa = VectorDBQA.from_chain_type(llm=OpenAI(openai_api_key = OPENAI_API_KEY, ), chain_type="stuff", vectorstore=vectordb)