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);