improved original prompt

This commit is contained in:
tcsenpai 2024-09-17 21:03:27 +02:00
parent 213bd49ce8
commit 669e3e205c

View File

@ -2,72 +2,72 @@ import json
import time import time
import os import os
def generate_response(prompt, api_handler): def generate_response(prompt, api_handler):
# Load the system prompt from an external file
with open('system_prompt.txt', 'r') as file:
SYSTEM_PROMPT = file.read()
# Initialize the conversation with system prompt, user input, and an initial assistant response
messages = [ messages = [
{ {"role": "system", "content": SYSTEM_PROMPT},
"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.""",
},
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
{ {"role": "assistant", "content": "Understood. I will now create a detailed reasoning chain following the given instructions, starting with a thorough problem decomposition."},
"role": "assistant",
"content": "Thank you! I will now think step by step following my instructions, starting at the beginning after decomposing the problem.",
},
] ]
steps = [] steps = []
step_count = 1 step_count = 1
total_thinking_time = 0 total_thinking_time = 0
# Main loop for generating reasoning steps
while True: while True:
# Measure time taken for each API call
start_time = time.time() start_time = time.time()
step_data = api_handler.make_api_call(messages, 300) step_data = api_handler.make_api_call(messages, 300)
end_time = time.time() end_time = time.time()
thinking_time = end_time - start_time thinking_time = end_time - start_time
total_thinking_time += thinking_time total_thinking_time += thinking_time
steps.append( # Store each step's information
( steps.append((f"Step {step_count}: {step_data['title']}", step_data["content"], thinking_time))
f"Step {step_count}: {step_data['title']}",
step_data["content"],
thinking_time,
)
)
# Add the assistant's response to the conversation
messages.append({"role": "assistant", "content": json.dumps(step_data)}) messages.append({"role": "assistant", "content": json.dumps(step_data)})
print("Next reasoning step: ", step_data["next_action"]) print("Next reasoning step: ", step_data["next_action"])
if step_data["next_action"].lower().strip() == "final_answer" or step_count > 10: # Prevents infinite loops in case of errors.
# Break the loop if it's the final answer or if step count exceeds 10
if step_data["next_action"].lower().strip() == "final_answer" or step_count > 10:
break break
step_count += 1 step_count += 1
# Yield intermediate results
yield steps, None yield steps, None
messages.append( # Request final answer
{ messages.append({
"role": "user", "role": "user",
"content": "Please provide the final answer based on your reasoning above.", "content": "Please provide the final answer based on your reasoning above.",
} })
)
# Generate and time the final answer
start_time = time.time() start_time = time.time()
final_data = api_handler.make_api_call(messages, 200, is_final_answer=True) final_data = api_handler.make_api_call(messages, 200, is_final_answer=True)
end_time = time.time() end_time = time.time()
thinking_time = end_time - start_time thinking_time = end_time - start_time
total_thinking_time += thinking_time total_thinking_time += thinking_time
# Add final answer to steps
steps.append(("Final Answer", final_data["content"], thinking_time)) steps.append(("Final Answer", final_data["content"], thinking_time))
# Yield final results
yield steps, total_thinking_time yield steps, total_thinking_time
def load_env_vars(): def load_env_vars():
# Load environment variables with default values
return { return {
"OLLAMA_URL": os.getenv("OLLAMA_URL", "http://localhost:11434"), "OLLAMA_URL": os.getenv("OLLAMA_URL", "http://localhost:11434"),
"OLLAMA_MODEL": os.getenv("OLLAMA_MODEL", "llama3.1:70b"), "OLLAMA_MODEL": os.getenv("OLLAMA_MODEL", "llama3.1:70b"),
"PERPLEXITY_API_KEY": os.getenv("PERPLEXITY_API_KEY"), "PERPLEXITY_API_KEY": os.getenv("PERPLEXITY_API_KEY"),
"PERPLEXITY_MODEL": os.getenv( "PERPLEXITY_MODEL": os.getenv("PERPLEXITY_MODEL", "llama-3.1-sonar-small-128k-online"),
"PERPLEXITY_MODEL", "llama-3.1-sonar-small-128k-online"
),
} }