Is it possible to provide dynamic instructions on ...
# ask-ai
f
Is it possible to provide dynamic instructions on top of the initial instructions an agent was initialized with every time the Realtime agent responds?
t
Thanks for reaching out, Xavier. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, it is possible to provide dynamic instructions to a Realtime agent on top of its initial instructions. You can use the
update_instructions
method to update the agent's core instructions dynamically. For example:
Copy code
await agent.update_instructions(new_instructions)
This method updates the instructions for the ongoing realtime session as well. However, please note that if the update fails during a realtime session, it will raise a
llm.RealtimeError
exception. Sources: livekit.agents API documentation
f
Can I run generate_reply() everytime it responds with additional instructions?
t
Yes, you can use the
generate_reply
method to generate a response from the agent while providing additional instructions. The method includes an optional
instructions
parameter that allows you to guide the reply generation dynamically. For example:
Copy code
def generate_reply(
    self,
    *,
    user_input: NotGivenOr[str] = NOT_GIVEN,
    instructions: NotGivenOr[str] = NOT_GIVEN,
    tool_choice: NotGivenOr[llm.ToolChoice] = NOT_GIVEN,
    allow_interruptions: NotGivenOr[bool] = NOT_GIVEN,
) -> SpeechHandle:
By using the
instructions
parameter, you can provide specific guidance to influence how the agent formulates its response. Sources: livekit.agents API documentation
f
and where can I call that for the agent to listen to those intructions, which handler can I use?
t
You can call the
generate_reply
method within the context of an agent session to provide additional instructions. For example:
Copy code
speech_handle = session.generate_reply(
    instructions="Your specific instructions here",
    user_input="Optional user input here",
    tool_choice=optional_tool_choice,
    allow_interruptions=optional_boolean
)
This method can be invoked wherever you manage the agent's session and need to dynamically guide its responses. The
instructions
parameter allows you to specify the additional guidance for the agent's reply generation. Sources: livekit.agents API documentation
f
What is the event handler I'd call it in so it's applied before the agent responds?
t
Unfortunately, I couldn't find specific information about an event handler that automatically calls
generate_reply
before the agent responds. However, you can manually invoke
generate_reply
within the context of your agent's session management to ensure the additional instructions are applied before the response is generated. If you need further assistance with this, please let me know!