Give me a script (which uses local llm models through ollama backend) to debate on a topic. Make 3 agents, one from supoorting team, one from opposing and one to give points on each side based on their arguments.
Assistant
Certainly! Below is a Python script that sets up a structured debate using three local language model agents through the Ollama backend:
Supporting Team Agent: Presents arguments in favor of the topic.
Opposing Team Agent: Presents arguments against the topic.
Evaluator Agent: Provides points based on the arguments presented by both sides.
Prerequisites
Ollama Installed: Ensure that Ollama is installed on your system and that the desired language models are available locally.
Python Environment: Ensure you have Python 3.7+ installed.
Dependencies: The script uses Python's subprocess and time modules, which are part of the standard library.
Script Overview
Functionality:
run_ollama_model: A helper function to interact with the Ollama CLI to get responses from the local models.
Debate Session: The script orchestrates a debate on a user-defined topic between the supporting and opposing agents, with the evaluator providing feedback after each round.
Customization:
Models: Update the SUPPORT_MODEL, OPPOSE_MODEL, and EVALUATOR_MODEL variables with the names of the local models you wish to use for each agent.
Topic: The debate topic can be set by modifying the TOPIC variable.
Python Script
python
import subprocess
import time
# Configuration: Define your models and topic here
SUPPORT_MODEL = "gpt-4-support" # Replace with your supporting model name
OPPOSE_MODEL = "gpt-4-oppose" # Replace with your opposing model name
EVALUATOR_MODEL = "gpt-4-evaluator" # Replace with your evaluator model name
TOPIC = "The impact of remote work on productivity"
# Function to interact with Ollama models
def run_ollama_model(model_name, prompt, max_tokens=200):
"""
Runs a local Ollama model with the given prompt.
Args:
model_name (str): The name of the Ollama model to use.
prompt (str): The input prompt for the model.
max_tokens (int): Maximum number of tokens in the response.
Returns:
str: The model's response.
"""
try:
completed_process = subprocess.run(
[
"ollama",
"run",
model_name,
"--prompt",
prompt,
"--max-tokens",
str(max_tokens)
],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
return completed_process.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error running model {model_name}: {e.stderr}")
return ""
# Agent Prompts
def get_support_prompt(topic, history):
return f"You are a supporting agent in a debate. Provide your next argument on the topic: '{topic}'. Here is the debate history:\n{history}"
def get_oppose_prompt(topic, history):
return f"You are an opposing agent in a debate. Provide your next argument against the topic: '{topic}'. Here is the debate history:\n{history}"
def get_evaluator_prompt(topic, history):
return f"You are an impartial evaluator. Based on the debate topic: '{topic}', and the following debate history:\n{history}\nProvide points for each side so far."
# Initialize debate history
debate_history = ""
# Number of rounds
ROUNDS = 3
print(f"Debate Topic: {TOPIC}\n")
for round_num in range(1, ROUNDS + 1):
print(f"--- Round {round_num} ---\n")
# Supporting Agent makes a point
support_prompt = get_support_prompt(TOPIC, debate_history)
support_response = run_ollama_model(SUPPORT_MODEL, support_prompt)
print(f"Supporting Team:\n{support_response}\n")
debate_history += f"Supporting Team: {support_response}\n"
time.sleep(1) # Optional: Pause for readability
# Opposing Agent makes a point
oppose_prompt = get_oppose_prompt(TOPIC, debate_history)
oppose_response = run_ollama_model(OPPOSE_MODEL, oppose_prompt)
print(f"Opposing Team:\n{oppose_response}\n")
debate_history += f"Opposing Team: {oppose_response}\n"
time.sleep(1) # Optional: Pause for readability
# Evaluator provides points
evaluator_prompt = get_evaluator_prompt(TOPIC, debate_history)
evaluator_response = run_ollama_model(EVALUATOR_MODEL, evaluator_prompt)
print(f"Evaluator:\n{evaluator_response}\n")
debate_history += f"Evaluator: {evaluator_response}\n"
print("\n")
time.sleep(2) # Optional: Pause before next round
print("Debate concluded.")
How to Use the Script
Install Ollama and Models: Ensure that Ollama is installed and that you have the required models (gpt-4-support, gpt-4-oppose, gpt-4-evaluator) locally. Replace these names in the script with the actual names of your models if they differ.
Set the Topic: Modify the TOPIC variable to set the debate subject.
Run the Script: Save the script to a file, for example, debate.py, and execute it using Python:
bash
python debate.py
Observe the Output: The script will print the debate rounds, showing arguments from both sides and evaluator feedback after each round.
Example Output
code
Debate Topic: The impact of remote work on productivity
--- Round 1 ---
Supporting Team:
Remote work offers employees greater flexibility, which can lead to increased job satisfaction and, consequently, higher productivity. Without the daily commute, employees can allocate more time to their tasks and maintain a better work-life balance.
Opposing Team:
While flexibility is beneficial, remote work can lead to feelings of isolation and difficulties in team collaboration. The lack of in-person interaction may reduce spontaneous idea sharing and slow down project progress.
Evaluator:
Supporting Team: 2 points for flexibility and increased job satisfaction.
Opposing Team: 2 points for team collaboration challenges and potential isolation.
--- Round 2 ---
Supporting Team:
Moreover, remote work allows companies to tap into a global talent pool, enabling them to hire the best talent regardless of geographical constraints. This diversity can drive innovation and enhance productivity.
Opposing Team:
However, managing a distributed workforce can be challenging. Time zone differences and varying work environments may lead to miscommunication and decreased efficiency.
Evaluator:
Supporting Team: 2 points for access to global talent and diversity-driven innovation.
Opposing Team: 2 points for management challenges and potential miscommunication.
--- Round 3 ---
Supporting Team:
Additionally, remote work can lead to cost savings for both employers and employees. Companies can reduce overhead costs related to office space, while employees save on commuting expenses, which can contribute to a more focused and productive workforce.
Opposing Team:
On the flip side, remote work requires investment in technology and cybersecurity measures. Without proper infrastructure, the benefits may be negated by technical issues and security vulnerabilities.
Evaluator:
Supporting Team: 2 points for cost savings and reduced overhead.
Opposing Team: 2 points for technology and cybersecurity investment requirements.
Debate concluded.
Notes and Customizations
Model Responses: The quality and style of the debate depend on the capabilities of the selected models. Ensure that the models are fine-tuned or designed for debate purposes if needed.
Error Handling: The script includes basic error handling for subprocess calls. For a more robust implementation, consider adding retries or more detailed error messages.
Extensibility: You can extend the script to include more rounds, additional agents, or more complex interaction patterns based on your requirements.
Timing Adjustments: The time.sleep() calls are optional and are added to improve the readability of the output. Adjust the durations as needed.
Logging: For more advanced use cases, consider integrating logging mechanisms to record the debate history to a file or database.
Conclusion
This script provides a foundational framework for orchestrating a debate using local language model agents via Ollama. By customizing the models and prompts, you can adapt it to various topics and use cases. Ensure that the local models are appropriately trained or configured to handle debate-style interactions for optimal results.