<@U084APK40NS> for my usage, i have 2 part in my w...
# ask-ai
d
@millions-hair-24956 for my usage, i have 2 part in my webcall with agent. I want 1 agent for the first part, and after another agent with another prompt will handle the call. How to handle this in python ?
g
To handle different parts of a web call with different agents in LiveKit, you can use the explicit agent dispatch feature. This allows you to control which agent is assigned to a room at different stages of the call. You can dispatch agents explicitly using the
AgentDispatchService
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:
Copy code
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.
d
@millions-hair-24956 can i in a single agent use different prompt and voicepipeline ?
g
Yes, you can use different prompts and voice pipelines within a single agent by dynamically updating the agent's configuration during the session. This can be achieved by modifying the
chat_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:
Copy code
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.
d
@millions-hair-24956 does the context start from 0 ? and how can I use a function started by the first agent to go to the second voicepipeline ?
g
Yes, the context can be reset to start from zero by creating a new
ChatContext
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.
Copy code
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.
d
@millions-hair-24956 here is the error i get : AttributeError: property 'chat_ctx' of 'VoicePipelineAgent' object has no setter
@millions-hair-24956 here is the error i get : AttributeError: property 'chat_ctx' of 'VoicePipelineAgent' object has no setter
g
The error "AttributeError: property 'chat_ctx' of 'VoicePipelineAgent' object has no setter" indicates that the
chat_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`:
Copy code
# 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.
The error "AttributeError: property 'chat_ctx' of 'VoicePipelineAgent' object has no setter" indicates that the
chat_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`:
Copy code
# 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.