cat-fact-agent
Version:
Cat Fact Agent - 专业的猫知识 AI 代理
45 lines (42 loc) • 1.56 kB
JavaScript
import { Agent } from "@mastra/core/agent";
import { createTool } from "@mastra/core/tools";
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
const zhipu = createOpenAICompatible({
name: "zhipu",
apiKey: "5f809a60bdc04074a4b7b8b845fa8eae.0fnSVCeiWWCk4TwQ",
baseURL: "https://open.bigmodel.cn/api/paas/v4",
});
import { z } from "zod";
const instructions = `你是一个有用的猫咪专家助手。在讨论猫咪时,你应该始终包含一个有趣的猫咪知识。
你的主要职责:
1. 回答关于猫咪的问题
2. 使用 catFact 工具提供经过验证的猫咪知识
3. 自然地融入猫咪知识到你的回答中
始终在你的回答中至少使用一次 catFact 工具以确保准确性。`;
const getCatFact = async () => {
const { fact } = (await fetch("https://catfact.ninja/fact").then((res) => res.json()));
console.log("fact:", fact);
return fact;
};
const catFact = createTool({
id: "Get cat facts",
inputSchema: z.object({}),
description: "Fetches cat facts",
execute: async () => {
console.log("using tool to fetch cat fact");
return {
catFact: await getCatFact(),
};
},
});
const catOne = new Agent({
name: "cat-one",
instructions: instructions,
model: zhipu("glm-4-flash"),
tools: {
catFact,
},
});
const result = await catOne.generate("告诉我一个关于猫咪的有趣知识");
// const result = await catOne.generate("猫能活多久? 请用中文回答");
console.log(result.text);