RespondAgent to create a simple agent that can be passed into StreamingConversation.
To do so, you must create an agent type, create an agent config, and then create your agent subclass. In the examples below, we will create an agent that responds with the same message no matter what is said to it, called BrokenRecordAgent.
Agent type
Each agent has a unique agent type string that is checked in various parts of Vocode, most notably in the factories that create agents. So, you must create a new type for your custom agent. See theAgentType enum in vocode/streaming/models/agent.py for examples.
For our BrokenRecordAgent, we will use “agent_broken_record” as our type.
Agent config
Your agent must have a corresponding agent config that is a subclass ofAgentConfig and is (JSON-serializable). Serialization is automatically handled by Pydantic.
The agent config should only contain the information you need to deterministically create the same agent each time. This means with the same parameters in your config, the corresponding agent should have the same behavior each time you create it.
For our BrokenRecordAgent, we create a config like:
Custom Agent
Now, you can create your custom agent subclass ofRespondAgent. In your class header, pass in RespondAgent with a your agent type as a type hint. This should look like RespondAgent[Your_Agent_Type].
Each agent should override the generate_response() async method to support streaming and respond() method to support turn-based conversations.
If you want to only support turn-based conversations, you do not have to overwriteThegenerate_response()but you MUST setgenerate_response=Falsein your agent config (seeChatVertexAIAgentConfiginvocode/streaming/models/agent.pyfor an example). Otherwise, you must ALWAYS implement thegenerate_response()async method.
generate_response() method returns an AsyncGenerator of tuples containing each message/sentence and a boolean for whether that message can be interrupted by the human speaking. You can automatically create this generator by yielding instead of returning (see example below).
We will now define our BrokenRecordAgent. Since we simply return the same message each time, we can return and yield that message in respond() and generate_response(), respectively:
