Created
November 21, 2024 22:53
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This Python script facilitates an AI-powered debate where an initial prompt is provided, | |
and the AI generates responses and counterpoints to its own statements in a loop. | |
Using the Ollama API, it alternates between generating a reply and rephrasing it into a counterpoint, | |
simulating a conversational exchange. The script is ideal for exploring dynamic interactions | |
and encouraging creative or critical thinking in a structured yet automated way. | |
""" | |
import ollama | |
# https://ollama.com/ | |
# ollama run llama3.2:latest | |
# Settings | |
model = "llama3.2:latest" | |
client = ollama.Client(host="http://127.0.0.1:11434") | |
starter = "Lets have a debate with one sentence reponses about tater tots." | |
reply = "Generate a one-sentence counterpoint to this statement: " | |
print("====================================") | |
print("====================================") | |
print(f"Starter: {starter}") | |
print(f"Response: {reply}") | |
print("") | |
print("") | |
# Initial prompt | |
messages = [{ | |
"role": "user", | |
"content": starter | |
}] | |
def reword_sentence(client, model, sentence): | |
"""Function to reword a sentence using the AI model.""" | |
response = client.chat( | |
model=model, | |
messages=[{ | |
"role": "user", | |
"content": f"{reply}'{sentence}'" | |
}], | |
stream=False | |
) | |
return response["message"]["content"] | |
# Loop to continue the conversation | |
print("Starting self-conversation...") | |
print("") | |
for i in range(4): # Adjust the range for the number of exchanges | |
# Get AI response | |
response = client.chat( | |
model=model, | |
messages=messages, | |
stream=False | |
) | |
ai_message = response["message"]["content"] | |
# Print the conversation | |
print(f"Round {i + 1}:") | |
print(f"AI-1: {messages[-1]['content']}") | |
print(f"AI-2: {ai_message}") | |
print("==================================================") | |
# Reword the AI's response | |
reworded_message = reword_sentence(client, model, ai_message) | |
# print(f" Reworded: {reworded_message}") | |
# print("==================================================") | |
# Add the question as the next user input | |
messages.append({ | |
"role": "assistant", | |
"content": ai_message | |
}) | |
messages.append({ | |
"role": "user", | |
"content": reworded_message | |
}) | |
print("Conversation ended.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment