Skip to content

Commit 585a2a2

Browse files
committed
move g1 core code to new file
1 parent 658a56c commit 585a2a2

2 files changed

Lines changed: 87 additions & 85 deletions

File tree

app.py

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,6 @@
11
import streamlit as st
2-
import groq
3-
import os
2+
from g1 import generate_response
43
import json
5-
import time
6-
7-
client = groq.Groq()
8-
9-
def make_api_call(messages, max_tokens, is_final_answer=False):
10-
for attempt in range(3):
11-
try:
12-
if is_final_answer:
13-
response = client.chat.completions.create(
14-
model="llama-3.1-70b-versatile",
15-
messages=messages,
16-
max_tokens=max_tokens,
17-
temperature=0.2,
18-
)
19-
return response.choices[0].message.content
20-
else:
21-
response = client.chat.completions.create(
22-
model="llama-3.1-70b-versatile",
23-
messages=messages,
24-
max_tokens=max_tokens,
25-
temperature=0.2,
26-
response_format={"type": "json_object"}
27-
)
28-
return json.loads(response.choices[0].message.content)
29-
except Exception as e:
30-
if attempt == 2:
31-
if is_final_answer:
32-
return {"title": "Error", "content": f"Failed to generate final answer after 3 attempts. Error: {str(e)}"}
33-
else:
34-
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
35-
time.sleep(1) # Wait for 1 second before retrying
36-
37-
def generate_response(prompt):
38-
messages = [
39-
{"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.
40-
41-
Example of a valid JSON response:
42-
```json
43-
{
44-
"title": "Identifying Key Information",
45-
"content": "To begin solving this problem, we need to carefully examine the given information and identify the crucial elements that will guide our solution process. This involves...",
46-
"next_action": "continue"
47-
}```
48-
"""},
49-
{"role": "user", "content": prompt},
50-
{"role": "assistant", "content": "Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem."}
51-
]
52-
53-
steps = []
54-
step_count = 1
55-
total_thinking_time = 0
56-
57-
while True:
58-
start_time = time.time()
59-
step_data = make_api_call(messages, 300)
60-
end_time = time.time()
61-
thinking_time = end_time - start_time
62-
total_thinking_time += thinking_time
63-
64-
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
65-
66-
messages.append({"role": "assistant", "content": json.dumps(step_data)})
67-
68-
if step_data['next_action'] == 'final_answer' or step_count > 25: # Maximum of 25 steps to prevent infinite thinking time. Can be adjusted.
69-
break
70-
71-
step_count += 1
72-
73-
# Yield after each step for Streamlit to update
74-
yield steps, None # We're not yielding the total time until the end
75-
76-
# Generate final answer
77-
messages.append({"role": "user", "content": "Please provide the final answer based solely on your reasoning above. Do not use JSON formatting. Only provide the text response without any titles or preambles. Retain any formatting as instructed by the original prompt, such as exact formatting for free response or multiple choice."})
78-
79-
start_time = time.time()
80-
final_data = make_api_call(messages, 1200, is_final_answer=True)
81-
end_time = time.time()
82-
thinking_time = end_time - start_time
83-
total_thinking_time += thinking_time
84-
85-
steps.append(("Final Answer", final_data, thinking_time))
86-
87-
yield steps, total_thinking_time
884

895
def main():
906
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")

g1.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import groq
2+
import time
3+
import os
4+
import json
5+
6+
client = groq.Groq()
7+
8+
def make_api_call(messages, max_tokens, is_final_answer=False):
9+
for attempt in range(3):
10+
try:
11+
if is_final_answer:
12+
response = client.chat.completions.create(
13+
model="llama-3.1-70b-versatile",
14+
messages=messages,
15+
max_tokens=max_tokens,
16+
temperature=0.2,
17+
)
18+
return response.choices[0].message.content
19+
else:
20+
response = client.chat.completions.create(
21+
model="llama-3.1-70b-versatile",
22+
messages=messages,
23+
max_tokens=max_tokens,
24+
temperature=0.2,
25+
response_format={"type": "json_object"}
26+
)
27+
return json.loads(response.choices[0].message.content)
28+
except Exception as e:
29+
if attempt == 2:
30+
if is_final_answer:
31+
return {"title": "Error", "content": f"Failed to generate final answer after 3 attempts. Error: {str(e)}"}
32+
else:
33+
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
34+
time.sleep(1) # Wait for 1 second before retrying
35+
36+
def generate_response(prompt):
37+
messages = [
38+
{"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.
39+
40+
Example of a valid JSON response:
41+
```json
42+
{
43+
"title": "Identifying Key Information",
44+
"content": "To begin solving this problem, we need to carefully examine the given information and identify the crucial elements that will guide our solution process. This involves...",
45+
"next_action": "continue"
46+
}```
47+
"""},
48+
{"role": "user", "content": prompt},
49+
{"role": "assistant", "content": "Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem."}
50+
]
51+
52+
steps = []
53+
step_count = 1
54+
total_thinking_time = 0
55+
56+
while True:
57+
start_time = time.time()
58+
step_data = make_api_call(messages, 300)
59+
end_time = time.time()
60+
thinking_time = end_time - start_time
61+
total_thinking_time += thinking_time
62+
63+
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
64+
65+
messages.append({"role": "assistant", "content": json.dumps(step_data)})
66+
67+
if step_data['next_action'] == 'final_answer' or step_count > 25: # Maximum of 25 steps to prevent infinite thinking time. Can be adjusted.
68+
break
69+
70+
step_count += 1
71+
72+
# Yield after each step for Streamlit to update
73+
yield steps, None # We're not yielding the total time until the end
74+
75+
# Generate final answer
76+
messages.append({"role": "user", "content": "Please provide the final answer based solely on your reasoning above. Do not use JSON formatting. Only provide the text response without any titles or preambles. Retain any formatting as instructed by the original prompt, such as exact formatting for free response or multiple choice."})
77+
78+
start_time = time.time()
79+
final_data = make_api_call(messages, 1200, is_final_answer=True)
80+
end_time = time.time()
81+
thinking_time = end_time - start_time
82+
total_thinking_time += thinking_time
83+
84+
steps.append(("Final Answer", final_data, thinking_time))
85+
86+
yield steps, total_thinking_time

0 commit comments

Comments
 (0)