# Tools are available in the autogen.tools namespace
from autogen import ConversableAgent, register_function, LLMConfig
from autogen.tools.experimental import SlackRetrieveTool, SlackSendTool
# For running the code in Jupyter, use nest_asyncio to allow nested event loops
#import nest_asyncio
#nest_asyncio.apply()
# LLM configuration for our agent to select the tools and craft the message
# Put your key in the OPENAI_API_KEY environment variable
llm_config = LLMConfig(api_type="openai", model="gpt-4o-mini")
# Our tool executor agent, which will run the tools once recommended by the slack_agent, no LLM required
executor_agent = ConversableAgent(
name="executor_agent",
human_input_mode="NEVER",
)
# The main star of the show, our ConversableAgent-based agent
# We will attach the tools to this agent
with llm_config:
slack_agent = ConversableAgent(
name="slack_agent",
)
# Create tools and register them with the agents
_bot_token = "xoxo..." # CHANGE THIS, OAuth token
_channel_id = "C1234567" # CHANGE THIS, ID of the Slack channel
# Create our Send tool
slack_send_tool = SlackSendTool(bot_token=_bot_token, channel_id=_channel_id)
# Register it for recommendation by our Slack agent
slack_send_tool.register_for_llm(slack_agent)
# Register it for execution by our executor agent
slack_send_tool.register_for_execution(executor_agent)
# And the same for our our Retrieve tool
slack_retrieve_tool = SlackRetrieveTool(bot_token=_bot_token, channel_id=_channel_id)
slack_retrieve_tool.register_for_llm(slack_agent)
slack_retrieve_tool.register_for_execution(executor_agent)
# Here we create a dummy weather function that will be used by our agent
def get_weather():
return "The weather today is 25 degrees Celsius and sunny, with a late storm."
# Register it with the slack_agent for LLM tool recommendations
# and the executor_agent to execute it
register_function(
get_weather,
caller=slack_agent,
executor=executor_agent,
description="Get the current weather forecast",
)
# Start the conversation
# The slack_agent suggests the weather and send tools, crafting the Slack message
# while the executor_agent executes the weather tool and the Slack send tool
executor_agent.initiate_chat(
recipient=slack_agent,
message="Get the latest weather forecast and send it to our Slack channel. Use some emojis to make it fun!",
max_turns=3,
)