Skip to content

Commit 8b81be6

Browse files
authored
move the LLM instance directly to Assistant to make it cleaner to share with tests (#71)
* Attach models to agent subclass * Fix up * comment * ruff * 4.1-mini * remove comment
1 parent b27bf9e commit 8b81be6

2 files changed

Lines changed: 15 additions & 27 deletions

File tree

src/agent.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919

2020
load_dotenv(".env.local")
2121

22-
AGENT_MODEL = "openai/gpt-5.2-chat-latest"
23-
2422

2523
class Assistant(Agent):
2624
def __init__(self) -> None:
2725
super().__init__(
26+
# A Large Language Model (LLM) is your agent's brain, processing user input and generating a response
27+
# See all available models at https://docs.livekit.io/agents/models/llm/
28+
llm=inference.LLM(model="openai/gpt-5.2-chat-latest"),
29+
# To use a realtime model instead of a voice pipeline, replace the LLM
30+
# with a RealtimeModel and remove the STT/TTS from the AgentSession
31+
# (Note: This is for the OpenAI Realtime API. For other providers, see https://docs.livekit.io/agents/models/realtime/)
32+
# 1. Install livekit-agents[openai]
33+
# 2. Set OPENAI_API_KEY in .env.local
34+
# 3. Add `from livekit.plugins import openai` to the top of this file
35+
# 4. Replace the llm argument with:
36+
# llm=openai.realtime.RealtimeModel(voice="marin")
2837
instructions=textwrap.dedent(
2938
"""\
3039
You are a friendly, reliable voice assistant that answers questions, explains topics, and completes tasks with available tools.
@@ -103,9 +112,6 @@ async def my_agent(ctx: JobContext):
103112
# Speech-to-text (STT) is your agent's ears, turning the user's speech into text that the LLM can understand
104113
# See all available models at https://docs.livekit.io/agents/models/stt/
105114
stt=inference.STT(model="deepgram/nova-3", language="multi"),
106-
# A Large Language Model (LLM) is your agent's brain, processing user input and generating a response
107-
# See all available models at https://docs.livekit.io/agents/models/llm/
108-
llm=inference.LLM(model=AGENT_MODEL),
109115
# Text-to-speech (TTS) is your agent's voice, turning the LLM's text into speech that the user can hear
110116
# See all available models as well as voice selections at https://docs.livekit.io/agents/models/tts/
111117
tts=inference.TTS(
@@ -120,16 +126,6 @@ async def my_agent(ctx: JobContext):
120126
preemptive_generation=True,
121127
)
122128

123-
# To use a realtime model instead of a voice pipeline, use the following session setup instead.
124-
# (Note: This is for the OpenAI Realtime API. For other providers, see https://docs.livekit.io/agents/models/realtime/))
125-
# 1. Install livekit-agents[openai]
126-
# 2. Set OPENAI_API_KEY in .env.local
127-
# 3. Add `from livekit.plugins import openai` to the top of this file
128-
# 4. Use the following session setup instead of the version above
129-
# session = AgentSession(
130-
# llm=openai.realtime.RealtimeModel(voice="marin")
131-
# )
132-
133129
# Start the session, which initializes the voice pipeline and warms up the models
134130
await session.start(
135131
agent=Assistant(),

tests/test_agent.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,19 @@
33
import pytest
44
from livekit.agents import AgentSession, inference, llm
55

6-
from agent import AGENT_MODEL, Assistant
7-
8-
9-
def _agent_llm() -> llm.LLM:
10-
return inference.LLM(model=AGENT_MODEL)
6+
from agent import Assistant
117

128

139
def _judge_llm() -> llm.LLM:
14-
# The judge LLM can be a cheaper model since it only evaluates agent responses
1510
return inference.LLM(model="openai/gpt-4.1-mini")
1611

1712

1813
@pytest.mark.asyncio
1914
async def test_offers_assistance() -> None:
2015
"""Evaluation of the agent's friendly nature."""
2116
async with (
22-
_agent_llm() as agent_llm,
2317
_judge_llm() as judge_llm,
24-
AgentSession(llm=agent_llm) as session,
18+
AgentSession() as session,
2519
):
2620
await session.start(Assistant())
2721

@@ -54,9 +48,8 @@ async def test_offers_assistance() -> None:
5448
async def test_grounding() -> None:
5549
"""Evaluation of the agent's ability to refuse to answer when it doesn't know something."""
5650
async with (
57-
_agent_llm() as agent_llm,
5851
_judge_llm() as judge_llm,
59-
AgentSession(llm=agent_llm) as session,
52+
AgentSession() as session,
6053
):
6154
await session.start(Assistant())
6255

@@ -99,9 +92,8 @@ async def test_grounding() -> None:
9992
async def test_refuses_harmful_request() -> None:
10093
"""Evaluation of the agent's ability to refuse inappropriate or harmful requests."""
10194
async with (
102-
_agent_llm() as agent_llm,
10395
_judge_llm() as judge_llm,
104-
AgentSession(llm=agent_llm) as session,
96+
AgentSession() as session,
10597
):
10698
await session.start(Assistant())
10799

0 commit comments

Comments
 (0)