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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值