rapid-shampoo-83551
08/05/2025, 6:50 AMjolly-air-155
08/05/2025, 7:11 AMawait fast_llm_fut in the example to run the main LLM in parallel, or call session.generate_reply in the function call insteadrapid-shampoo-83551
08/05/2025, 2:32 PMjolly-air-155
08/05/2025, 2:53 PMrapid-shampoo-83551
08/05/2025, 4:53 PMimport asyncio
import logging
from collections.abc import AsyncIterable
import random
from typing import Annotated
from dotenv import load_dotenv
from livekit.agents import (
Agent,
AgentSession,
JobContext,
WorkerOptions,
cli,
llm,
)
from livekit.agents.llm.chat_context import ChatContext, ChatMessage
from livekit.plugins import deepgram, groq, openai, silero
from livekit.agents.llm import function_tool
from pydantic import Field
logger = logging.getLogger("pre-reseponse-agent")
load_dotenv()
class PreResponseAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful assistant",
llm=groq.LLM(model="llama-3.3-70b-versatile"),
)
self._fast_llm = groq.LLM(model="llama-3.1-8b-instant")
self._fast_llm_prompt = llm.ChatMessage(
role="system",
content=[
"Tell the user a short poem about the weather, using simple and natural language.",
],
)
async def on_enter(self) -> None:
self.session.generate_reply()
@function_tool
async def get_number(
self,
value: Annotated[int | None, Field(description="The number value")],
) -> str:
"""
Called when the user wants to get a number value, None if user want a random value
"""
logger.info(f"Get number called with value: {value}")
return f"The number value is 5."
async def on_user_turn_completed(self, turn_ctx: ChatContext, new_message: ChatMessage):
# Create a short "silence filler" response to quickly acknowledge the user's input
fast_llm_ctx = turn_ctx.copy(
exclude_instructions=True, exclude_function_call=True
).truncate(max_items=3)
fast_llm_ctx.items.insert(0, self._fast_llm_prompt)
fast_llm_ctx.items.append(new_message)
# Intentionally not awaiting SpeechHandle to allow the main response generation to
# run concurrently
self.session.say(
self._fast_llm.chat(chat_ctx=fast_llm_ctx).to_str_iterable(),
add_to_chat_ctx=False,
)
async def entrypoint(ctx: JobContext):
session = AgentSession(
stt=deepgram.STT(),
tts=openai.TTS(),
vad=silero.VAD.load(),
)
await session.start(PreResponseAgent(), room=ctx.room)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))jolly-air-155
08/05/2025, 11:59 PMjolly-air-155
08/06/2025, 12:00 AM