Skip to content

Commit b8e369f

Browse files
[AI-262] Update and refactor example code (#153)
* refactor: modernize all example code to use AgentLauncher pattern Updated all examples to follow the modern pattern: - Split into create_agent() and join_call() functions - Use await agent.create_user() for user creation - Use await agent.create_call(call_type, call_id) for call creation - Use cli(AgentLauncher(...)) in main block - Add proper logging imports and logger instances This makes all examples consistent with the openai_realtime_example.py pattern. * fix: resolve ruff linting issues in agents.py Fixed two ruff linting errors: - F811: Removed duplicate Call import from getstream.video.async_call - F841: Removed unused response variable in create_call method All checks now pass (ruff, mypy, and tests). --------- Co-authored-by: Cursor Agent <[email protected]>
1 parent 7029f91 commit b8e369f

File tree

17 files changed

+294
-171
lines changed

17 files changed

+294
-171
lines changed

agents-core/vision_agents/core/agents/agents.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import time
66
import uuid
77
from dataclasses import asdict
8-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeGuard, Coroutine
8+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeGuard
99
from uuid import uuid4
1010

1111
import getstream.models
1212
from aiortc import VideoStreamTrack
13-
from getstream.video.async_call import Call
1413
from getstream.video.rtc import Call
1514

1615
from getstream.video.rtc.pb.stream.video.sfu.models.models_pb2 import TrackType
@@ -697,7 +696,7 @@ async def create_user(self) -> None:
697696
async def create_call(self, call_type: str, call_id: str) -> Call:
698697
"""Shortcut for creating a call/room etc."""
699698
call = self.edge.client.video.call(call_type, call_id)
700-
response = await call.get_or_create(data={"created_by_id": self.agent_user.id})
699+
await call.get_or_create(data={"created_by_id": self.agent_user.id})
701700

702701
return call
703702

examples/01_simple_agent_example/simple_agent_example.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import asyncio
2-
from uuid import uuid4
1+
import logging
2+
33
from dotenv import load_dotenv
44

5-
from vision_agents.core import User, Agent
5+
from vision_agents.core import User, Agent, cli
6+
from vision_agents.core.agents import AgentLauncher
67
from vision_agents.plugins import deepgram, getstream, gemini, vogent, elevenlabs
78
# from vision_agents.core.profiling import Profiler
89

10+
logger = logging.getLogger(__name__)
11+
912
load_dotenv()
1013

1114

12-
async def start_agent() -> None:
15+
async def create_agent(**kwargs) -> Agent:
1316
llm = gemini.LLM("gemini-2.0-flash")
1417
# create an agent to run with Stream's edge, openAI llm
1518
agent = Agent(
@@ -30,9 +33,14 @@ async def start_agent() -> None:
3033
# realtime version (vad, tts and stt not needed)
3134
# llm=openai.Realtime()
3235
)
36+
return agent
37+
3338

39+
async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None:
40+
# ensure the agent user is created
41+
await agent.create_user()
3442
# Create a call
35-
call = agent.edge.client.video.call("default", str(uuid4()))
43+
call = await agent.create_call(call_type, call_id)
3644

3745
# Have the agent join the call/room
3846
with await agent.join(call):
@@ -89,4 +97,4 @@ def _flush_and_shutdown():
8997

9098
if __name__ == "__main__":
9199
# setup_telemetry()
92-
asyncio.run(start_agent())
100+
cli(AgentLauncher(create_agent=create_agent, join_call=join_call))

examples/02_golf_coach_example/golf_coach_example.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import asyncio
2-
from uuid import uuid4
1+
import logging
32

43
from dotenv import load_dotenv
54

6-
from vision_agents.core import User, Agent
5+
from vision_agents.core import User, Agent, cli
6+
from vision_agents.core.agents import AgentLauncher
77
from vision_agents.plugins import getstream, ultralytics, gemini
88

9+
logger = logging.getLogger(__name__)
10+
911
load_dotenv()
1012

1113

12-
async def start_agent() -> None:
14+
async def create_agent(**kwargs) -> Agent:
1315
agent = Agent(
1416
edge=getstream.Edge(), # use stream for edge video transport
1517
agent_user=User(name="AI golf coach"),
@@ -20,9 +22,14 @@ async def start_agent() -> None:
2022
ultralytics.YOLOPoseProcessor(model_path="yolo11n-pose.pt")
2123
], # realtime pose detection with yolo
2224
)
25+
return agent
26+
2327

