qdqd commited on
Commit
806a7e3
·
verified ·
1 Parent(s): c868b37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
app.py CHANGED
@@ -7,14 +7,15 @@ import random
7
  def get_llm_response(prompt, model, max_retries=3):
8
  for attempt in range(max_retries):
9
  try:
10
- return DDGS().chat(prompt, model=model)
 
11
  except Exception as e:
12
  if attempt < max_retries - 1:
13
  print(f"Error occurred: {e}. Retrying in {2**attempt} seconds...")
14
  time.sleep(2**attempt + random.random())
15
  else:
16
  print(f"Max retries reached. Error: {e}")
17
- return f"<error>Unable to get response from {model} after {max_retries} attempts.</error>"
18
 
19
  def process_message(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
20
  conversation_history = deque(maxlen=5)
@@ -25,25 +26,32 @@ def process_message(message, history, analysis_prompt, rethinking_prompt, refine
25
 
26
  gpt4o_prompt = f"{analysis_prompt}\n\nConversation history:\n{context}\n\nUser query: {message}\n\nPlease analyze this query and respond accordingly."
27
  gpt4o_response = get_llm_response(gpt4o_prompt, "gpt-4o-mini")
28
- yield f"Analysis: {gpt4o_response}"
29
 
30
- if "<error>" in gpt4o_response:
31
  return
32
 
33
- llama_prompt = f"{rethinking_prompt}\n\nConversation history:\n{context}\n\nOriginal user query: {message}\n\nInitial response: {gpt4o_response}\n\nPlease review and suggest improvements or confirm if satisfactory."
34
  llama_response = get_llm_response(llama_prompt, "gpt-4o-mini")
35
- yield f"Analysis: {gpt4o_response}\nRethinking: {llama_response}"
36
 
37
- if "<error>" in llama_response:
38
- return gpt4o_response
39
 
40
- if "done" not in llama_response.lower():
41
- final_gpt4o_prompt = f"{refinement_prompt}\n\nConversation history:\n{context}\n\nOriginal user query: {message}\n\nInitial response: {gpt4o_response}\n\nSuggestion: {llama_response}\n\nPlease provide a final response considering the suggestion."
42
  final_response = get_llm_response(final_gpt4o_prompt, "gpt-4o-mini")
43
- yield f"Analysis: {gpt4o_response}\nRethinking: {llama_response}\nFinal Response: {final_response}"
44
- return final_response
45
  else:
46
- return gpt4o_response
 
 
 
 
 
 
 
47
 
48
  def respond(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
49
  response = ""
@@ -52,9 +60,12 @@ def respond(message, history, analysis_prompt, rethinking_prompt, refinement_pro
52
  yield response
53
 
54
  # Extract the final response from the last chunk
55
- final_response = response.split("Final Response: ")[-1] if "Final Response: " in response else response
56
  return final_response
57
 
 
 
 
58
  analysis_prompt = """
59
  You are Echo-Refraction, an AI assistant tasked with analyzing user queries. Your role is to:
60
  1. Carefully examine the user's input for clarity, completeness, and potential ambiguities.
@@ -93,13 +104,8 @@ Enclose your response in <output> tags.
93
 
94
  demo = gr.ChatInterface(
95
  respond,
96
- additional_inputs=[
97
- gr.Textbox(value=analysis_prompt, label="Analysis Prompt", lines=10),
98
- gr.Textbox(value=rethinking_prompt, label="Rethinking Prompt", lines=10),
99
- gr.Textbox(value=refinement_prompt, label="Refinement Prompt", lines=10),
100
- ],
101
- title="Echo-Refraction AI Assistant",
102
- description="Chat with Echo-Refraction, an AI assistant that analyzes, rethinks, and refines responses.",
103
  examples=[
104
  ["How many 'r' are there in the word 'strawberry'"],
105
  ["Explain the concept of quantum entanglement."],
 
7
  def get_llm_response(prompt, model, max_retries=3):
8
  for attempt in range(max_retries):
9
  try:
10
+ response = DDGS().chat(prompt, model=model)
11
+ return response.split() # Split the response into words
12
  except Exception as e:
13
  if attempt < max_retries - 1:
14
  print(f"Error occurred: {e}. Retrying in {2**attempt} seconds...")
15
  time.sleep(2**attempt + random.random())
16
  else:
17
  print(f"Max retries reached. Error: {e}")
18
+ return f"<error>Unable to get response from {model} after {max_retries} attempts.</error>".split()
19
 
20
  def process_message(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
21
  conversation_history = deque(maxlen=5)
 
26
 
27
  gpt4o_prompt = f"{analysis_prompt}\n\nConversation history:\n{context}\n\nUser query: {message}\n\nPlease analyze this query and respond accordingly."
28
  gpt4o_response = get_llm_response(gpt4o_prompt, "gpt-4o-mini")
29
+ yield from stream_words("Analysis: ", gpt4o_response)
30
 
31
+ if "<error>" in " ".join(gpt4o_response):
32
  return
33
 
34
+ llama_prompt = f"{rethinking_prompt}\n\nConversation history:\n{context}\n\nOriginal user query: {message}\n\nInitial response: {' '.join(gpt4o_response)}\n\nPlease review and suggest improvements or confirm if satisfactory."
35
  llama_response = get_llm_response(llama_prompt, "gpt-4o-mini")
36
+ yield from stream_words("\nRethinking: ", llama_response)
37
 
38
+ if "<error>" in " ".join(llama_response):
39
+ return " ".join(gpt4o_response)
40
 
41
+ if "done" not in " ".join(llama_response).lower():
42
+ final_gpt4o_prompt = f"{refinement_prompt}\n\nConversation history:\n{context}\n\nOriginal user query: {message}\n\nInitial response: {' '.join(gpt4o_response)}\n\nSuggestion: {' '.join(llama_response)}\n\nPlease provide a final response considering the suggestion."
43
  final_response = get_llm_response(final_gpt4o_prompt, "gpt-4o-mini")
44
+ yield from stream_words("\nFinal Response: ", final_response)
45
+ return " ".join(final_response)
46
  else:
47
+ return " ".join(gpt4o_response)
48
+
49
+ def stream_words(prefix, words):
50
+ response = prefix
51
+ for word in words:
52
+ response += word + " "
53
+ time.sleep(0.1) # Adjust this value to control the speed of word streaming
54
+ yield response
55
 
56
  def respond(message, history, analysis_prompt, rethinking_prompt, refinement_prompt):
57
  response = ""
 
60
  yield response
61
 
62
  # Extract the final response from the last chunk
63
+ final_response = response.split("Final Response: ")[-1] if "Final Response: " in response else response.split("Analysis: ")[-1]
64
  return final_response
65
 
66
+ # (The rest of the code remains the same: analysis_prompt, rethinking_prompt, refinement_prompt, and the Gradio
67
+ # (Previous code remains the same)
68
+
69
  analysis_prompt = """
70
  You are Echo-Refraction, an AI assistant tasked with analyzing user queries. Your role is to:
71
  1. Carefully examine the user's input for clarity, completeness, and potential ambiguities.
 
104
 
105
  demo = gr.ChatInterface(
106
  respond,
107
+ title="Open-O1",
108
+ description="Chat with Open-O1, an AI assistant that analyzes, rethinks, and refines responses. Watch as it streams its thought process word by word!",
 
 
 
 
 
109
  examples=[
110
  ["How many 'r' are there in the word 'strawberry'"],
111
  ["Explain the concept of quantum entanglement."],