欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 旅游 > 在nodeJS 中实现langchain 的Agent (实验笔记)

在nodeJS 中实现langchain 的Agent (实验笔记)

2024/10/24 17:33:15 来源:https://blog.csdn.net/yaojiawan/article/details/139457642  浏览:    关键词:在nodeJS 中实现langchain 的Agent (实验笔记)

在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 好像有点问题。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com