mirror of
https://github.com/tcsenpai/multi1.git
synced 2025-06-06 02:55:21 +00:00
Perplexity fork
This commit is contained in:
parent
555ac3d3ee
commit
9d7881a708
@ -1,6 +1,6 @@
|
|||||||
# ol1: Using Ollama to create o1-like reasoning chains
|
# ol1: Using Perplexity to create o1-like reasoning chains
|
||||||
|
|
||||||
* IMPORTANT: This repository is a fork of [bklieger-groq](https://github.com/bklieger-groq)'s [ol1](https://github.com/bklieger-groq/ol1) with the intention to offer a privacy-friendly local alternative to their work.
|
* IMPORTANT: This repository is a fork of [bklieger-groq](https://github.com/bklieger-groq)'s [ol1](https://github.com/bklieger-groq/ol1) with the intention to offer a Perplexity-based alternative to their work.
|
||||||
|
|
||||||
*** Note: This README is a modified version of the original README from [bklieger-groq](https://github.com/bklieger-groq)'s [ol1](https://github.com/bklieger-groq/ol1) repository. It may contains inaccuracies. ***
|
*** Note: This README is a modified version of the original README from [bklieger-groq](https://github.com/bklieger-groq)'s [ol1](https://github.com/bklieger-groq/ol1) repository. It may contains inaccuracies. ***
|
||||||
|
|
||||||
@ -14,13 +14,13 @@ ol1 demonstrates the potential of prompting alone to overcome straightforward LL
|
|||||||
|
|
||||||
### How it works
|
### How it works
|
||||||
|
|
||||||
ol1 powered by local Ollama models and creates reasoning chains, in principle a dynamic Chain of Thought, that allows the LLM to "think" and solve some logical problems that usually otherwise stump leading models.
|
ol1 powered by local Perplexity models and creates reasoning chains, in principle a dynamic Chain of Thought, that allows the LLM to "think" and solve some logical problems that usually otherwise stump leading models.
|
||||||
|
|
||||||
At each step, the LLM can choose to continue to another reasoning step, or provide a final answer. Each step is titled and visible to the user. The system prompt also includes tips for the LLM. There is a full explanation under Prompt Breakdown, but a few examples are asking the model to “include exploration of alternative answers” and “use at least 3 methods to derive the answer”.
|
At each step, the LLM can choose to continue to another reasoning step, or provide a final answer. Each step is titled and visible to the user. The system prompt also includes tips for the LLM. There is a full explanation under Prompt Breakdown, but a few examples are asking the model to “include exploration of alternative answers” and “use at least 3 methods to derive the answer”.
|
||||||
|
|
||||||
### Features of this fork
|
### Features of this fork
|
||||||
|
|
||||||
* Runs on local Ollama models
|
* Runs on local Perplexity models
|
||||||
* Fully configurable via .env file
|
* Fully configurable via .env file
|
||||||
|
|
||||||
### Original benchmarks with g1
|
### Original benchmarks with g1
|
||||||
|
184
app.py
184
app.py
@ -9,37 +9,89 @@ import os
|
|||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# Get configuration from .env file
|
# Get configuration from .env file
|
||||||
OLLAMA_URL = os.getenv('OLLAMA_URL', 'http://localhost:11434')
|
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")
|
||||||
OLLAMA_MODEL = os.getenv('OLLAMA_MODEL', 'llama2')
|
PERPLEXITY_MODEL = os.getenv("PERPLEXITY_MODEL", "llama-3.1-sonar-small-128k-online")
|
||||||
|
|
||||||
|
if not PERPLEXITY_API_KEY:
|
||||||
|
raise ValueError("PERPLEXITY_API_KEY is not set in the .env file")
|
||||||
|
|
||||||
|
|
||||||
def make_api_call(messages, max_tokens, is_final_answer=False):
|
def make_api_call(messages, max_tokens, is_final_answer=False):
|
||||||
for attempt in range(3):
|
for attempt in range(3):
|
||||||
try:
|
try:
|
||||||
response = requests.post(
|
url = "https://api.perplexity.ai/chat/completions"
|
||||||
f"{OLLAMA_URL}/api/chat",
|
|
||||||
json={
|
payload = {"model": PERPLEXITY_MODEL, "messages": messages}
|
||||||
"model": OLLAMA_MODEL,
|
headers = {
|
||||||
"messages": messages,
|
"Authorization": f"Bearer {PERPLEXITY_API_KEY}",
|
||||||
"stream": False,
|
"Content-Type": "application/json",
|
||||||
"options": {
|
}
|
||||||
"num_predict": max_tokens,
|
|
||||||
"temperature": 0.2
|
print(f"payload: {payload}")
|
||||||
}
|
|
||||||
}
|
response = requests.request("POST", url, json=payload, headers=headers)
|
||||||
)
|
|
||||||
|
print(f"Response status code: {response.status_code}")
|
||||||
|
print(f"Response content: {response.text}")
|
||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return json.loads(response.json()["message"]["content"])
|
response_json = response.json()
|
||||||
except Exception as e:
|
content = response_json["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
|
# Try to parse the content as JSON
|
||||||
|
try:
|
||||||
|
return json.loads(content)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# If parsing fails, return the content as is
|
||||||
|
return {
|
||||||
|
"title": "Raw Response",
|
||||||
|
"content": content,
|
||||||
|
"next_action": "final_answer" if is_final_answer else "continue"
|
||||||
|
}
|
||||||
|
|
||||||
|
except requests.exceptions.HTTPError as e:
|
||||||
|
if response.status_code == 400:
|
||||||
|
error_message = f"400 Bad Request: {response.text}"
|
||||||
|
print(error_message)
|
||||||
|
if attempt == 2:
|
||||||
|
return {
|
||||||
|
"title": "Error",
|
||||||
|
"content": error_message,
|
||||||
|
"next_action": "final_answer",
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
# Handle other HTTP errors
|
||||||
|
if attempt == 2:
|
||||||
|
error_message = f"HTTP error occurred: {str(e)}"
|
||||||
|
return {
|
||||||
|
"title": "Error",
|
||||||
|
"content": error_message,
|
||||||
|
"next_action": "final_answer",
|
||||||
|
}
|
||||||
|
except json.JSONDecodeError:
|
||||||
if attempt == 2:
|
if attempt == 2:
|
||||||
if is_final_answer:
|
return {
|
||||||
return {"title": "Error", "content": f"Failed to generate final answer after 3 attempts. Error: {str(e)}"}
|
"title": "Error",
|
||||||
else:
|
"content": f"Failed to parse API response: {response.text}",
|
||||||
return {"title": "Error", "content": f"Failed to generate step after 3 attempts. Error: {str(e)}", "next_action": "final_answer"}
|
"next_action": "final_answer",
|
||||||
time.sleep(1) # Wait for 1 second before retrying
|
}
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
if attempt == 2:
|
||||||
|
error_message = f"API request failed after 3 attempts. Error: {str(e)}"
|
||||||
|
return {
|
||||||
|
"title": "Error",
|
||||||
|
"content": error_message,
|
||||||
|
"next_action": "final_answer",
|
||||||
|
}
|
||||||
|
time.sleep(1) # Wait for 1 second before retrying
|
||||||
|
|
||||||
|
|
||||||
def generate_response(prompt):
|
def generate_response(prompt):
|
||||||
|
|
||||||
messages = [
|
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.
|
{
|
||||||
|
"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:
|
Example of a valid JSON response:
|
||||||
```json
|
```json
|
||||||
@ -48,87 +100,113 @@ Example of a valid JSON response:
|
|||||||
"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...",
|
"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"
|
"next_action": "continue"
|
||||||
}```
|
}```
|
||||||
"""},
|
""",
|
||||||
|
},
|
||||||
{"role": "user", "content": prompt},
|
{"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 = []
|
steps = []
|
||||||
step_count = 1
|
step_count = 1
|
||||||
total_thinking_time = 0
|
total_thinking_time = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
step_data = make_api_call(messages, 300)
|
step_data = 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((f"Step {step_count}: {step_data['title']}", step_data['content'], 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)})
|
messages.append({"role": "assistant", "content": json.dumps(step_data)})
|
||||||
|
|
||||||
if step_data['next_action'] == 'final_answer':
|
if step_data["next_action"] == "final_answer":
|
||||||
break
|
break
|
||||||
|
|
||||||
step_count += 1
|
step_count += 1
|
||||||
|
|
||||||
|
# Add a user message to maintain alternation
|
||||||
|
messages.append({"role": "user", "content": "Continue with the next step."})
|
||||||
|
|
||||||
# Yield after each step for Streamlit to update
|
# Yield after each step for Streamlit to update
|
||||||
yield steps, None # We're not yielding the total time until the end
|
yield steps, None # We're not yielding the total time until the end
|
||||||
|
|
||||||
# Generate final answer
|
# Generate final answer
|
||||||
messages.append({"role": "user", "content": "Please provide the final answer based on your reasoning above."})
|
messages.append(
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": "Please provide the final answer based on your reasoning above.",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
final_data = make_api_call(messages, 200, is_final_answer=True)
|
final_data = 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
|
||||||
|
|
||||||
steps.append(("Final Answer", final_data['content'], thinking_time))
|
steps.append(("Final Answer", final_data["content"], thinking_time))
|
||||||
|
|
||||||
yield steps, total_thinking_time
|
yield steps, total_thinking_time
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
|
st.set_page_config(page_title="g1 prototype", page_icon="🧠", layout="wide")
|
||||||
|
|
||||||
st.title("ol1: Using Ollama to create o1-like reasoning chains")
|
st.title("pl1: Using Perplexity AI to create o1-like reasoning chains")
|
||||||
|
|
||||||
st.markdown("""
|
st.markdown(
|
||||||
This is an early prototype of using prompting to create o1-like reasoning chains to improve output accuracy. It is not perfect and accuracy has yet to be formally evaluated. It is powered by Ollama so that the reasoning step is local!
|
"""
|
||||||
|
This is an early prototype of using prompting to create o1-like reasoning chains to improve output accuracy. It is not perfect and accuracy has yet to be formally evaluated. It is powered by Perplexity AI API!
|
||||||
|
|
||||||
Forked from [bklieger-groq](https://github.com/bklieger-groq)
|
Forked from [bklieger-groq](https://github.com/bklieger-groq)
|
||||||
Open source [repository here](https://github.com/tcsenpai/ol1)
|
Open source [repository here](https://github.com/tcsenpai/ol1)
|
||||||
""")
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
st.markdown(f"**Current Configuration:**")
|
st.markdown(f"**Current Configuration:**")
|
||||||
st.markdown(f"- Ollama URL: `{OLLAMA_URL}`")
|
st.markdown(f"- Perplexity AI Model: `{PERPLEXITY_MODEL}`")
|
||||||
st.markdown(f"- Ollama Model: `{OLLAMA_MODEL}`")
|
|
||||||
|
|
||||||
# Text input for user query
|
# 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?")
|
user_query = st.text_input(
|
||||||
|
"Enter your query:",
|
||||||
|
placeholder="e.g., How many 'R's are in the word strawberry?",
|
||||||
|
)
|
||||||
|
|
||||||
if user_query:
|
if user_query:
|
||||||
st.write("Generating response...")
|
st.write("Generating response...")
|
||||||
|
|
||||||
# Create empty elements to hold the generated text and total time
|
# Create empty elements to hold the generated text and total time
|
||||||
response_container = st.empty()
|
response_container = st.empty()
|
||||||
time_container = st.empty()
|
time_container = st.empty()
|
||||||
|
|
||||||
# Generate and display the response
|
# Generate and display the response
|
||||||
for steps, total_thinking_time in generate_response(user_query):
|
for steps, total_thinking_time in generate_response(user_query):
|
||||||
with response_container.container():
|
with response_container.container():
|
||||||
for i, (title, content, thinking_time) in enumerate(steps):
|
for i, (title, content, thinking_time) in enumerate(steps):
|
||||||
if title.startswith("Final Answer"):
|
if title.startswith("Final Answer"):
|
||||||
st.markdown(f"### {title}")
|
st.markdown(f"### {title}")
|
||||||
st.markdown(content.replace('\n', '<br>'), unsafe_allow_html=True)
|
st.markdown(
|
||||||
|
content.replace("\n", "<br>"), unsafe_allow_html=True
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
with st.expander(title, expanded=True):
|
with st.expander(title, expanded=True):
|
||||||
st.markdown(content.replace('\n', '<br>'), unsafe_allow_html=True)
|
st.markdown(
|
||||||
|
content.replace("\n", "<br>"), unsafe_allow_html=True
|
||||||
|
)
|
||||||
|
|
||||||
# Only show total time when it's available at the end
|
# Only show total time when it's available at the end
|
||||||
if total_thinking_time is not None:
|
if total_thinking_time is not None:
|
||||||
time_container.markdown(f"**Total thinking time: {total_thinking_time:.2f} seconds**")
|
time_container.markdown(
|
||||||
|
f"**Total thinking time: {total_thinking_time:.2f} seconds**"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
streamlit
|
streamlit
|
||||||
dotenv
|
python-dotenv
|
||||||
requests
|
requests
|
Loading…
x
Reference in New Issue
Block a user