ShynBui commited on
Commit
cee2c03
1 Parent(s): 4433ef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -9
app.py CHANGED
@@ -6,6 +6,7 @@ import requests
6
  from pyvi import ViTokenizer, ViPosTagger
7
  import time
8
  from transformers import AutoTokenizer, AutoModelForQuestionAnswering
 
9
  import torch
10
 
11
  retriever = load_the_embedding_retrieve(is_ready=False, k=3)
@@ -15,23 +16,39 @@ ensemble_retriever = EnsembleRetriever(
15
  retrievers=[bm25_retriever, retriever], weights=[0.2, 0.8]
16
  )
17
 
 
18
 
19
 
20
- def greet2(quote):
21
 
22
- qa_chain = get_qachain(retriever=ensemble_retriever)
 
 
 
23
 
24
- prompt = os.environ['PROMPT']
25
 
26
- qa_chain.combine_documents_chain.llm_chain.prompt.messages[0].prompt.template = prompt
27
 
28
- llm_response = qa_chain(quote)
 
29
 
30
- return llm_response['result']
 
31
 
 
32
 
33
- if __name__ == "__main__":
34
- quote = "Địa chỉ nhà trường?"
 
 
 
 
 
35
 
36
- iface = gr.Interface(fn=greet2, inputs="text", outputs="text")
 
 
 
 
37
  iface.launch()
 
6
  from pyvi import ViTokenizer, ViPosTagger
7
  import time
8
  from transformers import AutoTokenizer, AutoModelForQuestionAnswering
9
+ from langchain_community.chat_message_histories import ChatMessageHistory
10
  import torch
11
 
12
  retriever = load_the_embedding_retrieve(is_ready=False, k=3)
 
16
  retrievers=[bm25_retriever, retriever], weights=[0.2, 0.8]
17
  )
18
 
19
+ llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0, openai_api_key= os.environ["OPENAI_API_KEY"])
20
 
21
 
22
+ def greet3(quote, history):
23
 
24
+ demo_ephemeral_chat_history = ChatMessageHistory()
25
+ for user, assistant in eval(history):
26
+ demo_ephemeral_chat_history.add_user_message(user)
27
+ demo_ephemeral_chat_history.add_ai_message(assistant)
28
 
29
+ #Summary the message
30
 
31
+ chat_history = summarize_messages(demo_ephemeral_chat_history=demo_ephemeral_chat_history, llm=llm).messages
32
 
33
+ #Get the new question
34
+ new_question = get_question_from_summarize(chat_history[0].content, quote, llm)
35
 
36
+ #Retrieve
37
+ documents_query = ensemble_retriever.invoke(new_question)
38
 
39
+ # print(documents_query)
40
 
41
+ context = ''
42
+ for i in documents_query:
43
+ context += i.page_content + '\n'
44
+
45
+ #Get answer
46
+
47
+ answer = get_final_answer(question=new_question, context=context, chat_history=chat_history, prompt=os.environ['PROMPT'], llm=llm)
48
 
49
+ return new_question, answer
50
+
51
+
52
+ if __name__ == "__main__":
53
+ iface = gr.Interface(fn=greet3, inputs=["text", "text"], outputs=["text", "text"])
54
  iface.launch()