delightful-air-266
02/25/2025, 11:17 PMgray-monkey-83525
02/25/2025, 11:17 PMAgentDispatchService
in Python.
1. Initialize the First Agent: Use the create_dispatch
method to assign the first agent to the room for the initial part of the call. This agent will handle the first part of the conversation.
2. Switch to the Second Agent: Once the first part is complete, you can remove the first agent and dispatch the second agent with a different prompt. This can be done by calling create_dispatch
again with the second agent's configuration.
Here's a basic example of how you might implement this:
import asyncio
from livekit import api
async def switch_agents(room_name, first_agent_name, second_agent_name):
lkapi = api.LiveKitAPI()
# Dispatch the first agent
await lkapi.agent_dispatch.create_dispatch(
api.CreateAgentDispatchRequest(agent_name=first_agent_name, room=room_name)
)
print(f"First agent {first_agent_name} dispatched to {room_name}")
# Simulate the first part of the call
await asyncio.sleep(10) # Replace with actual condition to switch agents
# Remove the first agent and dispatch the second agent
await lkapi.agent_dispatch.create_dispatch(
api.CreateAgentDispatchRequest(agent_name=second_agent_name, room=room_name)
)
print(f"Second agent {second_agent_name} dispatched to {room_name}")
asyncio.run(switch_agents("my-room", "first-agent", "second-agent"))
Would you like more details on any specific part of this process?
Sources:
- Dispatching agents 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.delightful-air-266
02/25/2025, 11:20 PMgray-monkey-83525
02/25/2025, 11:20 PMchat_ctx
for different prompts and adjusting the components of the VoicePipelineAgent
as needed.
1. Update the Prompt: You can change the prompt by updating the chat_ctx
attribute of the agent. This allows you to modify the context that is sent to the LLM.
2. Switch Voice Pipeline Components: If you need to change the components of the voice pipeline (e.g., STT, TTS), you can reconfigure the VoicePipelineAgent
with different components as required.
Here's a basic example of how you might implement this:
from livekit.agents import llm, VoicePipelineAgent
from livekit.plugins import openai, deepgram, silero
# Initial setup
initial_ctx = llm.ChatContext().append(role="system", text="Initial prompt")
agent = VoicePipelineAgent(
vad=silero.VAD.load(),
stt=deepgram.STT(model="nova-2-general"),
llm=openai.LLM(),
tts=openai.TTS(),
chat_ctx=initial_ctx,
)
# Update prompt
new_ctx = llm.ChatContext().append(role="system", text="New prompt for second part")
agent.chat_ctx = new_ctx
# Optionally, switch components
agent.stt = deepgram.STT(model="another-model")
agent.tts = openai.TTS(voice="new-voice")
# Start the agent
agent.start(room, participant)
Would you like more detailed guidance on any specific part of this process?
Sources:
- Updating chat context 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.delightful-air-266
02/25/2025, 11:21 PMgray-monkey-83525
02/25/2025, 11:21 PMChatContext
for the agent. This allows you to clear the previous conversation history and start fresh with a new prompt or context.
To use a function started by the first agent to transition to the second voice pipeline, you can leverage function calling capabilities. This involves defining a function within the FunctionContext
of the first agent that, when triggered, initiates the transition to the second agent or voice pipeline.
Here's a basic example of how you might implement this:
1. Reset Context: Create a new ChatContext
to reset the context.
2. Function to Transition: Define a function in the FunctionContext
of the first agent that will handle the transition.
from livekit.agents import llm, VoicePipelineAgent
from livekit.plugins import openai, deepgram, silero
# Define a function context with a transition function
class TransitionFunctionContext(llm.FunctionContext):
@llm.ai_callable()
async def switch_to_second_pipeline(self):
# Logic to switch to the second voice pipeline
print("Switching to second voice pipeline")
# Reset context
new_ctx = llm.ChatContext().append(role="system", text="New prompt for second part")
agent.chat_ctx = new_ctx
# Optionally, switch components
agent.stt = deepgram.STT(model="another-model")
agent.tts = openai.TTS(voice="new-voice")
# Initial setup
initial_ctx = llm.ChatContext().append(role="system", text="Initial prompt")
fnc_ctx = TransitionFunctionContext()
agent = VoicePipelineAgent(
vad=silero.VAD.load(),
stt=deepgram.STT(model="nova-2-general"),
llm=openai.LLM(),
tts=openai.TTS(),
chat_ctx=initial_ctx,
fnc_ctx=fnc_ctx,
)
# Start the agent
agent.start(room, participant)
Would you like more detailed guidance on any specific part of this process?
Sources:
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1730764131.357979
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1728124961.554009 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.delightful-air-266
02/25/2025, 11:56 PMdelightful-air-266
02/25/2025, 11:57 PMgray-monkey-83525
02/26/2025, 12:02 AMchat_ctx
property of the VoicePipelineAgent
is read-only and cannot be directly modified after the agent has been initialized.
To update the chat context, you should modify the existing ChatContext
object rather than trying to assign a new one. You can append new messages or clear the existing messages in the ChatContext
to effectively reset or update the context.
Here's how you can modify the `ChatContext`:
# Assuming 'agent' is your VoicePipelineAgent instance
# Clear existing messages
agent.chat_ctx.messages.clear()
# Append new prompt
agent.chat_ctx.append(role="system", text="New prompt for the second part")
Would you like more detailed guidance on any specific part of this process?
Sources:
- https://docs.livekit.io/python/livekit/agents/pipeline/index.html#livekit.agents.pipeline.VoicePipelineAgent.MIN_TIME_PLAYED_FOR_COMMIT
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731000521.843349 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.gray-monkey-83525
02/26/2025, 12:04 AMchat_ctx
property is read-only and cannot be directly reassigned after the VoicePipelineAgent
has been initialized. Instead of trying to set a new ChatContext
, you should modify the existing ChatContext
by clearing its messages or appending new ones.
Here's how you can modify the `ChatContext`:
# Assuming 'agent' is your VoicePipelineAgent instance
# Clear existing messages
agent.chat_ctx.messages.clear()
# Append new prompt
agent.chat_ctx.append(role="system", text="New prompt for the second part")
This approach allows you to effectively reset or update the context without needing to reassign the chat_ctx
property.
Would you like more detailed guidance on any specific part of this process?
Sources:
- https://docs.livekit.io/python/livekit/plugins/openai/realtime/index.html#livekit.plugins.openai.realtime.RealtimeSession.Conversation
- https://docs.livekit.io/python/livekit/plugins/openai/realtime/realtime_model.html#livekit.plugins.openai.realtime.realtime_model.RealtimeSession.Conversation 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.