跳至主要內容

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() 前)加入指示
  • 視乎 provider 的實作方式,多次呼叫 addInstructions() 可能會取代現有指示,或附加至現有指示