> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # ![Mastra 標誌](https://mastra.ai/brand/logo.svg)Mastra Gateway 是內置 [Observational Memory](https://gateway.mastra.ai/docs/features#observational-memory)、與 OpenAI 相容的 API 代理。只需將任何 HTTP 用戶端、SDK 或框架指向 Gateway,每段對話便會自動記錄,毋須編寫任何記憶管理程式碼。 詳情請參閱[Gateway 文件](https://gateway.mastra.ai/docs)。 ## 取得 API 金鑰 前往 [gateway.mastra.ai](https://gateway.mastra.ai) 註冊 Mastra 帳戶。完成啟用流程時,你會取得個人 API 金鑰,用於驗證請求。 ## 用法 將 API 金鑰定義為環境變數: ```bash MASTRA_GATEWAY_API_KEY=your-gateway-key ``` 設定 Gateway 模型 ID: ```typescript import { Agent } from '@mastra/core/agent' const agent = new Agent({ id: 'my-agent', name: 'My Agent', instructions: 'You are a helpful assistant', model: 'mastra/openai/gpt-5-mini', }) ``` 對於需要經 Gateway 運行的嵌入模型,請使用相同前綴: ```typescript import { ModelRouterEmbeddingModel } from '@mastra/core/llm' const embedder = new ModelRouterEmbeddingModel('mastra/openai/text-embedding-3-small') ``` 產生或串流回應時傳入 `memory.thread` 及 `memory.resource`,即可啟用 Observational Memory: ```typescript import { weatherAgent } from './agents/weather-agent' const memory = { thread: 'assistant-thread-1', resource: 'user-42', } const result = await weatherAgent.stream('My name is Alex and I prefer concise answers.', { memory, }) for await (const chunk of result.textStream) { process.stdout.write(chunk) } ``` ## 設定 ```bash # Use gateway API key MASTRA_GATEWAY_API_KEY=your-gateway-key ``` ## 延伸閱讀 - [功能](https://gateway.mastra.ai/docs/features) - [模型](https://gateway.mastra.ai/docs/models) - [限制](https://gateway.mastra.ai/docs/limits) - [API 參考](https://gateway.mastra.ai/docs/api/overview) - [範例](https://gateway.mastra.ai/docs/examples/)