24-
# create a call, some other video networks call this a room
25-
call = agent.edge.client.video.call("default", str(uuid4()))
28+
async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None:
29+
# ensure the agent user is created
30+
await agent.create_user()
31+
# Create a call
32+
call = await agent.create_call(call_type, call_id)
2633

2734
# join the call and open a demo env
2835
with await agent.join(call):
@@ -37,4 +44,4 @@ async def start_agent() -> None:
3744

3845

3946
if __name__ == "__main__":
40-
asyncio.run(start_agent())
47+
cli(AgentLauncher(create_agent=create_agent, join_call=join_call))

examples/other_examples/09_github_mcp_demo/gemini_realtime_github_mcp_demo.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
using voice commands through the Gemini Live API.
66
"""
77

8-
import asyncio
98
import logging
109
import os
11-
from uuid import uuid4
10+
1211
from dotenv import load_dotenv
1312

14-
from vision_agents.core.agents import Agent
13+
from vision_agents.core.agents import Agent, AgentLauncher
14+
from vision_agents.core import cli
1515
from vision_agents.core.mcp import MCPServerRemote
1616
from vision_agents.plugins.gemini.gemini_realtime import Realtime
1717
from vision_agents.plugins import getstream
@@ -26,22 +26,22 @@
2626
logger = logging.getLogger(__name__)
2727

2828

29-
async def start_agent():
29+
async def create_agent(**kwargs) -> Agent:
3030
"""Demonstrate Gemini Realtime with GitHub MCP server integration."""
3131

3232
# Get GitHub PAT from environment
3333
github_pat = os.getenv("GITHUB_PAT")
3434
if not github_pat:
3535
logger.error("GITHUB_PAT environment variable not found!")
3636
logger.error("Please set GITHUB_PAT in your .env file or environment")
37-
return
37+
raise ValueError("GITHUB_PAT environment variable not found")
3838

3939
# Get Google API key from environment
4040
google_api_key = os.getenv("GOOGLE_API_KEY")
4141
if not google_api_key:
4242
logger.error("GOOGLE_API_KEY environment variable not found!")
4343
logger.error("Please set GOOGLE_API_KEY in your .env file or environment")
44-
return
44+
raise ValueError("GOOGLE_API_KEY environment variable not found")
4545

4646
# Create GitHub MCP server
4747
github_server = MCPServerRemote(
@@ -69,6 +69,10 @@ async def start_agent():
6969
logger.info("Agent created with Gemini Realtime and GitHub MCP server")
7070
logger.info(f"GitHub server: {github_server}")
7171

72+
return agent
73+
74+
75+
async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None:
7276
try:
7377
# Set up event handler for when participants join
7478
@agent.subscribe
@@ -85,8 +89,10 @@ async def on_participant_joined(event: CallSessionParticipantJoinedEvent):
8589
f"Hello {event.participant.user.name}! I'm your GitHub AI assistant powered by Gemini Live. I have access to {len(mcp_functions)} GitHub tools and can help you with repositories, issues, pull requests, and more through voice commands!"
8690
)
8791

92+
# ensure the agent user is created
93+
await agent.create_user()
8894
# Create a call
89-
call = agent.edge.client.video.call("default", str(uuid4()))
95+
call = await agent.create_call(call_type, call_id)
9096

9197
# Have the agent join the call/room
9298
logger.info("🎤 Agent joining call...")
@@ -123,4 +129,4 @@ async def on_participant_joined(event: CallSessionParticipantJoinedEvent):
123129

124130

125131
if __name__ == "__main__":
126-
asyncio.run(start_agent())
132+
cli(AgentLauncher(create_agent=create_agent, join_call=join_call))

examples/other_examples/09_github_mcp_demo/github_mcp_demo.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
by the LLM without any manual registration required.
66
"""
77

