Skip to content

Instantly share code, notes, and snippets.

@georgewritescode
Created September 15, 2024 23:09
Show Gist options
  • Save georgewritescode/b4435d9da14d61269093a2fe14088e57 to your computer and use it in GitHub Desktop.
Save georgewritescode/b4435d9da14d61269093a2fe14088e57 to your computer and use it in GitHub Desktop.
groq step by step resoning test
import groq
import json
import time
import os
import dotenv
dotenv.load_dotenv()
client = groq.Groq(api_key=os.environ["GROQ_API_KEY"])
def make_api_call(messages, max_tokens, temperature=0.2, is_final_answer=False):
"""
Makes an API call to the chat model and retries up to 3 times in case of failure.
Args:
messages (list): List of messages to send to the model.
max_tokens (int): Maximum number of tokens to generate in the response.
temperature (float): Temperature for sampling.
is_final_answer (bool): Flag indicating if this is the final answer attempt.
Returns:
dict: The model's response in JSON format.
"""
for attempt in range(5):
try:
response = client.chat.completions.create(
model="llama-3.1-70b-versatile",
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
response_format={"type": "json_object"}
)
return json.loads(response.choices[0].message.content)
except Exception as e:
if attempt == 4:
if is_final_answer:
return {"title": "Error", "content": f"Failed to generate final answer after 5 attempts. Error: {str(e)}"}
else:
return {"title": "Error", "content": f"Failed to generate step after 5 attempts. Error: {str(e)}", "next_action": "final_answer"}
time.sleep(1) # Wait for 1 second before retrying
def generate_response(prompt, max_tokens=4096, temperature=0.2, top_p=1, seed=42):
"""
Generates a response using the AI model, reasoning step-by-step until a final answer is reached.
Args:
prompt (str): The user prompt.
max_tokens (int): The maximum number of tokens for each step.
temperature (float): Temperature for sampling.
Returns:
tuple: A tuple containing the final answer and the total thinking time.
"""
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."}
]
total_thinking_time = 0
while True:
start_time = time.time()
step_data = make_api_call(messages, max_tokens, temperature)
end_time = time.time()
thinking_time = end_time - start_time
total_thinking_time += thinking_time
if step_data['next_action'] == 'final_answer':
break
messages.append({"role": "assistant", "content": json.dumps(step_data)})
# 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, temperature, is_final_answer=True)
end_time = time.time()
thinking_time = end_time - start_time
total_thinking_time += thinking_time
return final_data['content'], total_thinking_time
def test_model(
system_prompt,
user_prompt,
max_tokens,
temperature,
top_p,
seed,
):
# Use system and user prompts to craft initial messages
prompt = f"{system_prompt}\n\n{user_prompt}"
# Call generate_response to get the step-by-step reasoning
final_answer, total_thinking_time = generate_response(prompt, max_tokens, temperature, top_p, seed)
print(final_answer)
print(total_thinking_time)
return final_answer
if __name__ == "__main__":
response = test_model(
system_prompt="You are an expert assistant.",
user_prompt="What is the capital of France?",
max_tokens=4096,
temperature=0.2,
top_p=0.9,
seed=42,
)
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment