Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -38,7 +38,48 @@ def make_api_call(messages, max_tokens, is_final_answer=False):
|
|
38 |
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
|
39 |
time.sleep(1) # Wait for 1 second before retrying
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
def main():
|
44 |
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
|
|
|
38 |
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
|
39 |
time.sleep(1) # Wait for 1 second before retrying
|
40 |
|
41 |
+
def generate_response(prompt):
|
42 |
+
messages = [
|
43 |
+
{"role": "system", "content": """You are an expert AI assistant that explains your reasoning step by step. For each step, provide a title that describes what you're doing in that step, along with the content. Decide if you need another step or if you're ready to give the final answer. Respond in JSON format with 'title', 'content', and 'next_action' (either 'continue' or 'final_answer') keys. USE AS MANY REASONING STEPS AS POSSIBLE. AT LEAST 3. BE AWARE OF YOUR LIMITATIONS AS AN LLM AND WHAT YOU CAN AND CANNOT DO. IN YOUR REASONING, INCLUDE EXPLORATION OF ALTERNATIVE ANSWERS. CONSIDER YOU MAY BE WRONG, AND IF YOU ARE WRONG IN YOUR REASONING, WHERE IT WOULD BE. FULLY TEST ALL OTHER POSSIBILITIES. YOU CAN BE WRONG. WHEN YOU SAY YOU ARE RE-EXAMINING, ACTUALLY RE-EXAMINE, AND USE ANOTHER APPROACH TO DO SO. DO NOT JUST SAY YOU ARE RE-EXAMINING. USE AT LEAST 3 METHODS TO DERIVE THE ANSWER. USE BEST PRACTICES."""},
|
44 |
+
{"role": "user", "content": prompt},
|
45 |
+
{"role": "assistant", "content": "Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem."}
|
46 |
+
]
|
47 |
+
|
48 |
+
steps = []
|
49 |
+
step_count = 1
|
50 |
+
total_thinking_time = 0
|
51 |
+
|
52 |
+
while True:
|
53 |
+
start_time = time.time()
|
54 |
+
step_data = make_api_call(messages, 300)
|
55 |
+
end_time = time.time()
|
56 |
+
thinking_time = end_time - start_time
|
57 |
+
total_thinking_time += thinking_time
|
58 |
+
|
59 |
+
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
|
60 |
+
|
61 |
+
messages.append({"role": "assistant", "content": json.dumps(step_data)})
|
62 |
+
|
63 |
+
if step_data['next_action'] == 'final_answer' or step_count > 25: # Maximum of 25 steps to prevent infinite thinking time. Can be adjusted.
|
64 |
+
break
|
65 |
+
|
66 |
+
step_count += 1
|
67 |
+
|
68 |
+
# Yield after each step for Streamlit to update
|
69 |
+
yield steps, None # We're not yielding the total time until the end
|
70 |
+
|
71 |
+
# Generate final answer
|
72 |
+
messages.append({"role": "user", "content": "Please provide the final answer based on your reasoning above."})
|
73 |
+
|
74 |
+
start_time = time.time()
|
75 |
+
final_data = make_api_call(messages, 200, is_final_answer=True)
|
76 |
+
end_time = time.time()
|
77 |
+
thinking_time = end_time - start_time
|
78 |
+
total_thinking_time += thinking_time
|
79 |
+
|
80 |
+
steps.append(("Final Answer", final_data['content'], thinking_time))
|
81 |
+
|
82 |
+
yield steps, total_thinking_time
|
83 |
|
84 |
def main():
|
85 |
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
|