8-
import asyncio
98
import logging
109
import os
11-
from uuid import uuid4
10+
1211
from dotenv import load_dotenv
1312

14-
from vision_agents.core.agents import Agent
13+
from vision_agents.core.agents import Agent, AgentLauncher
14+
from vision_agents.core import cli
1515
from vision_agents.core.mcp import MCPServerRemote
1616
from vision_agents.plugins.openai.openai_llm import OpenAILLM
1717
from vision_agents.plugins import elevenlabs, deepgram, silero, getstream
@@ -26,15 +26,15 @@
2626
logger = logging.getLogger(__name__)
2727

2828

29-
async def start_agent():
29+
async def create_agent(**kwargs) -> Agent:
3030
"""Demonstrate GitHub MCP server integration."""
3131

3232
# Get GitHub PAT from environment
3333
github_pat = os.getenv("GITHUB_PAT")
3434
if not github_pat:
3535
logger.error("GITHUB_PAT environment variable not found!")
3636
logger.error("Please set GITHUB_PAT in your .env file or environment")
37-
return
37+
raise ValueError("GITHUB_PAT environment variable not found")
3838

3939
# Create GitHub MCP server
4040
github_server = MCPServerRemote(
@@ -49,7 +49,7 @@ async def start_agent():
4949
if not openai_api_key:
5050
logger.error("OPENAI_API_KEY environment variable not found!")
5151
logger.error("Please set OPENAI_API_KEY in your .env file or environment")
52-
return
52+
raise ValueError("OPENAI_API_KEY environment variable not found")
5353

5454
# Create OpenAI LLM
5555
llm = OpenAILLM(model="gpt-4o", api_key=openai_api_key)
@@ -74,6 +74,10 @@ async def start_agent():
7474
logger.info("Agent created with GitHub MCP server")
7575
logger.info(f"GitHub server: {github_server}")
7676

77+
return agent
78+
79+
80+
async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None:
7781
try:
7882
# Connect to GitHub MCP server with timeout
7983
logger.info("Connecting to GitHub MCP server...")
@@ -95,8 +99,10 @@ async def on_participant_joined(event: CallSessionParticipantJoinedEvent):
9599
f"Hello {event.participant.user.name}! I'm your GitHub AI assistant with access to {len(mcp_functions)} GitHub tools. I can help you with repositories, issues, pull requests, and more!"
96100
)
97101

102+
# ensure the agent user is created
103+
await agent.create_user()
98104
# Create a call
99-
call = agent.edge.client.video.call("default", str(uuid4()))
105+
call = await agent.create_call(call_type, call_id)
100106

101107
# Have the agent join the call/room
102108
logger.info("🎤 Agent joining call...")
@@ -126,4 +132,4 @@ async def on_participant_joined(event: CallSessionParticipantJoinedEvent):
126132

127133

128134
if __name__ == "__main__":
129-
asyncio.run(start_agent())
135+
cli(AgentLauncher(create_agent=create_agent, join_call=join_call))

