Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,13 @@ import os
|
|
4 |
import json
|
5 |
import time
|
6 |
|
7 |
-
client
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def make_api_call(messages, max_tokens, is_final_answer=False):
|
10 |
for attempt in range(3):
|
@@ -25,57 +31,7 @@ def make_api_call(messages, max_tokens, is_final_answer=False):
|
|
25 |
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
|
26 |
time.sleep(1) # Wait for 1 second before retrying
|
27 |
|
28 |
-
|
29 |
-
messages = [
|
30 |
-
{"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.
|
31 |
-
|
32 |
-
Example of a valid JSON response:
|
33 |
-
```json
|
34 |
-
{
|
35 |
-
"title": "Identifying Key Information",
|
36 |
-
"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...",
|
37 |
-
"next_action": "continue"
|
38 |
-
}```
|
39 |
-
"""},
|
40 |
-
{"role": "user", "content": prompt},
|
41 |
-
{"role": "assistant", "content": "Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem."}
|
42 |
-
]
|
43 |
-
|
44 |
-
steps = []
|
45 |
-
step_count = 1
|
46 |
-
total_thinking_time = 0
|
47 |
-
|
48 |
-
while True:
|
49 |
-
start_time = time.time()
|
50 |
-
step_data = make_api_call(messages, 300)
|
51 |
-
end_time = time.time()
|
52 |
-
thinking_time = end_time - start_time
|
53 |
-
total_thinking_time += thinking_time
|
54 |
-
|
55 |
-
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
|
56 |
-
|
57 |
-
messages.append({"role": "assistant", "content": json.dumps(step_data)})
|
58 |
-
|
59 |
-
if step_data['next_action'] == 'final_answer' or step_count > 25: # Maximum of 25 steps to prevent infinite thinking time. Can be adjusted.
|
60 |
-
break
|
61 |
-
|
62 |
-
step_count += 1
|
63 |
-
|
64 |
-
# Yield after each step for Streamlit to update
|
65 |
-
yield steps, None # We're not yielding the total time until the end
|
66 |
-
|
67 |
-
# Generate final answer
|
68 |
-
messages.append({"role": "user", "content": "Please provide the final answer based on your reasoning above."})
|
69 |
-
|
70 |
-
start_time = time.time()
|
71 |
-
final_data = make_api_call(messages, 200, is_final_answer=True)
|
72 |
-
end_time = time.time()
|
73 |
-
thinking_time = end_time - start_time
|
74 |
-
total_thinking_time += thinking_time
|
75 |
-
|
76 |
-
steps.append(("Final Answer", final_data['content'], thinking_time))
|
77 |
-
|
78 |
-
yield steps, total_thinking_time
|
79 |
|
80 |
def main():
|
81 |
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
|
@@ -88,6 +44,20 @@ def main():
|
|
88 |
Open source [repository here](https://github.com/bklieger-groq)
|
89 |
""")
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
# Text input for user query
|
92 |
user_query = st.text_input("Enter your query:", placeholder="e.g., How many 'R's are in the word strawberry?")
|
93 |
|
|
|
4 |
import json
|
5 |
import time
|
6 |
|
7 |
+
# Initialize the Groq client with the API key from Hugging Face secrets
|
8 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
9 |
+
if not api_key:
|
10 |
+
st.error("No Groq API key found. Please add your GROQ_API_KEY to the Hugging Face Space secrets.")
|
11 |
+
st.stop()
|
12 |
+
|
13 |
+
client = groq.Groq(api_key=api_key)
|
14 |
|
15 |
def make_api_call(messages, max_tokens, is_final_answer=False):
|
16 |
for attempt in range(3):
|
|
|
31 |
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
|
32 |
time.sleep(1) # Wait for 1 second before retrying
|
33 |
|
34 |
+
# ... [rest of the code remains the same] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def main():
|
37 |
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
|
|
|
44 |
Open source [repository here](https://github.com/bklieger-groq)
|
45 |
""")
|
46 |
|
47 |
+
if not api_key:
|
48 |
+
st.warning("Please add your GROQ_API_KEY to the Hugging Face Space secrets to use this application.")
|
49 |
+
st.markdown("""
|
50 |
+
To add your API key:
|
51 |
+
1. Go to the Settings tab of this Space
|
52 |
+
2. Scroll down to the "Repository secrets" section
|
53 |
+
3. Click on "New secret"
|
54 |
+
4. Set the secret name as `GROQ_API_KEY`
|
55 |
+
5. Paste your Groq API key as the value
|
56 |
+
6. Click "Add secret"
|
57 |
+
7. Rebuild the Space
|
58 |
+
""")
|
59 |
+
st.stop()
|
60 |
+
|
61 |
# Text input for user query
|
62 |
user_query = st.text_input("Enter your query:", placeholder="e.g., How many 'R's are in the word strawberry?")
|
63 |
|