Autonoumous multiagent frame system able to execute code in local computer with memory and tool & function usage
Jarkko Iso-Kuortti
Smiling engineer, Lead Information Technology Specialist @ Q-Factory Oy | ITIL, ScrumMaster
PRE WORK after you have retrieved code from github:
API_KEY_OPENAI=YOURAPICODEHERE
API_KEY_ANTHROPIC=YOURAPICODEHERE
API_KEY_GROQ=YOURAPICODEHERE
API_KEY_PERPLEXITY=YOURAPICODEHERE
API_KEY_GOOGLE=YOURAPICODEHERE
TOKENIZERS_PARALLELISM=true
PYDEVD_DISABLE_FILE_VALIDATION=1
def initialize():
# main chat model used by agents (smarter, more accurate)
# chat_llm = models.get_groq_llama70b(temperature=0.2)
# chat_llm = models.get_groq_llama70b_json(temperature=0.2)
# chat_llm = models.get_groq_llama8b(temperature=0.2)
# chat_llm = models.get_openai_gpt35(temperature=0)
# chat_llm = models.get_openai_gpt4o(temperature=0)
chat_llm = models.get_openai_chat(temperature=0)
# chat_llm = models.get_anthropic_opus(temperature=0)
# chat_llm = models.get_anthropic_sonnet(temperature=0)
# chat_llm = models.get_anthropic_sonnet_35(temperature=0)
# chat_llm = models.get_anthropic_haiku(temperature=0)
# chat_llm = models.get_ollama_dolphin()
# chat_llm = models.get_ollama(model_name="gemma2:27b")
# chat_llm = models.get_ollama(model_name="llama3:8b-text-fp16")
# chat_llm = models.get_ollama(model_name="gemma2:latest")
# chat_llm = models.get_ollama(model_name="qwen:14b")
# chat_llm = models.get_google_chat()
Here’s a script designed to be run on a Linux (Ubuntu) system. This script will create a virtual environment, activate it, install the required Python packages from requirements.txt, and start Docker. I use wsl -d Ubuntu in my windows 11
Script: setup_and_start_docker.sh
#!/bin/bash
# Function to check if a command exists
command_exists() {
command -v "$1" &> /dev/null
}
# Check if Python3 is installed
if ! command_exists python3; then
echo "Python3 is not installed. Please install Python3 and try again."
exit 1
fi
# Check if pip is installed
if ! command_exists pip; then
echo "pip is not installed. Please install pip and try again."
exit 1
fi
# Check if Docker is installed
if ! command_exists docker; then
echo "Docker is not installed. Please install Docker and try again."
exit 1
fi
# Create Virtual Environment
echo "Creating virtual environment..."
python3 -m venv venv
# Activate Virtual Environment and Install Python Packages
echo "Activating virtual environment and installing packages..."
source venv/bin/activate
if [ -f requirements.txt ]; then
pip install -r requirements.txt
else
echo "requirements.txt not found"
fi
# Start Docker
echo "Starting Docker..."
sudo service docker start
# Check Docker Status
echo "Checking Docker status..."
sudo service docker status
echo "Setup complete."
Instructions to Use the Script
Important Notes
Troubleshooting
If you encounter any issues:
This script should help automate the process of setting up the Python environment and starting Docker within an Ubuntu system.
Executing framework
# Navigate to the project folder
cd /home/user/myproject
# Activate the virtual environment
source venv/bin/activate
# Ensure Docker is running
sudo service docker start
# Run the main.py script using python3
python3 main.py
领英推荐
Important Notes
By following these instructions, you should be able to navigate to your project folder and start main.py with the necessary environment and services running using python3.
Example how code is created and executed: in folder ./work_dir
I start to discuss with system and at some point paused... at this point the system creates "memoryes" of my actions (I already asked it to tellme current weather in Finland Helsinki and Sweden Stockholm, it failed since it did not have API code for the service it selected to use by its own, requesting the user to provide it)
Agent 0: Mid messages cleanup summary:
{
"system_info": "Messages have been summarized to save space.",
"messages_summary": [
"User confirmed the provided time method works for both Helsinki and Madrid.",
"User requested to compare the current weather in Helsinki and Stockholm.",
"AI suggested using various weather APIs, including OpenWeatherMap, Meteosource, Weatherbit, Visual Crossing, and Open-Meteo.",
"AI prepared to use the OpenWeatherMap API to retrieve weather data but required a valid API key.",
"AI noted it could not find a valid OpenWeatherMap API key in memory and requested the user to provide one."
]
}
I give my openweather api key to system (I will delete this key , dont you worry):
User message ('e' to leave):
> openweather API key = 918403ad1a1997bf8650f34d05c9c9a4
And then magic starts :) see especially when agent 1 makes and executes code in local computer docker container :) asked by Agent0 , and Agent0 delivering only the final answer
Agent 0: Starting a message:
{
"thoughts": [
"The user has provided a valid OpenWeatherMap API key.",
"I will now proceed to retrieve the current weather data for both Helsinki and Stockholm using this API key."
],
"tool_name": "call_subordinate",
"tool_args": {
"message": "Now that I have the API key, please retrieve the current weather data for Helsinki and Stockholm using the following API calls:\n1. For Helsinki: https://api.openweathermap.org/data/2.5/weather?lat=60.1699&lon=24.9384&appid=918403ad1a1997bf8650f34d05c9c9a4\n2. For Stockholm: https://api.openweathermap.org/data/2.5/weather?lat=59.3293&lon=18.0686&appid=918403ad1a1997bf8650f34d05c9c9a4.",
"reset": "false"
}
}
Agent 0: Using tool 'call_subordinate':
Message:
Now that I have the API key, please retrieve the current weather data for Helsinki and Stockholm using the following API calls:
1. For Helsinki: https://api.openweathermap.org/data/2.5/weather?lat=60.1699&lon=24.9384&appid=918403ad1a1997bf8650f34d05c9c9a4
2. For Stockholm: https://api.openweathermap.org/data/2.5/weather?lat=59.3293&lon=18.0686&appid=918403ad1a1997bf8650f34d05c9c9a4.
Reset: false
Agent 1: Starting a message:
{
"thoughts": [
"The user has provided a valid API key for the OpenWeatherMap API.",
"I will prepare the API calls for both Helsinki and Stockholm using the provided API key.",
"Next, I will execute the API calls to retrieve the current weather data."
],
"tool_name": "code_execution_tool",
"tool_args": {
"runtime": "python",
"code": "import requests\n\n# API key\napi_key = '918403ad1a1997bf8650f34d05c9c9a4'\n\n# URLs for the API calls\nhelsinki_url = f'https://api.openweathermap.org/data/2.5/weather?lat=60.1699&lon=24.9384&appid={api_key}'\nstockholm_url = f'https://api.openweathermap.org/data/2.5/weather?lat=59.3293&lon=18.0686&appid={api_key}'\n\n# Making the API calls\nhelsinki_response = requests.get(helsinki_url)\nstockholm_response = requests.get(stockholm_url)\n\n# Getting the JSON data\nhelsinki_data = helsinki_response.json()\nstockholm_data = stockholm_response.json()\n\n# Printing the results\nprint('Helsinki Weather:', helsinki_data)\nprint('Stockholm Weather:', stockholm_data)"
}
}
Agent 1: Using tool 'code_execution_tool':
Runtime: python
Code:
import requests
# API key
api_key = '918403ad1a1997bf8650f34d05c9c9a4'
# URLs for the API calls
helsinki_url = f'https://api.openweathermap.org/data/2.5/weather?lat=60.1699&lon=24.9384&appid={api_key}'
stockholm_url = f'https://api.openweathermap.org/data/2.5/weather?lat=59.3293&lon=18.0686&appid={api_key}'
# Making the API calls
helsinki_response = requests.get(helsinki_url)
stockholm_response = requests.get(stockholm_url)
# Getting the JSON data
helsinki_data = helsinki_response.json()
stockholm_data = stockholm_response.json()
# Printing the results
print('Helsinki Weather:', helsinki_data)
print('Stockholm Weather:', stockholm_data)
Agent 1 code execution output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'requests'
(venv) root@2c7cada58dbe:~#
above the agent 1 counters an error and starts to solve it below:
Agent 1: Starting a message:
{
"thoughts": [
"The execution failed because the 'requests' module is not installed in the environment.",
"I need to install the 'requests' library before I can make the API calls.",
"I will proceed to install the 'requests' library and then re-execute the API calls."
],
"tool_name": "code_execution_tool",
"tool_args": {
"runtime": "terminal",
"code": "pip install requests"
}
}
Agent 1: Using tool 'code_execution_tool':
Runtime: terminal
Code: pip install requests
Agent 1 code execution output:
Collecting requests
Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Collecting charset-normalizer<4,>=2 (from requests)
Downloading charset_normalizer-3.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (33 kB)
Collecting idna<4,>=2.5 (from requests)
Downloading idna-3.7-py3-none-any.whl.metadata (9.9 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
Downloading urllib3-2.2.2-py3-none-any.whl.metadata (6.4 kB)
Collecting certifi>=2017.4.17 (from requests)
Downloading certifi-2024.7.4-py3-none-any.whl.metadata (2.2 kB)
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.9/64.9 kB 2.9 MB/s eta 0:00:00
Downloading certifi-2024.7.4-py3-none-any.whl (162 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.0/163.0 kB 5.4 MB/s eta 0:00:00
Downloading charset_normalizer-3.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (140 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.3/140.3 kB 5.3 MB/s eta 0:00:00
Downloading idna-3.7-py3-none-any.whl (66 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.8/66.8 kB 5.3 MB/s eta 0:00:00
Downloading urllib3-2.2.2-py3-none-any.whl (121 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.4/121.4 kB 3.5 MB/s eta 0:00:00
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2024.7.4 charset-normalizer-3.3.2 idna-3.7 requests-2.32.3 urllib3-2.2.2
[notice] A new release of pip is available: 24.1.2 -> 24.2
[notice] To update, run: pip install --upgrade pip
(venv) root@2c7cada58dbe:~#
Problem solved Agent 1 proceees on task
Agent 1: Starting a message:
{
"thoughts": [
"The 'requests' library has been successfully installed.",
"Now I will re-execute the API calls to retrieve the current weather data for Helsinki and Stockholm."
],
"tool_name": "code_execution_tool",
"tool_args": {
"runtime": "python",
"code": "import requests\n\n# API key\napi_key = '918403ad1a1997bf8650f34d05c9c9a4'\n\n# URLs for the API calls\nhelsinki_url = f'https://api.openweathermap.org/data/2.5/weather?lat=60.1699&lon=24.9384&appid={api_key}'\nstockholm_url = f'https://api.openweathermap.org/data/2.5/weather?lat=59.3293&lon=18.0686&appid={api_key}'\n\n# Making the API calls\nhelsinki_response = requests.get(helsinki_url)\nstockholm_response = requests.get(stockholm_url)\n\n# Getting the JSON data\nhelsinki_data = helsinki_response.json()\nstockholm_data = stockholm_response.json()\n\n# Printing the results\nprint('Helsinki Weather:', helsinki_data)\nprint('Stockholm Weather:', stockholm_data)"
}
}
Agent 1: Using tool 'code_execution_tool':
Runtime: python
Code:
import requests
# API key
api_key = '918403ad1a1997bf8650f34d05c9c9a4'
# URLs for the API calls
helsinki_url = f'https://api.openweathermap.org/data/2.5/weather?lat=60.1699&lon=24.9384&appid={api_key}'
stockholm_url = f'https://api.openweathermap.org/data/2.5/weather?lat=59.3293&lon=18.0686&appid={api_key}'
# Making the API calls
helsinki_response = requests.get(helsinki_url)
stockholm_response = requests.get(stockholm_url)
# Getting the JSON data
helsinki_data = helsinki_response.json()
stockholm_data = stockholm_response.json()
# Printing the results
print('Helsinki Weather:', helsinki_data)
print('Stockholm Weather:', stockholm_data)
Agent 1 code execution output:
Helsinki Weather: {'coord': {'lon': 24.9384, 'lat': 60.1699}, 'weather': [{'id': 803, 'main': 'Clouds', 'description': 'broken clouds', 'icon': '04d'}], 'base': 'stations', 'main': {'temp': 289.96, 'feels_like': 289.97, 'temp_min': 288.76, 'temp_max': 291.18, 'pressure': 1005, 'humidity': 87, 'sea_level': 1005, 'grnd_level': 1002}, 'visibility': 10000, 'wind': {'speed': 5.36, 'deg': 317, 'gust': 6.26}, 'clouds': {'all': 75}, 'dt': 1722581370, 'sys': {'type': 2, 'id': 2011913, 'country': 'FI', 'sunrise': 1722564175, 'sunset': 1722624597}, 'timezone': 10800, 'id': 658225, 'name': 'Helsinki', 'cod': 200}
Stockholm Weather: {'coord': {'lon': 18.0686, 'lat': 59.3293}, 'weather': [{'id': 803, 'main': 'Clouds', 'description': 'broken clouds', 'icon': '04d'}], 'base': 'stations', 'main': {'temp': 289.46, 'feels_like': 288.97, 'temp_min': 288.9, 'temp_max': 290.45, 'pressure': 1010, 'humidity': 70, 'sea_level': 1010, 'grnd_level': 1005}, 'visibility': 10000, 'wind': {'speed': 2.06, 'deg': 270}, 'clouds': {'all': 57}, 'dt': 1722581370, 'sys': {'type': 1, 'id': 1788, 'country': 'SE', 'sunrise': 1722566149, 'sunset': 1722625921}, 'timezone': 7200, 'id': 2673730, 'name': 'Stockholm', 'cod': 200}
(venv) root@2c7cada58dbe:~#
Agent 1: Starting a message:
{
"thoughts": [
"I have successfully retrieved the current weather data for both Helsinki and Stockholm.",
"The data includes temperature, weather conditions, humidity, and wind speed.",
"I will now format this information for the user."
],
"tool_name": "response",
"tool_args": {
"text": "Here is the current weather data:\n\n**Helsinki:**\n- Temperature: 16.81°C\n- Weather: Broken clouds\n- Humidity: 87%\n- Wind Speed: 5.36 m/s\n\n**Stockholm:**\n- Temperature: 16.31°C\n- Weather: Broken clouds\n- Humidity: 70%\n- Wind Speed: 2.06 m/s"
}
}
Agent 0: Response from tool 'call_subordinate':
Here is the current weather data:
Helsinki:
- Temperature: 16.81°C
- Weather: Broken clouds
- Humidity: 87%
- Wind Speed: 5.36 m/s
Stockholm:
- Temperature: 16.31°C
- Weather: Broken clouds
- Humidity: 70%
- Wind Speed: 2.06 m/s
Agent 0: Starting a message:
{
"thoughts": [
"I have successfully retrieved the current weather data for both Helsinki and Stockholm.",
"Now I will prepare a response to the user comparing the weather in both locations."
],
"tool_name": "response",
"tool_args": {
"text": "Here is the current weather data:\n\n**Helsinki:**\n- Temperature: 16.81°C\n- Weather: Broken clouds\n- Humidity: 87%\n- Wind Speed: 5.36 m/s\n\n**Stockholm:**\n- Temperature: 16.31°C\n- Weather: Broken clouds\n- Humidity: 70%\n- Wind Speed: 2.06 m/s"
}
}
Final responce to my question :
Agent 0: reponse:
Here is the current weather data:
Helsinki:
- Temperature: 16.81°C
- Weather: Broken clouds
- Humidity: 87%
- Wind Speed: 5.36 m/s
Stockholm:
- Temperature: 16.31°C
- Weather: Broken clouds
- Humidity: 70%
- Wind Speed: 2.06 m/s