Agent.generateLegacy()(遗留)
🌐 Agent.generateLegacy() (Legacy)
已弃用:此方法已被弃用,仅适用于 V1 模型。对于 V2 模型,请改用新的 .generate() 方法。
.generateLegacy() 方法是代理生成 API 的旧版本,用于与 V1 模型代理交互以生成文本或结构化响应。该方法接受消息和可选的生成选项。
🌐 The .generateLegacy() method is the legacy version of the agent generation API, used to interact with V1 model agents to produce text or structured responses. This method accepts messages and optional generation options.
使用示例Direct link to 使用示例
🌐 Usage example
await agent.generateLegacy("message for agent");
参数Direct link to 参数
🌐 Parameters
messages:
options?:
选项参数Direct link to 选项参数
🌐 Options parameters
abortSignal?:
context?:
structuredOutput?:
schema:
model:
errorStrategy?:
fallbackValue?:
instructions?:
outputProcessors?:
inputProcessors?:
experimental_output?:
instructions?:
output?:
memory?:
thread:
resource:
options?:
maxSteps?:
maxRetries?:
onStepFinish?:
runId?:
telemetry?:
isEnabled?:
recordInputs?:
recordOutputs?:
functionId?:
temperature?:
toolChoice?:
'auto':
'none':
'required':
{ type: 'tool'; toolName: string }:
toolsets?:
clientTools?:
savePerStep?:
providerOptions?:
openai?:
anthropic?:
google?:
[providerName]?:
requestContext?:
maxTokens?:
topP?:
topK?:
presencePenalty?:
frequencyPenalty?:
stopSequences?:
seed?:
headers?:
返回Direct link to 返回
🌐 Returns
text?:
object?:
toolCalls?:
toolName:
args:
迁移到新APIDirect link to 迁移到新API
🌐 Migration to New API
新的 .generate() 方法提供了增强的功能,包括兼容 AI SDK v5+、更好的结构化输出处理以及改进的流式支持。有关详细的迁移说明,请参阅 迁移指南。
🌐 The new .generate() method offers enhanced capabilities including AI SDK v5+ compatibility, better structured output handling, and improved streaming support. See the migration guide for detailed migration instructions.
快速迁移示例Direct link to 快速迁移示例
🌐 Quick Migration Example
以前(传统)Direct link to 以前(传统)
🌐 Before (Legacy)
const result = await agent.generateLegacy("message", {
temperature: 0.7,
maxSteps: 3,
});
在(新 API)之后Direct link to 在(新 API)之后
🌐 After (New API)
const result = await agent.generate("message", {
modelSettings: {
temperature: 0.7,
},
maxSteps: 3,
});
扩展使用示例Direct link to 扩展使用示例
🌐 Extended usage example
import { z } from "zod";
import {
ModerationProcessor,
TokenLimiterProcessor,
} from "@mastra/core/processors";
await agent.generateLegacy(
[
{ role: "user", content: "message for agent" },
{
role: "user",
content: [
{
type: "text",
text: "message for agent",
},
{
type: "image",
imageUrl: "https://example.com/image.jpg",
mimeType: "image/jpeg",
},
],
},
],
{
temperature: 0.7,
maxSteps: 3,
memory: {
thread: "user-123",
resource: "test-app",
},
toolChoice: "auto",
providerOptions: {
openai: {
reasoningEffort: "high",
},
},
// Structured output with better DX
structuredOutput: {
schema: z.object({
sentiment: z.enum(["positive", "negative", "neutral"]),
confidence: z.number(),
}),
model: "openai/gpt-5.1",
errorStrategy: "warn",
},
// Output processors for response validation
outputProcessors: [
new ModerationProcessor({ model: "openai/gpt-4.1-nano" }),
new TokenLimiterProcessor({ maxTokens: 1000 }),
],
},
);
相关Direct link to 相关
🌐 Related