voice.addInstructions()
addInstructions() 方法为语音提供商配备了指导模型在实时交互中行为的指令。这对于在整个对话中保持上下文的实时语音提供商尤其有用。
🌐 The addInstructions() method equips a voice provider with instructions that guide the model's behavior during real-time interactions. This is particularly useful for real-time voice providers that maintain context across a conversation.
使用示例Direct link to 使用示例
🌐 Usage Example
import { OpenAIRealtimeVoice } from "@mastra/voice-openai-realtime";
import { Agent } from "@mastra/core/agent";
// Initialize a real-time voice provider
const voice = new OpenAIRealtimeVoice({
realtimeConfig: {
model: "gpt-5.1-realtime",
apiKey: process.env.OPENAI_API_KEY,
},
});
// Create an agent with the voice provider
const agent = new Agent({
name: "Customer Support Agent",
instructions:
"You are a helpful customer support agent for a software company.",
model: "openai/gpt-5.1",
voice,
});
// Add additional instructions to the voice provider
voice.addInstructions(`
When speaking to customers:
- Always introduce yourself as the customer support agent
- Speak clearly and concisely
- Ask clarifying questions when needed
- Summarize the conversation at the end
`);
// Connect to the real-time service
await voice.connect();
参数Direct link to 参数
🌐 Parameters
instructions:
string
Instructions to guide the voice model's behavior
返回值Direct link to 返回值
🌐 Return Value
此方法不返回值。
🌐 This method does not return a value.
注意Direct link to 注意
🌐 Notes
- 指示在清晰、具体且与语音互动相关时最有效
- 该方法主要用于维护对话上下文的实时语音提供商
- 如果调用不支持指令的语音提供商,它会记录一条警告并且不会执行任何操作
- 通过这种方法添加的指令通常会与关联代理提供的任何指令结合使用
- 为了获得最佳效果,请在开始对话之前添加说明(在调用
connect()之前) - 多次调用
addInstructions()可能会根据提供商的实现方式,替换或附加到现有指令上