Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/agents/adk_gemini_agent_javelin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .agent import root_agent
97 changes: 97 additions & 0 deletions examples/agents/adk_gemini_agent_javelin/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os
import asyncio
from dotenv import load_dotenv

from google.adk.agents import LlmAgent, SequentialAgent
from google.adk.models.lite_llm import LiteLlm
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.genai.types import Content, Part

load_dotenv()

GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
JAVELIN_API_KEY = os.getenv("JAVELIN_API_KEY")

if not GEMINI_API_KEY:
raise ValueError("Missing GEMINI_API_KEY")
if not JAVELIN_API_KEY:
raise ValueError("Missing JAVELIN_API_KEY")
Comment on lines +16 to +19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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


# Agent 1: Researcher
research_agent = LlmAgent(
model=LiteLlm(
model="openai/gemini-1.5-flash",
api_base="https://api-dev.javelin.live/v1/",
extra_headers={
"x-javelin-route": "google_univ",
"x-api-key": JAVELIN_API_KEY,
"Authorization": f"Bearer {GEMINI_API_KEY}",
},
),
name="GeminiResearchAgent",
instruction="Research the query and save findings in state['research'].",
output_key="research",
)

# Agent 2: Summarizer
summary_agent = LlmAgent(
model=LiteLlm(
model="openai/gemini-1.5-flash",
api_base="https://api-dev.javelin.live/v1/",
extra_headers={
"x-javelin-route": "google_univ",
"x-api-key": JAVELIN_API_KEY,
"Authorization": f"Bearer {GEMINI_API_KEY}",
},
),
name="GeminiSummaryAgent",
instruction="Summarize state['research'] into state['summary'].",
output_key="summary",
)

# Agent 3: Reporter
report_agent = LlmAgent(
model=LiteLlm(
model="openai/gemini-1.5-flash",
api_base="https://api-dev.javelin.live/v1/",
extra_headers={
"x-javelin-route": "google_univ",
"x-api-key": JAVELIN_API_KEY,
"Authorization": f"Bearer {GEMINI_API_KEY}",
},
),
name="GeminiReportAgent",
instruction="Generate a report from state['summary'] and include a source URL.",
output_key="report",
)

# Coordinator agent
root_agent = SequentialAgent(
name="GeminiMultiAgentCoordinator",
sub_agents=[research_agent, summary_agent, report_agent]
)

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)
Comment on lines +75 to +94
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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}")


if __name__ == "__main__":
asyncio.run(main())
1 change: 1 addition & 0 deletions examples/agents/adk_openai_agent_javelin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .agent import root_agent
100 changes: 100 additions & 0 deletions examples/agents/adk_openai_agent_javelin/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import os
import asyncio
from dotenv import load_dotenv

from google.adk.agents import LlmAgent, SequentialAgent
from google.adk.models.lite_llm import LiteLlm
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.genai.types import Content, Part

load_dotenv()

JAVELIN_API_KEY = os.getenv("JAVELIN_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

if not JAVELIN_API_KEY:
raise ValueError("Missing JAVELIN_API_KEY in environment")
if not OPENAI_API_KEY:
raise ValueError("Missing OPENAI_API_KEY in environment")

# Agent 1: Researcher
research_agent = LlmAgent(
model=LiteLlm(
model="openai/gpt-4o",
api_base="https://api-dev.javelin.live/v1",
extra_headers={
"x-javelin-route": "openai_univ",
"x-api-key": JAVELIN_API_KEY,
"Authorization": f"Bearer {OPENAI_API_KEY}",
},
),
name="ResearchAgent",
instruction="Research the query and save findings in state['research'].",
output_key="research",
)

# Agent 2: Summarizer
summary_agent = LlmAgent(
model=LiteLlm(
model="openai/gpt-4o",
api_base="https://api-dev.javelin.live/v1",
extra_headers={
"x-javelin-route": "openai_univ",
"x-api-key": JAVELIN_API_KEY,
"Authorization": f"Bearer {OPENAI_API_KEY}",
},
),
name="SummaryAgent",
instruction="Summarize state['research'] into state['summary'].",
output_key="summary",
)

# Agent 3: Reporter
report_agent = LlmAgent(
model=LiteLlm(
model="openai/gpt-4o",
api_base="https://api-dev.javelin.live/v1",
extra_headers={
"x-javelin-route": "openai_univ",
"x-api-key": JAVELIN_API_KEY,
"Authorization": f"Bearer {OPENAI_API_KEY}",
},
),
name="ReportAgent",
instruction="Generate a report from state['summary'] and include a source URL.",
output_key="report",
)

# Coordinator agent running all three sequentially
coordinator = SequentialAgent(
name="OpenAI_MultiAgentCoordinator",
sub_agents=[research_agent, summary_agent, report_agent]
)
root_agent = coordinator


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)
Comment on lines +77 to +97
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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}")


if __name__ == "__main__":
asyncio.run(main())
Loading