在nodeJS 中实现langchain 的Agent 实验过程记录如下:
1 构建一个Agent ,使用两个工具 Calculator和TavilySearchResults
2 Tavily Search的API key 的获取
之前一直找不到一个合适的搜索引擎,Google Search 被墙,bing Search 获取API Key 过程麻烦,百度好像还没有这个服务。这一次终于获取了Tavily 免费的research API Key,过程如下
访问官网,点击 Get Started,选择Free,再次点击Get Started
出现了你要的Key
点击Copy 就OK了
Agent 程序
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { OpenAI } from "@langchain/openai";
import { Calculator } from "@langchain/community/tools/calculator";
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
process.env['OPENAI_API_KEY']="sk-xxxxxxxxxx";
process.env['OPENAI_BASE_URL']="https://api.chatanywhere.tech/v1"
process.env['TAVILY_API_KEY']="tvly-xxxxxxxxx"
const openai = new OpenAI({apiKey: "sk-xxxxxxxxx",baseURL:"https://api.chatanywhere.tech/v1",model: "gpt-3.5-turbo",temperature: 0
});
const tools = [new TavilySearchResults(),new Calculator(),
];
const executor = await initializeAgentExecutorWithOptions(tools,openai,{ agentType: "chat-zero-shot-react-description", verbose: false });
//Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?
const input = `无锡当前的天气如何?`;
const result = await executor.invoke({input: input,});
console.log(result.output);
运行结果
PS E:\yao2024\nodeJS\langchainDemo> node kimiAgent.mjs
The current weather in Wuxi is partly cloudy with a temperature of 21.7°C (71.1°F).
注:同样的程序,kimi 好像有点问题。