lindsay-qu commited on
Commit
a1dacf0
1 Parent(s): 675f9f4

Update core/chatbot/retrieval_chatbot.py

Browse files
Files changed (1) hide show
  1. core/chatbot/retrieval_chatbot.py +19 -6
core/chatbot/retrieval_chatbot.py CHANGED
@@ -4,6 +4,7 @@ from ..retriever import BaseRetriever, ChromaRetriever, FaissRetriever
4
  from ..refiner import BaseRefiner, SimpleRefiner
5
  from models import BaseModel, GPT4Model
6
  from prompts import DecomposePrompt, QAPrompt, SummaryPrompt, ReferencePrompt
 
7
  import ast
8
  from utils.image_encoder import encode_image
9
  import asyncio
@@ -39,12 +40,15 @@ class RetrievalChatbot(BaseChatbot):
39
  print("Query: {message}".format(message=message))
40
  question = self.decomposer.refine(message, None, image_paths)
41
  print(question)
42
- question = question.replace('"', "'").replace("\n", "").replace("', '", "','").lstrip("['").rstrip("']")
43
- sub_questions = question.split("','")
44
- print("Decomposed your query into subquestions: {sub_questions}".format(sub_questions=sub_questions))
 
 
 
45
  tasks = []
46
  time2 = time.time()
47
- for sub_question in sub_questions:
48
  # print("="*20)
49
  # print(f"Subquestion: {sub_question}")
50
  # print(f"Retrieving pdf papers for references...\n")
@@ -100,7 +104,16 @@ class RetrievalChatbot(BaseChatbot):
100
  async def subquestion_answerer(self, sub_question: str, image_paths=None, return_logs=False) -> str:
101
  sub_retrieve_reference=""
102
  time_s = time.time()
103
- sub_retrieve, titles = self.retriever.retrieve(sub_question)
 
 
 
 
 
 
 
 
 
104
  for ref in sub_retrieve:
105
  sub_retrieve_reference += "Related research: {ref}\n".format(ref=ref)
106
  sub_answerer_context = "Sub Question References: {sub_retrieve_reference}\nQuestion: {question}\n".format(sub_retrieve_reference=sub_retrieve_reference, question=sub_question)
@@ -112,5 +125,5 @@ class RetrievalChatbot(BaseChatbot):
112
  print(f"Subanswer: {sub_answer}")
113
  return {
114
  "answer": "Subquestion: {sub_question}\nSubanswer: {sub_answer}\n\n\n".format(sub_question=sub_question, sub_answer=sub_answer),
115
- "titles": titles
116
  }
 
4
  from ..refiner import BaseRefiner, SimpleRefiner
5
  from models import BaseModel, GPT4Model
6
  from prompts import DecomposePrompt, QAPrompt, SummaryPrompt, ReferencePrompt
7
+ from utils import convert_str_to_list
8
  import ast
9
  from utils.image_encoder import encode_image
10
  import asyncio
 
40
  print("Query: {message}".format(message=message))
41
  question = self.decomposer.refine(message, None, image_paths)
42
  print(question)
43
+ # question = question.replace('"', "'").replace("\n", "").replace("', '", "','").lstrip("['").rstrip("']")
44
+ # sub_questions = question.split("','")
45
+ # print("Decomposed your query into subquestions: {sub_questions}".format(sub_questions=sub_questions))
46
+ sub_questions_str = self.decomposer.refine(message, None, image_paths)
47
+ sub_questions_list = convert_str_to_list(sub_questions_str)
48
+ print("Decomposed your query into subquestions: {sub_questions}".format(sub_questions=sub_questions_list))
49
  tasks = []
50
  time2 = time.time()
51
+ for sub_question in sub_questions_list:
52
  # print("="*20)
53
  # print(f"Subquestion: {sub_question}")
54
  # print(f"Retrieving pdf papers for references...\n")
 
104
  async def subquestion_answerer(self, sub_question: str, image_paths=None, return_logs=False) -> str:
105
  sub_retrieve_reference=""
106
  time_s = time.time()
107
+ # sub_retrieve, titles = self.retriever.retrieve(sub_question)
108
+ if len(sub_question) >= 2:
109
+ sub_retrieve_english, titles_english = self.retriever.retrieve(sub_question[0])
110
+ sub_retrieve_chinese, titles_chinese = self.retriever.retrieve(sub_question[1])
111
+ else:
112
+ sub_retrieve_english, titles_english = self.retriever.retrieve(sub_question[0])
113
+ sub_retrieve_chinese, titles_chinese = [], []
114
+ # print("英文检索结果: ", sub_retrieve_english)
115
+ # print("中文检索结果: ", sub_retrieve_chinese)
116
+ sub_retrieve=sub_retrieve_english+sub_retrieve_chinese
117
  for ref in sub_retrieve:
118
  sub_retrieve_reference += "Related research: {ref}\n".format(ref=ref)
119
  sub_answerer_context = "Sub Question References: {sub_retrieve_reference}\nQuestion: {question}\n".format(sub_retrieve_reference=sub_retrieve_reference, question=sub_question)
 
125
  print(f"Subanswer: {sub_answer}")
126
  return {
127
  "answer": "Subquestion: {sub_question}\nSubanswer: {sub_answer}\n\n\n".format(sub_question=sub_question, sub_answer=sub_answer),
128
+ "titles": titles_english + titles_chinese
129
  }