Spaces:
CyranoB
/
Runtime error

Zihao Wang commited on
Commit
725388b
1 Parent(s): a625cd7

update apps

Browse files
Files changed (1) hide show
  1. app.py +35 -4
app.py CHANGED
@@ -142,6 +142,33 @@ def split_draft(draft, split_char = '\n\n'):
142
  # print(f"The draft answer has {len(draft_paragraphs)}")
143
  return draft_paragraphs
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  def get_query(question, answer):
146
  query_prompt = '''
147
  I want to verify the content correctness of the given question, especially the last sentences.
@@ -220,7 +247,9 @@ Just output the revised answer directly. DO NOT add additional explanations or a
220
 
221
  def get_reflect_answer(question, answer):
222
  reflect_prompt = '''
223
- Add a title to each paragraph in the answer and output the final answer using markdown format. This will make the answer to this question look more structured for better understanding.
 
 
224
  **IMPORTANT**
225
  Try to keep the structure (multiple paragraphs with its subtitles) in the response and make it more structual for understanding.
226
  Split the paragraphs with \n\n characters.
@@ -308,7 +337,8 @@ def rat(question):
308
  # print(f"##################### END #######################")
309
 
310
  print(f"{datetime.now()} [INFO] Processing draft ...")
311
- draft_paragraphs = split_draft(draft)
 
312
  print(f"{datetime.now()} [INFO] Draft is splitted into {len(draft_paragraphs)} sections.")
313
  answer = ""
314
  for i, p in enumerate(draft_paragraphs):
@@ -336,10 +366,11 @@ def rat(question):
336
  else:
337
  content = res
338
 
 
339
  for j, c in enumerate(content):
340
- if j > 2:
341
  break
342
- print(f"{datetime.now()} [INFO] Revising answers with retrieved network pages...[{j}/{min(len(content),3)}]")
343
  # answer = get_revise_answer(question, answer, c)
344
  res = run_with_timeout(get_revise_answer_wrapper, 15, question, answer, c)
345
  if not res:
 
142
  # print(f"The draft answer has {len(draft_paragraphs)}")
143
  return draft_paragraphs
144
 
145
+ def split_draft_openai(question, answer, NUM_PARAGRAPHS = 3):
146
+ split_prompt = f'''
147
+ Split the answer of the question into multiple paragraphs with each paragraph containing a complete thought.
148
+ The answer should be splited into less than {NUM_PARAGRAPHS} paragraphs.
149
+ Use ## as splitting char to seperate the paragraphs.
150
+ So you should output the answer with ## to split the paragraphs.
151
+ **IMPORTANT**
152
+ Just output the query directly. DO NOT add additional explanations or introducement in the answer unless you are asked to.
153
+ '''
154
+ openai_client = OpenAI(api_key = os.getenv('OPENAI_API_KEY'))
155
+ splited_answer = openai_client.chat.completions.create(
156
+ model="gpt-3.5-turbo",
157
+ messages=[
158
+ {
159
+ "role": "system",
160
+ "content": chatgpt_system_prompt
161
+ },
162
+ {
163
+ "role": "user",
164
+ "content": f"##Question: {question}\n\n##Response: {answer}\n\n##Instruction: {split_prompt}"
165
+ }
166
+ ],
167
+ temperature = 1.0
168
+ ).choices[0].message.content
169
+ split_draft_paragraphs = split_draft(splited_answer, split_char = '##')
170
+ return split_draft_paragraphs
171
+
172
  def get_query(question, answer):
173
  query_prompt = '''
174
  I want to verify the content correctness of the given question, especially the last sentences.
 
247
 
248
  def get_reflect_answer(question, answer):
249
  reflect_prompt = '''
250
+ Give a title for the answer of the question.
251
+ And add a subtitle to each paragraph in the answer and output the final answer using markdown format.
252
+ This will make the answer to this question look more structured for better understanding.
253
  **IMPORTANT**
254
  Try to keep the structure (multiple paragraphs with its subtitles) in the response and make it more structual for understanding.
255
  Split the paragraphs with \n\n characters.
 
337
  # print(f"##################### END #######################")
338
 
339
  print(f"{datetime.now()} [INFO] Processing draft ...")
340
+ # draft_paragraphs = split_draft(draft)
341
+ draft_paragraphs = split_draft_openai(question, draft)
342
  print(f"{datetime.now()} [INFO] Draft is splitted into {len(draft_paragraphs)} sections.")
343
  answer = ""
344
  for i, p in enumerate(draft_paragraphs):
 
366
  else:
367
  content = res
368
 
369
+ LIMIT = 2
370
  for j, c in enumerate(content):
371
+ if j >= LIMIT: # limit rge number of network pages
372
  break
373
+ print(f"{datetime.now()} [INFO] Revising answers with retrieved network pages...[{j}/{min(len(content),LIMIT)}]")
374
  # answer = get_revise_answer(question, answer, c)
375
  res = run_with_timeout(get_revise_answer_wrapper, 15, question, answer, c)
376
  if not res: