diff --git a/src/index.ts b/src/index.ts index 9931b36..13fc33a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { PROMPTS } from './prompts/system.js'; import { getOpenAITools, findTool } from './tools/registry.js'; import * as readline from 'node:readline/promises'; -const promptName = process.argv[2] || 'agent'; +const promptName = process.argv[2] || 'reAct'; const systemPrompt = PROMPTS[promptName as keyof typeof PROMPTS]; if (!systemPrompt) { @@ -33,7 +33,7 @@ while (true) { let reply = await chat(context.getMessages(), tools); // 如果模型要求调用工具,执行并在上下文中构造 tool 消息 - if (reply.tool_calls && reply.tool_calls.length > 0) { + while (reply.tool_calls && reply.tool_calls.length > 0) { // 先把 assistant 消息(含 tool_calls)加入上下文 context.add({ role: 'assistant', content: '', tool_calls: reply.tool_calls } as any); diff --git a/src/prompts/reAct.ts b/src/prompts/reAct.ts new file mode 100644 index 0000000..ad64baf --- /dev/null +++ b/src/prompts/reAct.ts @@ -0,0 +1,16 @@ +export const REACT_SYSTEM_PROMPT = `你是一个自主智能体,能够使用工具来完成目标。 + +## 工作方式 +你需要反复执行以下步骤,直到目标完成: + +1. **思考**:分析当前状态,决定下一步行动。 +2. **行动**:调用一个工具,或者给出最终答案。 + +## 行动格式 +- 如果需要调用工具,只返回工具调用的 JSON,不要其他内容。 + +## 重要规则 +- 如果工具返回了错误,分析错误并尝试修复,不要重复相同的错误调用。 +- 如果连续三次调用没有进展,给出最终答案并说明遇到困难。 +- 诚实:如果无法完成,直接说明,不要编造。 +- 使用中文回复。`; \ No newline at end of file diff --git a/src/prompts/system.ts b/src/prompts/system.ts index d99bef1..8e2a0ab 100644 --- a/src/prompts/system.ts +++ b/src/prompts/system.ts @@ -1,3 +1,5 @@ +import { REACT_SYSTEM_PROMPT } from './reAct.js'; + export const PROMPTS = { default: '你是一个直爽的代码审查员,回答尽量简洁。', toxic: '你是一个毒舌代码审查员,用讽刺的语气表达。', @@ -6,6 +8,8 @@ export const PROMPTS = { "answer": "你的回答", "confidence": 0.0-1.0 之间的数字 }`, - agent: `你是一个有工具调用能力的助手。当需要查询信息或执行计算时,请使用提供的工具。 + agent: `你是一个有工具调用能力的助手。当需要查询信息/执行计算或者玩猜谜游戏时,请使用提供的工具。 +如果调用了工具,根据工具执行结果给出答案。 如果不需要工具,直接回答即可。回答简洁。`, + reAct: REACT_SYSTEM_PROMPT, } as const; \ No newline at end of file diff --git a/src/tools/guess.ts b/src/tools/guess.ts new file mode 100644 index 0000000..49088ca --- /dev/null +++ b/src/tools/guess.ts @@ -0,0 +1,20 @@ +import { Tool } from '../types/index.js'; + +export const guessTool: Tool = { + name: 'guess_number', + description: '猜一个1到100之间的整数。返回“大了”、“小了”或“猜对了”。', + parameters: { + type: 'object', + properties: { + number: { type: 'number', description: '你猜的数字' }, + }, + required: ['number'], + }, + execute: async (args) => { + // 答案写死在工具内部,模型绝对不知道 + const answer = 67; + const guess = args.number; + if (guess === answer) return '猜对了!'; + return guess > answer ? '大了' : '小了'; + }, +}; \ No newline at end of file diff --git a/src/tools/registry.ts b/src/tools/registry.ts index c8d5a75..0a74703 100644 --- a/src/tools/registry.ts +++ b/src/tools/registry.ts @@ -1,8 +1,9 @@ import { weatherTool } from './weather.js'; import { calculatorTool } from './calculator.js'; +import { guessTool } from './guess.js'; import { Tool } from '../types/index.js'; -const tools: Tool[] = [weatherTool, calculatorTool]; +const tools: Tool[] = [weatherTool, calculatorTool, guessTool]; export function getTools(): Tool[] { return tools;