JPBianchi commited on
Commit
87dd32d
1 Parent(s): 677bca6

Using the new gpt-3.5-turbo-0125

Browse files
Files changed (2) hide show
  1. app.py +39 -24
  2. openai_interface.py +3 -0
app.py CHANGED
@@ -394,7 +394,7 @@ def main():
394
 
395
  # let's use Llama2, and fall back on GPT3.5 if it fails
396
  reworded_query = reword_query(query, guest,
397
- model_name='llama2-13b-chat')
398
  new_query = reworded_query['rewritten_question']
399
 
400
  if reworded_query['status'] != 'error': # or reworded_query['changed']:
@@ -566,21 +566,21 @@ def reword_query(query, guest, model_name='llama2-13b-chat', response_processing
566
  prompt_fields = {
567
  "you_are":f"You are an expert in linguistics and semantics, analyzing the question asked by a user to a vector search system, \
568
  and making sure that the question is well formulated and that the system can understand it.",
569
-
570
  "your_task":f"Your task is to detect if the name of the guest ({guest}) is mentioned in the user's question, \
571
  and if that is not the case, rewrite the question using the guest name, \
572
  without changing the meaning of the question. \
573
  Most of the time, the user will have used a pronoun to designate the guest, in which case, \
574
  simply replace the pronoun with the guest name.",
575
-
576
- "question":f"If the user mentions the guest name, ie {query}, just return his question as is. \
577
  If the user does not mention the guest name, rewrite the question using the guest name.",
578
-
579
- "final_instruction":f"Only regerate the requested rewritten question or the original, WITHOUT ANY COMMENT OR REPHRASING. \
580
  Your answer must be as close as possible to the original question, \
581
  and exactly identical, word for word, if the user mentions the guest name, i.e. {guest}.",
582
  }
583
-
584
  # prompt created by chatGPT :-)
585
  # and Llama still outputs the original question and precedes the answer with 'rewritten question'
586
  prompt_fields2 = {
@@ -596,7 +596,7 @@ def reword_query(query, guest, model_name='llama2-13b-chat', response_processing
596
  "question": (
597
  f"Original question: '{query}'. "
598
  "Rewrite this question to include the guest's FULL name if it's not already mentioned."
599
- "The Only thing you can and MUST add is a question mark if it's missing."
600
  ),
601
  "final_instruction": (
602
  "Create a rewritten question or keep the original question as is. "
@@ -652,7 +652,7 @@ def reword_query(query, guest, model_name='llama2-13b-chat', response_processing
652
  # st.write(f"Error in LLM response: 'error':{reworded_query['error']}")
653
  # st.write("The LLM could not connect to the server. Please try again later.")
654
  # st.stop()
655
- return reword_query(query, guest, model_name='gpt-3.5-turbo-0613')
656
 
657
  if response_processing:
658
  if isinstance(response, list) and isinstance(response[0], dict) and 'generated_text' in response[0]:
@@ -666,7 +666,7 @@ def reword_query(query, guest, model_name='llama2-13b-chat', response_processing
666
  return result | {'status': 'success'}
667
  else:
668
  print("Found no answer")
669
- return reword_query(query, guest, model_name='gpt-3.5-turbo-0613')
670
  # return {'original_question': query, 'rewritten_question': query, 'changed': False, 'status': 'no properly formatted answer' }
671
  else:
672
  return response
@@ -677,36 +677,51 @@ def reword_query(query, guest, model_name='llama2-13b-chat', response_processing
677
  # return response[0]['generated_text'] .split("Rewritten question: '")[-1][:-1]
678
 
679
  else:
680
- # assume openai
681
- model_ids = ['gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613']
682
- model_name = model_ids[1]
683
- GPTllm = GPT_Turbo(model=model_name,
684
- api_key=openai_api_key)
685
 
686
  openai_prompt = """
687
- {your_task}\n
 
688
  ```
689
  \n\n
690
  Question: {question}\n
691
- {final_instruction}
692
 
693
  Answer:
694
  """
695
  prompt = openai_prompt.format(**prompt_fields)
696
-
 
 
 
 
 
 
 
 
 
697
  try:
698
- resp = GPTllm.get_chat_completion(prompt=openai_prompt,
 
699
  system_message=prompt_fields['you_are'],
 
700
  temperature=0.01,
701
- max_tokens=1500, # it's a question...
702
  show_response=True,
703
  stream=False)
704
- return {'rewritten_question': resp.choices[0].delta.content,
705
- 'changed': True, 'status': 'success'}
 
 
 
 
706
  except Exception:
707
  return {'rewritten_question': query, 'changed': False, 'status': 'not success'}
708
-
709
 
710
  if __name__ == '__main__':
711
  main()
712
- # %%
 
394
 
395
  # let's use Llama2, and fall back on GPT3.5 if it fails
396
  reworded_query = reword_query(query, guest,
397
+ model_name='gpt-3.5-turbo-0125')
398
  new_query = reworded_query['rewritten_question']
399
 
400
  if reworded_query['status'] != 'error': # or reworded_query['changed']:
 
566
  prompt_fields = {
567
  "you_are":f"You are an expert in linguistics and semantics, analyzing the question asked by a user to a vector search system, \
568
  and making sure that the question is well formulated and that the system can understand it.",
569
+
570
  "your_task":f"Your task is to detect if the name of the guest ({guest}) is mentioned in the user's question, \
571
  and if that is not the case, rewrite the question using the guest name, \
572
  without changing the meaning of the question. \
573
  Most of the time, the user will have used a pronoun to designate the guest, in which case, \
574
  simply replace the pronoun with the guest name.",
575
+
576
+ "question":f"If the user mentions the guest name ({guest}) in the following question '{query}', just return his question as is. \
577
  If the user does not mention the guest name, rewrite the question using the guest name.",
578
+
579
+ "final_instruction":f"Only regenerate the requested rewritten question or the original, WITHOUT ANY COMMENT OR REPHRASING. \
580
  Your answer must be as close as possible to the original question, \
581
  and exactly identical, word for word, if the user mentions the guest name, i.e. {guest}.",
582
  }
583
+
584
  # prompt created by chatGPT :-)
585
  # and Llama still outputs the original question and precedes the answer with 'rewritten question'
586
  prompt_fields2 = {
 
596
  "question": (
597
  f"Original question: '{query}'. "
598
  "Rewrite this question to include the guest's FULL name if it's not already mentioned."
599
+ "Add a question mark if it's missing, nothing else."
600
  ),
601
  "final_instruction": (
602
  "Create a rewritten question or keep the original question as is. "
 
652
  # st.write(f"Error in LLM response: 'error':{reworded_query['error']}")
653
  # st.write("The LLM could not connect to the server. Please try again later.")
654
  # st.stop()
655
+ return reword_query(query, guest, model_name='gpt-3.5-turbo-0125')
656
 
657
  if response_processing:
658
  if isinstance(response, list) and isinstance(response[0], dict) and 'generated_text' in response[0]:
 
666
  return result | {'status': 'success'}
667
  else:
668
  print("Found no answer")
669
+ return reword_query(query, guest, model_name='gpt-3.5-turbo-0125')
670
  # return {'original_question': query, 'rewritten_question': query, 'changed': False, 'status': 'no properly formatted answer' }
671
  else:
672
  return response
 
677
  # return response[0]['generated_text'] .split("Rewritten question: '")[-1][:-1]
678
 
679
  else:
680
+ # we assume / force openai
681
+ model_ids = ['gpt-3.5-turbo-0125', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613']
682
+ if model_name not in model_ids:
683
+ model_name = model_ids[0]
684
+ GPTllm = GPT_Turbo(model=model_name, api_key=openai_api_key)
685
 
686
  openai_prompt = """
687
+ {your_task} \n
688
+ {final_instruction} /n
689
  ```
690
  \n\n
691
  Question: {question}\n
 
692
 
693
  Answer:
694
  """
695
  prompt = openai_prompt.format(**prompt_fields)
696
+
697
+ openai_prompt2 = """
698
+ {your_task}\n
699
+ ```
700
+ \n\n
701
+ {final_instruction}
702
+ """
703
+ prompt2 = openai_prompt2.format(**{'your_task':prompt_fields['your_task'],
704
+ 'final_instruction':prompt_fields['final_instruction']})
705
+
706
  try:
707
+ # https://platform.openai.com/docs/guides/text-generation/chat-completions-api
708
+ resp = GPTllm.get_chat_completion(prompt=prompt,
709
  system_message=prompt_fields['you_are'],
710
+ user_message = None, #prompt_fields['question'],
711
  temperature=0.01,
712
+ max_tokens=1500, # it's a long question...
713
  show_response=True,
714
  stream=False)
715
+
716
+ if resp.choices[0].finish_reason == 'stop':
717
+ return {'rewritten_question': resp.choices[0].message.content,
718
+ 'changed': True, 'status': 'success'}
719
+ else:
720
+ raise Exception("LLM did not stop") # to go to the except block
721
  except Exception:
722
  return {'rewritten_question': query, 'changed': False, 'status': 'not success'}
723
+
724
 
725
  if __name__ == '__main__':
726
  main()
727
+ # streamlit run app.py --server.allowRunOnSave True
openai_interface.py CHANGED
@@ -21,6 +21,7 @@ class GPT_Turbo:
21
  def get_chat_completion(self,
22
  prompt: str,
23
  system_message: str='You are a helpful assistant.',
 
24
  temperature: int=0,
25
  max_tokens: int=500,
26
  stream: bool=False,
@@ -30,6 +31,8 @@ class GPT_Turbo:
30
  {'role': 'system', 'content': system_message},
31
  {'role': 'assistant', 'content': prompt}
32
  ]
 
 
33
 
34
  response = self.client.chat.completions.create( model=self.model,
35
  messages=messages,
 
21
  def get_chat_completion(self,
22
  prompt: str,
23
  system_message: str='You are a helpful assistant.',
24
+ user_message: str=None,
25
  temperature: int=0,
26
  max_tokens: int=500,
27
  stream: bool=False,
 
31
  {'role': 'system', 'content': system_message},
32
  {'role': 'assistant', 'content': prompt}
33
  ]
34
+ if user_message is not None:
35
+ messages.append({'role': 'user', 'content': user_message})
36
 
37
  response = self.client.chat.completions.create( model=self.model,
38
  messages=messages,