UNPKG

cat-fact-agent

Version:

Cat Fact Agent - 专业的猫知识 AI 代理

74 lines (71 loc) 2.42 kB
#!/usr/bin/env node import { Agent } from "@mastra/core/agent"; import { createTool } from "@mastra/core/tools"; import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; import { z } from "zod"; const zhipu = createOpenAICompatible({ name: "zhipu", apiKey: "5f809a60bdc04074a4b7b8b845fa8eae.0fnSVCeiWWCk4TwQ", baseURL: "https://open.bigmodel.cn/api/paas/v4", }); const instructions = `你是一个有用的猫咪专家助手。在讨论猫咪时,你应该始终包含一个有趣的猫咪知识。 你的主要职责: 1. 回答关于猫咪的问题 2. 使用 catFact 工具提供经过验证的猫咪知识 3. 自然地融入猫咪知识到你的回答中 始终在你的回答中至少使用一次 catFact 工具以确保准确性。`; const getCatFact = async () => { const { fact } = (await fetch("https://catfact.ninja/fact").then((res) => res.json())); return fact; }; const catFact = createTool({ id: "Get cat facts", inputSchema: z.object({}), description: "Fetches cat facts", execute: async () => { console.log("🔍 正在获取猫咪知识..."); return { catFact: await getCatFact(), }; }, }); const catOne = new Agent({ name: "cat-one", instructions: instructions, model: zhipu("glm-4-flash"), tools: { catFact, }, }); async function main() { const args = process.argv.slice(2); if (args.length === 0) { // 无参数时,自动生成随机猫咪知识 try { console.log("🐱 猫咪知识代理 CLI"); console.log("⏳ 正在为你获取一个有趣的猫咪知识...\n"); const result = await catOne.generate("告诉我一个关于猫咪的有趣知识"); console.log("🐱 回答:"); console.log(result.text); } catch (error) { console.error("❌ 错误:", error); process.exit(1); } return; } // 有参数时,使用用户提供的问题 const question = args.join(" "); try { console.log("🤔 问题:", question); console.log("⏳ 正在思考中...\n"); const result = await catOne.generate(question); console.log("🐱 回答:"); console.log(result.text); } catch (error) { console.error("❌ 错误:", error); process.exit(1); } } main();