Nodejs 调用langchainjs的实验

langchain 提供了python 和JavaScript 两种库,其中JavaScript 库称为 langchainjs,官网地址:

https://github.com/langchain-ai/langchainjs

  langchain 支持js/ts 语言,可以在nodeJs 中调用langchain 。这有利于在网站后台软件中使用,本博文记录做的小实验。

安装与升级

升级node

   直接在官网下载,重新安装就可以了。

安装大模型模块

 npm install openai

安装langchain

 npm install langchain

简单的程序

程序-1 大模型调用

import OpenAI from 'openai';
const openai = new OpenAI({
  apiKey: "sk-xxxxxxxxxx",
  baseURL:"https://api.chatanywhere.tech/v1",
  model: "gpt-3.5-turbo",
  temperature: 0
});
async function main() {
  const stream = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [{ role: 'user', content: '北京在哪里?' }],
    stream: true,
  });
  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || '');
  }
}

main();

访问kimi 也是可以的,代码如下:

import OpenAI from 'openai';
const openai = new OpenAI({
    apiKey: "sk-xxxxxxxxx",
    baseURL:"https://api.moonshot.cn/v1",
    model: "moonshot-v1-8k",
  temperature: 0
});
async function main() {
  const stream = await openai.chat.completions.create({
    model: 'moonshot-v1-8k',
    messages: [{ role: 'user', content: '北京在哪里?' }],
    stream: true,
  });
  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || '');
  }
}

main();

程序-2 langchain 调用

import { LLMChain } from "langchain/chains";
import { OpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
process.env['OPENAI_API_KEY']="sk-xxxxxxxxxx";
process.env['OPENAI_BASE_URL']="https://api.chatanywhere.tech/v1"

  const chat = new OpenAI({
    apiKey: "sk-FfhkMFdQQwDqAR5Mta2UxsU9amU6AoIwDG1NbqqAWGzMpTyi",
    baseURL:"https://api.chatanywhere.tech/v1",
    model: "gpt-3.5-turbo",
    temperature: 0
  });
  const chatPrompt = ChatPromptTemplate.fromMessages([
    [
      "system",
      "You are a helpful assistant that translates {input_language} to {output_language}.",
    ],
    ["human", "{text}"],
  ]);

  const chain = new LLMChain({
    prompt: chatPrompt,
    llm: chat,
  });

  const response = await chain.invoke({
    input_language: "English",
    output_language: "French",
    text: "I love programming.",
  });

  console.log(response.text);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值