feat: added multi-agent coordination example for OpenAI and Gemini #186
feat: added multi-agent coordination example for OpenAI and Gemini #186
Conversation
…a Javelin (research → summary → report)
There was a problem hiding this comment.
Hello @dhruvj07, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request introduces two new examples demonstrating multi-agent coordination using the Google ADK (Agent Development Kit) with Javelin. The first example, adk_gemini_agent_javelin, showcases coordination using the Gemini model, while the second, adk_openai_agent_javelin, uses the OpenAI model. Both examples implement a sequential agent that coordinates a researcher, a summarizer, and a reporter to process a user query and generate a final report. The agents leverage the Javelin API for model access and routing.
Highlights
- Multi-Agent Coordination: Demonstrates how to create and coordinate multiple agents using the ADK to perform a complex task (research, summarize, report).
- Javelin Integration: Shows how to integrate with the Javelin API to access language models like Gemini and GPT-4o.
- Sequential Agent: Implements a
SequentialAgentto orchestrate the execution of sub-agents in a predefined order. - Environment Variables: Uses environment variables (
GEMINI_API_KEY,JAVELIN_API_KEY,OPENAI_API_KEY) for secure API key management.
Changelog
Click here to see the changelog
- examples/agents/adk_gemini_agent_javelin/init.py
- Added
__init__.pyto import theroot_agentfromagent.py.
- Added
- examples/agents/adk_gemini_agent_javelin/agent.py
- Created
agent.pyto define the Gemini-based multi-agent system. - Implemented a
research_agent,summary_agent, andreport_agentusingLlmAgentand the Gemini model via Javelin. - Defined a
root_agentas aSequentialAgentto coordinate the sub-agents. - Added a
mainfunction to run the agent and print the final report. - Utilized
LiteLlmto configure the language model with Javelin API details. - Ensured API keys are loaded from environment variables and validated.
- Created
- examples/agents/adk_openai_agent_javelin/init.py
- Added
__init__.pyto import theroot_agentfromagent.py.
- Added
- examples/agents/adk_openai_agent_javelin/agent.py
- Created
agent.pyto define the OpenAI-based multi-agent system. - Implemented a
research_agent,summary_agent, andreport_agentusingLlmAgentand the GPT-4o model via Javelin. - Defined a
coordinator(aliased asroot_agent) as aSequentialAgentto coordinate the sub-agents. - Added a
mainfunction to run the agent and print the final report. - Utilized
LiteLlmto configure the language model with Javelin API details. - Ensured API keys are loaded from environment variables and validated.
- Created
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Agents in sequence flow,
Research, summarize, then show.
Javelin's swift route,
A report to boot,
Knowledge from the AI glow.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces multi-agent coordination examples for OpenAI and Gemini using Javelin. The code is well-structured and demonstrates the use of LlmAgent and SequentialAgent for creating research, summary, and report agents. However, there are a few areas that could be improved for clarity and maintainability.
Summary of Findings
- Missing Error Handling: The
mainfunctions in both agent examples lack comprehensive error handling. Consider adding try-except blocks to catch potential exceptions during agent execution and provide informative error messages. - Duplicated Code: The OpenAI and Gemini agent examples share a lot of code. Consider creating a base class or utility function to reduce duplication and improve maintainability.
- Hardcoded Values: The query strings in both examples are hardcoded. Consider making them configurable via environment variables or command-line arguments.
Merge Readiness
The pull request introduces valuable examples of multi-agent coordination. However, addressing the missing error handling and code duplication issues would significantly improve the code's robustness and maintainability. I recommend addressing these issues before merging. I am unable to directly approve this pull request, and users should have others review and approve this code before merging, especially after the requested changes are made.
| if not GEMINI_API_KEY: | ||
| raise ValueError("Missing GEMINI_API_KEY") | ||
| if not JAVELIN_API_KEY: | ||
| raise ValueError("Missing JAVELIN_API_KEY") |
There was a problem hiding this comment.
Consider using os.environ.get with a default value instead of raising a ValueError. This allows the program to run with a default behavior if the environment variables are not set, and provides a more graceful degradation. Also, consider logging a warning message if the environment variables are not set.
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
JAVELIN_API_KEY = os.getenv("JAVELIN_API_KEY")
if not GEMINI_API_KEY:
print("Warning: Missing GEMINI_API_KEY in environment, using default value.")
GEMINI_API_KEY = "default_gemini_api_key" # Replace with an actual default value
if not JAVELIN_API_KEY:
print("Warning: Missing JAVELIN_API_KEY in environment, using default value.")
JAVELIN_API_KEY = "default_javelin_api_key" # Replace with an actual default value| async def main(): | ||
| session_service = InMemorySessionService() | ||
| session_service.create_session("gemini_multi_agent_app", "user", "sess") | ||
|
|
||
| runner = Runner( | ||
| agent=root_agent, | ||
| app_name="gemini_multi_agent_app", | ||
| session_service=session_service, | ||
| ) | ||
|
|
||
| query = "role of AI in sustainable energy" | ||
| msg = Content(role="user", parts=[Part.from_text(query)]) | ||
|
|
||
| final_answer = "" | ||
| async for event in runner.run_async("user", "sess", new_message=msg): | ||
| if event.is_final_response() and event.content: | ||
| final_answer = event.content.parts[0].text | ||
| break | ||
|
|
||
| print("\n--- Final Report ---\n", final_answer) |
There was a problem hiding this comment.
Consider adding a try-except block to handle potential exceptions during agent execution. This will prevent the program from crashing and provide more informative error messages.
try:
session_service = InMemorySessionService()
session_service.create_session("gemini_multi_agent_app", "user", "sess")
runner = Runner(
agent=root_agent,
app_name="gemini_multi_agent_app",
session_service=session_service,
)
query = "role of AI in sustainable energy"
msg = Content(role="user", parts=[Part.from_text(query)])
final_answer = ""
async for event in runner.run_async("user", "sess", new_message=msg):
if event.is_final_response() and event.content:
final_answer = event.content.parts[0].text
break
print("\n--- Final Report ---\n", final_answer)
except Exception as e:
print(f"An error occurred: {e}")| async def main(): | ||
| session_service = InMemorySessionService() | ||
| session_service.create_session("openai_multi_agent_app", "user", "sess") | ||
|
|
||
| runner = Runner( | ||
| agent=coordinator, | ||
| app_name="openai_multi_agent_app", | ||
| session_service=session_service, | ||
| ) | ||
|
|
||
| # Provide user query | ||
| query = "impact of AI on global education" | ||
| msg = Content(role="user", parts=[Part.from_text(query)]) | ||
|
|
||
| final_answer = "" | ||
| async for event in runner.run_async("user", "sess", new_message=msg): | ||
| if event.is_final_response() and event.content: | ||
| final_answer = event.content.parts[0].text | ||
| break | ||
|
|
||
| print("\n--- Final Report ---\n", final_answer) |
There was a problem hiding this comment.
Consider adding a try-except block to handle potential exceptions during agent execution. This will prevent the program from crashing and provide more informative error messages.
try:
session_service = InMemorySessionService()
session_service.create_session("openai_multi_agent_app", "user", "sess")
runner = Runner(
agent=coordinator,
app_name="openai_multi_agent_app",
session_service=session_service,
)
# Provide user query
query = "impact of AI on global education"
msg = Content(role="user", parts=[Part.from_text(query)])
final_answer = ""
async for event in runner.run_async("user", "sess", new_message=msg):
if event.is_final_response() and event.content:
final_answer = event.content.parts[0].text
break
print("\n--- Final Report ---\n", final_answer)
except Exception as e:
print(f"An error occurred: {e}")
added multi-agent coordination example for OpenAI and Gemini via Javelin (research → summary → report)