how can I update main instructions assigned to Age...
# ask-ai
a
how can I update main instructions assigned to Agent dynamically after agent is defined and initiated with original prompt? how can I update system instruction prompt dynamically for the agent?
t
Thanks for reaching out, Hiren. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please help me understand a bit more? Are you looking to update system instructions for a specific LiveKit feature, like a bot or AI agent, or is this about a custom integration? Also, if you could share a little about the context or how you’re planning to use it, that would really help me give you the best possible guidance. I’m here to help!
a
ai agent yes.. inside and AgentSession
t
To dynamically update system instructions for an AI agent inside an AgentSession, you can use the update_instructions method provided by the Agent class. This handy method lets you adjust the agent's behavior on the fly by changing its instructions. Let me walk you through how it works: • Method Signature:
async def update_instructions(self, instructions: str) -> None
• Parameter:
instructions
(str) - The new system instructions you'd like to set for the agent. • Behavior: These updates take effect in future interactions, and for agents using a
RealtimeModel
, the changes are synced with the server-side session seamlessly. Example Usage:
Copy code
async def on_enter(self, ctx: JobContext):
    new_instructions = "You are a support agent. Be professional and helpful."
    try:
        await self.update_instructions(new_instructions)
        print("Instructions updated successfully.")
    except Exception as e:
        print(f"Failed to update instructions: {e}")
This method helps ensure your updated instructions are applied smoothly in all upcoming interactions. If you have any questions or want me to provide more examples, just let me know—I'm here to help! Sources: examples/voice_agents/dynamic_tool_creation.py
a
I dont want to update instructions in "on_enter" event.. can we create a function inside the Agent class which I can trigger from outside the agent class to update the instructions?
t
You can create a function inside the Agent class to update instructions dynamically from outside the class. The update_instructions method provided by the Agent class is designed for this purpose and can be called externally to modify the agent's behavior at runtime. Here’s an example of how you can define and use such a function:
Copy code
class CustomAgent(Agent):
    def __init__(self):
        super().__init__(
            instructions="Default instructions",
            stt="deepgram/stt",
            llm="openai/gpt-4o-mini",
            tts="elevenlabs/tts"
        )

    async def update_agent_instructions(self, new_instructions: str):
        try:
            await self.update_instructions(new_instructions)
            print("Instructions updated successfully.")
        except Exception as e:
            print(f"Failed to update instructions: {e}")

# Example usage from outside the class
agent = CustomAgent()
await agent.update_agent_instructions("You are a helpful assistant. Respond concisely.")
This approach lets you easily update the instructions from outside the class by calling your custom update_agent_instructions function. If you’d like, I’m here to help with any questions or if you want me to walk you through more examples! Sources: examples/voice_agents/restaurant_agent.py | multi-agent/long_or_short_agent.py