initial push

This commit is contained in:
Benjamin Klieger 2024-09-13 14:48:50 -07:00
parent fa43ceb268
commit a565a6b599
6 changed files with 143 additions and 2 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.env
venv
.venv

View File

@ -1,2 +1,18 @@
# g1
Early Prototype of g1: Using Llama-3.1 70b on Groq to create o1-like reasoning chains
# g1: Early Prototype of Using Llama-3.1 70b on Groq to create o1-like reasoning chains
This is an early prototype of using prompting strategies to improve the LLM's reasoning capabilities through o1-like reasoning chains. This allows the LLM to "think" and solve logical problems that usually otherwise stump leading models. Unlike o1, all the reasoning tokens are shown.
### Examples
> [!IMPORTANT]
> g1 is not perfect, but seems to perform significantly better than LLMs out-of-the-box. From initial testing, g1 accurately solves logic problems 60-80% of the time that usually stump LLMs. See examples below.
##### How many Rs are in strawberry?
Prompt: How many Rs are in strawberry?
Result:
[Strawberry example](examples/strawberry.png)
### Prompting Strategy

120
app.py Normal file
View File

@ -0,0 +1,120 @@
import streamlit as st
import groq
import os
import json
import time
# Set up Groq client
client = groq.Groq()
def make_api_call(messages, max_tokens, is_final_answer=False):
for attempt in range(3): # Try up to 3 times
try:
response = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=messages,
max_tokens=max_tokens,
temperature=0.2,
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
except Exception as e:
if attempt == 2: # If this was the last attempt
if is_final_answer:
return {"title": "Error", "content": f"Failed to generate final answer after 3 attempts. Error: {str(e)}"}
else:
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
time.sleep(1) # Wait for 1 second before retrying
def generate_response(prompt):
messages = [
{"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.
Example of a valid JSON response:
```json
{
"title": "Identifying Key Information",
"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...",
"next_action": "continue"
}```
"""},
{"role": "user", "content": prompt},
{"role": "assistant", "content": "Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem."}
]
steps = []
step_count = 1
total_thinking_time = 0
while True:
start_time = time.time()
step_data = make_api_call(messages, 300)
end_time = time.time()
thinking_time = end_time - start_time
total_thinking_time += thinking_time
steps.append((f"Step {step_count}: {step_data['title']}", step_data['content'], thinking_time))
messages.append({"role": "assistant", "content": json.dumps(step_data)})
if step_data['next_action'] == 'final_answer':
break
step_count += 1
# Yield after each step for Streamlit to update
yield steps, None # We're not yielding the total time until the end
# Generate final answer
messages.append({"role": "user", "content": "Please provide the final answer based on your reasoning above."})
start_time = time.time()
final_data = make_api_call(messages, 200, is_final_answer=True)
end_time = time.time()
thinking_time = end_time - start_time
total_thinking_time += thinking_time
steps.append(("Final Answer", final_data['content'], thinking_time))
yield steps, total_thinking_time
def main():
st.set_page_config(page_title="Groq Dynamic Reasoning Chain", page_icon="🧠", layout="wide")
st.title("Early Prototype of g1: Using Llama-3.1 70b on Groq to create o1-like reasoning chains")
st.markdown("""
This is an early prototype of using prompting to create o1-like reasoning chains to improve output accuracy. It is not perfect, it seems to be accurate on about 60-80% of runs on logic problems leading LLMs typically get right 0-20% of the time. It is powered by Groq so that the reasoning step is fast!
Created by @benjaminklieger, open sourced here:
""")
# Text input for user query
user_query = st.text_input("Enter your query:", placeholder="e.g., How many 'R's are in the word strawberry?")
if user_query:
st.write("Generating response...")
# Create empty elements to hold the generated text and total time
response_container = st.empty()
time_container = st.empty()
# Generate and display the response
for steps, total_thinking_time in generate_response(user_query):
with response_container.container():
for i, (title, content, thinking_time) in enumerate(steps):
if title.startswith("Final Answer"):
st.markdown(f"### {title}")
st.markdown(content)
else:
with st.expander(title, expanded=True):
st.markdown(content)
# Only show total time when it's available (i.e., at the end)
if total_thinking_time is not None:
time_container.markdown(f"**Total thinking time: {total_thinking_time:.2f} seconds**")
time.sleep(0.1) # Add a small delay to make the step-by-step effect visible
if __name__ == "__main__":
main()

0
example.env Normal file
View File

BIN
examples/strawberry.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
streamlit
groq