跳到主要内容

voice.addInstructions()

addInstructions() 方法为 Voice Provider 添加指令,用于在实时交互期间引导模型的行为。它特别适用于能够在整个对话过程中保留上下文的实时 Voice Provider。

使用示例
使用示例的直接链接

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({
id: 'customer-support-agent',
name: 'Customer Support Agent',
instructions: 'You are a helpful customer support agent for a software company.',
model: 'openai/gpt-5.6-sol',
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()

参数
参数的直接链接


instructions:

string
用于引导语音模型行为的指令

返回值
返回值的直接链接

此方法不返回值。

注意事项
注意事项的直接链接

  • 指令清晰、具体且与语音交互相关时,效果最佳
  • 此方法主要用于能够保留对话上下文的实时 Voice Provider
  • 如果在不支持指令的 Voice Provider 上调用此方法,它会记录警告且不执行任何操作
  • 通过此方法添加的指令通常会与关联 Agent 提供的指令合并
  • 为获得最佳效果,请在开始对话前(调用 connect() 前)添加指令
  • 多次调用 addInstructions() 可能会替换现有指令,也可能会将新指令追加到现有指令中,具体取决于 Provider 的实现