Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bizrockman/fda8649f722e60e2b6512f704b6a46a3 to your computer and use it in GitHub Desktop.
Save bizrockman/fda8649f722e60e2b6512f704b6a46a3 to your computer and use it in GitHub Desktop.
AutoGen - Statische Kommunikationswege
import os
import requests
from autogen import AssistantAgent, UserProxyAgent, ConversableAgent
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
def reply_func_Weather2User(self: ConversableAgent, messages, sender: ConversableAgent, config):
last_message = messages[-1]
while True:
messages.append({'content': 'Prüfen ob der User eine Stadt genannt hat und gibt den Namen dieser Stadt als '
'Antwort zurück. Falls nein, schreibe "Keine Stadt"', 'role': 'user'})
final, city_response = self.generate_oai_reply(messages, config)
if "Keine Stadt" in city_response:
human_input = self.get_human_input("Bitte nennen Sie mir die Stadt, für die Sie das Wetter wissen möchten: ")
messages.append({'content': human_input, 'role': 'user'})
else:
break
weather = get_current_weather(city_response)
messages = [{'content': f"Bitte erstelle einen kurzen Wetterbericht für die Stadt {city_response}. Das Wetter ist {weather}. Keine Erläuterungen NUR den Wetterbericht.",
'role': 'system'}]
final, response = self.generate_oai_reply(messages, config)
return True, response
def reply_func_User2Activity(self: ConversableAgent, messages, sender: ConversableAgent, config):
reply = self.send(recipient=activity_agent, message=f"Hier der Wetterbericht: {messages[-1]['content']} ", request_reply=True)
return True, reply
config_list = [
{
"model": "gpt-4o",
"api_key": "sk-..."
}
]
llm_config = {
"config_list": config_list,
"cache_seed": None
}
activity_agent_system_msg = """You are an activity agent who recommends
activities considering the weather situation from weather_reporter.
Make it a bullet list.
Don't ask questions. Make your response short.
Reply "TERMINATE" in the end when everything is done."""
weather_reporter = AssistantAgent(
name="Weather_reporter",
system_message="",
llm_config=llm_config,
)
activity_agent = AssistantAgent(
name="activity_agent",
system_message=activity_agent_system_msg,
llm_config=llm_config,
)
user_proxy = UserProxyAgent(
name="user_proxy",
is_termination_msg=lambda msg: "TERMINATE" in msg["content"],
human_input_mode="TERMINATE",
)
weather_reporter.register_reply(user_proxy, reply_func_Weather2User)
user_proxy.register_reply(weather_reporter, reply_func_User2Activity)
user_proxy.initiate_chat(weather_reporter, message="Was kann ich heute unternehmen?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment