In this tutorial, we have demonstrated how Microsoft Autogen Framework allows developers to orchestrate complex and multi-agent workflows with a minimum code. By taking advantage of the Roundrobingrouchat and Teamtool of Autogen abstractions, you can transparently assemble specialized assistants, such as researchers, checks, criticism, summaries and publishers, in a “deepdive” cohesive tool. Autogen manages the subtleties of production, termination conditions and the streaming outing, allowing you to focus on the definition of expertise and prompts from the system of each agent rather than the plumbing of reminders or channels of manual prompts. Whether it is looking for in -depth research, validating facts, refining prose or integrating third -party tools, Autogen provides a unified API which evolves from pipelines to two simple agents with collaboratives drawn up with five agents.
!pip install -q autogen-agentchat(gemini) autogen-ext(openai) nest_asyncio
We install the Autogen Agentchat package with the management of the gemini, the OPENAI extension for the compatibility of the API and the Nest_asyncio library to correct the event loop of the notebook, guaranteeing that you have all the necessary components to execute asynchronous and multi-agent workflows in colab.
import os, nest_asyncio
from getpass import getpass
nest_asyncio.apply()
os.environ("GEMINI_API_KEY") = getpass("Enter your Gemini API key: ")
We import and apply Nest_asyncio to activate the nested event loops in notebooks, then invites you safely for your Gemini API key using Getpass and stocks in OS. Environnounce for access to the authenticated customer of the model.
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(
model="gemini-1.5-flash-8b",
api_key=os.environ("GEMINI_API_KEY"),
api_type="google",
)
We initialize an OpenAi compatibility cat client pointed at Google Gemini by specifying the Gemini-1.5-Flash-8B model, injecting your stored gemini API key and defining API_TYPE = “Google”, offering you a ready to use model for downstream.
from autogen_agentchat.agents import AssistantAgent
researcher = AssistantAgent(name="Researcher", system_message="Gather and summarize factual info.", model_client=model_client)
factchecker = AssistantAgent(name="FactChecker", system_message="Verify facts and cite sources.", model_client=model_client)
critic = AssistantAgent(name="Critic", system_message="Critique clarity and logic.", model_client=model_client)
summarizer = AssistantAgent(name="Summarizer",system_message="Condense into a brief executive summary.", model_client=model_client)
editor = AssistantAgent(name="Editor", system_message="Polish language and signal APPROVED when done.", model_client=model_client)
We define five specialized assistant agents, researcher, factchecker, critic, summary and publisher, each initialized with a system specific to the role and customer of shared model Gémini, allowing them to collect information, respectively, to verify the accuracy, the content of the criticism, the summaries of Condense and the Polish language within the Autogen flora.
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
max_msgs = MaxMessageTermination(max_messages=20)
text_term = TextMentionTermination(text="APPROVED", sources=("Editor"))
termination = max_msgs | text_term
team = RoundRobinGroupChat(
participants=(researcher, factchecker, critic, summarizer, editor),
termination_condition=termination
)
We import the RoundrobingRoupchat class as well as two termination conditions, then compose a stop rule which draws after 20 messages in total or when the editor's agent mentions “approved”. Finally, he instantiated a team in the round of the five specialized agents with this combined end logic, allowing them to go through research, verification of facts, criticism, summary and publishing until one of the stop conditions is met.
from autogen_agentchat.tools import TeamTool
deepdive_tool = TeamTool(team=team, name="DeepDive", description="Collaborative multi-agent deep dive")
We wrap our RoundrobingRoupchat team in a teamtool called “Deepdive” with a description readable by humans, effectively packing the entire multi-agent workflow in a single called tool that other agents can invoke transparently.
host = AssistantAgent(
name="Host",
model_client=model_client,
tools=(deepdive_tool),
system_message="You have access to a DeepDive tool for in-depth research."
)
We create a “host” assistant agent configured with the model shared gemini, grant him the Deepdive team tool to orchestrate in-depth research and amortize it with a system message that informs him of his ability to invoke the Deepdive Multi-agent workflow.
import asyncio
async def run_deepdive(topic: str):
result = await host.run(task=f"Deep dive on: {topic}")
print("🔍 DeepDive result:\n", result)
await model_client.close()
topic = "Impacts of Model Context Protocl on Agentic AI"
loop = asyncio.get_event_loop()
loop.run_until_complete(run_deepdive(topic))
Finally, we define a asynchronous run_deepdive function which indicates the host agent to execute the Deepdive team tool on a given subject, prints the full result, then closes the model customer; He then entered the existing asyncio loop of Colab and executes the Coroutine to be completed for a seamless synchronous execution.
In conclusion, the integration of Google Gemini via the Autogen Openai compatible customer and the envelope of our multi-agent team as a Calble Tandenblas gives us a powerful model to build highly modular and reusable workflows. Autogen sums up the management of the event loop (with Nest_asyncio), the responses in streaming and the termination logic, which allows us to quickly iterate on agent roles and global orchestration. This advanced model rationalizes the development of collaborative AI systems and lays the basics of extension in recovery pipelines, dynamic selectors or conditional execution strategies.
Discover the Notebook here. All the merit of this research goes to researchers in this project. Also, don't hesitate to follow us Twitter And don't forget to join our 95K + ML Subdreddit and subscribe to Our newsletter.
Asif Razzaq is the CEO of Marktechpost Media Inc .. as a visionary entrepreneur and engineer, AIF undertakes to exploit the potential of artificial intelligence for social good. His most recent company is the launch of an artificial intelligence media platform, Marktechpost, which stands out from its in-depth coverage of automatic learning and in-depth learning news which are both technically solid and easily understandable by a large audience. The platform has more than 2 million monthly views, illustrating its popularity with the public.
