-
-
Save bizrockman/e9ecae0d7904512d64a547c5492ac296 to your computer and use it in GitHub Desktop.
AutoGen - Function Calling
This file contains hidden or 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
import os | |
import requests | |
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager | |
import openai | |
os.environ["AUTOGEN_USE_DOCKER"] = "False" | |
def get_current_weather(city_name): | |
weather_api_key = "<API KEY>" | |
# URL and parameters for the OpenWeatherMap API | |
base_url = "https://api.openweathermap.org/data/2.5/weather?" | |
complete_url = f"{base_url}q={city_name}&appid={weather_api_key}" | |
# Make a request to the API | |
response = requests.get(complete_url) | |
weather_data = response.json() | |
if weather_data['cod'] != 200: | |
return f"Error: {weather_data['message']}" | |
# Extracting weather information | |
weather = weather_data['weather'][0]['description'] | |
temperature = weather_data['main']['temp'] - 273.15 # Convert from Kelvin to Celsius | |
return weather, temperature | |
config_list = [ | |
{ | |
"model": "gpt-4o", | |
"api_key": "sk-..." | |
} | |
] | |
manager_llm_config = { | |
"config_list": config_list, | |
"cache_seed": None, | |
} | |
llm_config = { | |
"config_list": config_list, | |
"cache_seed": None, | |
"functions": [ | |
{ | |
"name": "get_current_weather", | |
"description": "retrieves the current weather for a given city", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"city_name":{ | |
"type": "string", | |
"description": "The name of the city to get the weather for" | |
} | |
}, | |
"required": ["city_name"] | |
} | |
}, | |
] | |
} | |
weather_reporter_system_msg = """Du bist ein Wetterberichterstatter, der den allgemeinen Wetterzustand aufgrund der | |
vom Benutzer angegebenen Daten und des Ortes liefert. Der Name eines Monats wird als Datum betrachtet. | |
Sollte der Benutzer keine Stadt angeben, dann frage nach der Stadt. | |
Nutze danach die Funktion get_current_weather(city_name), um das Wetter ab zurufen. | |
Deine Antwort soll kurz und ausschließlich über das Wetter sein. | |
Keine anderen Empfehlungen und keine Ausreden, dass du nur das Wetter liefern kannst. | |
""" | |
activity_agent_system_msg = """You are an activity agent who recommends | |
activities considering the weather situation from weather_reporter. | |
Don't ask questions. Stelle deine Antwort als eine Liste von Möglichkeiten da. | |
Close the conversation with TERMINATE.""" | |
weather_reporter = AssistantAgent( | |
name="Weather_reporter", | |
system_message=weather_reporter_system_msg, | |
llm_config=llm_config, | |
description="""This agent is reponsible for providing weather | |
overall status based on the dates and location user provided. | |
The weather information should be forwared to the acivity agent. | |
""" | |
) | |
activity_agent = AssistantAgent( | |
name="activity_agent", | |
system_message=activity_agent_system_msg, | |
llm_config=llm_config, | |
description="""This agent is responsible for actitivies | |
recommendation considering the weather situation from weather_reporter. | |
The response fo the activity_agent need to be send back to the user. | |
""", | |
) | |
user_proxy = UserProxyAgent( | |
name="user_proxy", | |
is_termination_msg=lambda msg: "TERMINATE" in msg["content"], | |
human_input_mode="ALWAYS", | |
code_execution_config=False, | |
description="If a messages contains TERMINATE, the conversation will be handled by this user proxy." | |
) | |
user_proxy.register_function(function_map={"get_current_weather": get_current_weather}) | |
groupchat = GroupChat(agents=[user_proxy, activity_agent, weather_reporter], messages=[], max_round=8) | |
manager = GroupChatManager(groupchat=groupchat, llm_config=manager_llm_config) | |
user_proxy.initiate_chat(manager, message="Was kann ich heute unternehmen?") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment