代码之家  ›  专栏  ›  技术社区  ›  Adrian Botteon

如何将ChatGPT插件与我的nodejs服务器集成?

  •  0
  • Adrian Botteon  · 技术社区  · 3 年前

    我开发了一个Node.js服务器,通过使用Polygon.io ChatGPT插件来获取财务数据。 如果我集成了klarna插件,它可以工作,但不能与多边形插件一起工作。 以下是我使用Polygon插件的代码。

    var http = require("http");
    const axios = require("axios");
    const {OpenAI} = require("langchain/llms/openai");
    const {initializeAgentExecutorWithOptions} = require("langchain/agents");
    const {SerpAPI, AIPluginTool, RequestsGetTool, RequestsPostTool} = require("langchain/tools")
    const {Calculator} = require("langchain/tools/calculator");
    const {ChatOpenAI} = require("langchain/chat_models/openai");
    
    // At the top of the file
    
    const server = http
      .createServer((req, res) => {
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        res.setHeader("Access-Control-Allow-Headers", "Content-Type");
        if (req.method === "POST") {
          // Handle post info...
          let body = "";
          req.on("data", (chunk) => {
            body += chunk.toString(); // convert Buffer to string
          });
          req.on("end", () => {
            var question = body.split('"')[3];
            console.log(question);
    
          const tools = [
            new RequestsGetTool(),
            new RequestsPostTool(),
    
            AIPluginTool.fromPluginUrl("https://polygon.io/.well-known/ai-plugin.json", "70DnLnda2q81MoTZpXTAalXJ0dXS4Ovp")
              .then((tool) => {
                // Make sure the tool has a description before using it
                if (!tool.description) {
                  throw new Error("AIPluginTool is missing a description.");
                }
                return tool;
              })
              .catch((err) => {
                console.error("Error initializing AIPluginTool:", err);
                throw err; // Rethrow the error to handle it appropriately
              }),
          ];
    
    
          Promise.all(tools)
          .then((resolvedTools) => {
            const agent = initializeAgentExecutorWithOptions(
              resolvedTools,
              new ChatOpenAI({ temperature: 0, openAIApiKey: "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" }),
              { agentType: "chat-zero-shot-react-description", verbose: true }
            );
    
            return agent.then((exe) => {
              return exe.call({ input: question });
            });
          })
          .then((response) => {
            res.end(response.output);
          })
          .catch((err) => {
            console.error("Error running the agent:", err);
            res.statusCode = 500;
            res.end("Internal Server Error");
          });
    

    如果我问“AAPL的股票是多少?”,那么这个代码中的问题变量就是我问的问题。 我得到的结果是

    }
    [chain/end] [1:chain:agent_executor > 2:chain:llm_chain] [3.55s] Exiting Chain run with 
    output: {
      "text": "Thought: I need to find the last dividend of AAPL.\nAction: I will use the Polygon 
    API to get the dividend data for AAPL.\n"
    }
    [chain/end] [1:chain:agent_executor] [3.55s] Exiting Chain run with output: {
      "output": "Thought: I need to find the last dividend of AAPL.\nAction: I will use the 
    Polygon API to get the dividend data for AAPL."
    }
    

    为什么会发生这种情况? 如果我使用另一个插件像klarna,它工作得很好。 这是使用klarna ChatGPT插件的工作代码

    var http = require("http");
    const axios = require("axios");
    const {OpenAI} = require("langchain/llms/openai");
    const {initializeAgentExecutorWithOptions} = require("langchain/agents");
    const {SerpAPI, AIPluginTool, RequestsGetTool, RequestsPostTool} = require("langchain/tools")
    const {Calculator} = require("langchain/tools/calculator");
    const {ChatOpenAI} = require("langchain/chat_models/openai");
    
    // At the top of the file
    
    const server = http
      .createServer((req, res) => {
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        res.setHeader("Access-Control-Allow-Headers", "Content-Type");
        if (req.method === "POST") {
          // Handle post info...
          let body = "";
          req.on("data", (chunk) => {
            body += chunk.toString(); // convert Buffer to string
          });
          req.on("end", () => {
            var question = body.split('"')[3];
            console.log(question);
    
          const tools = [
            new RequestsGetTool(),
            new RequestsPostTool(),
            AIPluginTool.fromPluginUrl("https://www.klarna.com/.well-known/ai-plugin.json")
          ];
    
    
          Promise.all(tools)
          .then((resolvedTools) => {
            const agent = initializeAgentExecutorWithOptions(
              resolvedTools,
              new ChatOpenAI({ temperature: 0, openAIApiKey: "sk-jlbN4HSHNMTOuk0Nw2A****************nkV1n1hSq" }),
              { agentType: "chat-zero-shot-react-description", verbose: true }
            );
    
            return agent.then((exe) => {
              return exe.call({ input: question });
            });
          })
          .then((response) => {
            res.end(response.output);
          })
          .catch((err) => {
            console.error("Error running the agent:", err);
            res.statusCode = 500;
            res.end("Internal Server Error");
          });
    

    谢谢

    0 回复  |  直到 3 年前
    推荐文章