examples/other_examples/09_github_mcp_demo/openai_realtime_github_mcp_demo.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
using voice commands through the OpenAI Realtime API.
66
"""
77

8-
import asyncio
98
import logging
109
import os
11-
from uuid import uuid4
10+
1211
from dotenv import load_dotenv
1312

14-
from vision_agents.core.agents import Agent
13+
from vision_agents.core.agents import Agent, AgentLauncher
14+
from vision_agents.core import cli
1515
from vision_agents.core.mcp import MCPServerRemote
1616
from vision_agents.plugins.openai.openai_realtime import Realtime
1717
from vision_agents.plugins import getstream
@@ -26,22 +26,22 @@
2626
logger = logging.getLogger(__name__)
2727

2828

29-
async def start_agent():
29+
async def create_agent(**kwargs) -> Agent:
3030
"""Demonstrate OpenAI Realtime with GitHub MCP server integration."""
3131

3232
# Get GitHub PAT from environment
3333
github_pat = os.getenv("GITHUB_PAT")
3434
if not github_pat:
3535
logger.error("GITHUB_PAT environment variable not found!")
3636
logger.error("Please set GITHUB_PAT in your .env file or environment")
37-
return
37+
raise ValueError("GITHUB_PAT environment variable not found")
3838

3939
# Check OpenAI API key from environment
4040
openai_api_key = os.getenv("OPENAI_API_KEY")
4141
if not openai_api_key:
4242
logger.error("OPENAI_API_KEY environment variable not found!")
4343
logger.error("Please set OPENAI_API_KEY in your .env file or environment")
44-
return
44+
raise ValueError("OPENAI_API_KEY environment variable not found")
4545

4646
# Create GitHub MCP server
4747
github_server = MCPServerRemote(
@@ -71,6 +71,10 @@ async def start_agent():
7171
logger.info("Agent created with OpenAI Realtime and GitHub MCP server")
7272
logger.info(f"GitHub server: {github_server}")
7373

74+
return agent
75+
76+
77+
async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None:
7478
try:
7579
# Set up event handler for when participants join
7680
@agent.subscribe
@@ -87,8 +91,10 @@ async def on_participant_joined(event: CallSessionParticipantJoinedEvent):
8791
f"Hello {event.participant.user.name}! I'm your GitHub AI assistant powered by OpenAI Realtime. I have access to {len(mcp_functions)} GitHub tools and can help you with repositories, issues, pull requests, and more through voice commands!"
8892
)
8993

94+
# ensure the agent user is created
95+
await agent.create_user()
9096
# Create a call
91-
call = agent.edge.client.video.call("default", str(uuid4()))
97+
call = await agent.create_call(call_type, call_id)
9298

9399
# Have the agent join the call/room
94100
logger.info("🎤 Agent joining call...")
@@ -125,4 +131,4 @@ async def on_participant_joined(event: CallSessionParticipantJoinedEvent):
125131

126132

127133
if __name__ == "__main__":
128-
asyncio.run(start_agent())
134+
cli(AgentLauncher(create_agent=create_agent, join_call=join_call))
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import asyncio
21
import logging
3-
from uuid import uuid4
42

53
from dotenv import load_dotenv
6-
from getstream import AsyncStream
74

85
from vision_agents.core.edge.types import User
9-
from vision_agents.core.agents import Agent
6+
from vision_agents.core.agents import Agent, AgentLauncher
7+
from vision_agents.core import cli
108
from vision_agents.plugins import gemini, getstream
119

1210
load_dotenv()
@@ -18,9 +16,7 @@
1816
logger = logging.getLogger(__name__)
1917

2018

21-
async def start_agent() -> None:
22-
client = AsyncStream()
23-
19+
async def create_agent(**kwargs) -> Agent:
2420
agent = Agent(
2521
edge=getstream.Edge(),
2622
agent_user=User(
@@ -30,17 +26,20 @@ async def start_agent() -> None:
3026
llm=gemini.Realtime(),
3127
processors=[], # processors can fetch extra data, check images/audio data or transform video
3228
)
33-
34-
call = client.video.call("default", str(uuid4()))
29+
return agent
3530

3631

32+
async def join_call(agent: Agent, call_type: str, call_id: str, **kwargs) -> None:
33+
# ensure the agent user is created
34+
await agent.create_user()
35+
# Create a call
36+
call = await agent.create_call(call_type, call_id)
3737

3838
with await agent.join(call):
39-
await asyncio.sleep(5)
4039
await agent.edge.open_demo(call)
4140
await agent.llm.simple_response(text="Describe what you see and say hi")
4241
await agent.finish() # run till the call ends
4342

4443

4544
if __name__ == "__main__":
46-
asyncio.run(start_agent())
45+
cli(AgentLauncher(create_agent=create_agent, join_call=join_call))

0 commit comments

Comments
 (0)