1-认识langchain.js

初始化项目

  • npm init -y

安装 LangChain 核心包

  • npm install langchain

安装 LangChain 的 OpenAI 适配器

  • npm install @langchain/openai

(可选) 安装环境变量管理包 dotenv,用于管理 API Key

  • npm install dotenv

  • .env环境变量

txt 复制代码
DEEPSEEK_API_KEY="sk-f89f4c441f4540e68fxxxxxxxxxxxx"
BASE_URL="https://api.deepseek.com"
AI_MODEL="deepseek-chat"
  • llm.js单次调用
js 复制代码
import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";

// 加载环境变量
dotenv.config();

// 配置 DeepSeek 模型
const model = new ChatOpenAI({
  // 指定 DeepSeek 的模型名称,例如 deepseek-chat
  model: process.env.AI_MODEL,
  // 配置 DeepSeek 的 API Key
  apiKey: process.env.DEEPSEEK_API_KEY,
  // 配置 DeepSeek 的 Base URL
  configuration: {
    baseURL: process.env.BASE_URL,
  },
});

async function main() {
  try {
    // 调用模型
    const response = await model.invoke("你好,请介绍一下你自己。");// 单次调用
    console.log(response.content);
  } catch (error) {
    console.error("发生错误:", error);
  }
}

main();
  • node llm.js执行如下:

llm.js批量调用

js 复制代码
import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";

// 加载环境变量
dotenv.config();

// 配置 DeepSeek 模型
const model = new ChatOpenAI({
  // 指定 DeepSeek 的模型名称,例如 deepseek-chat
  model: process.env.AI_MODEL,
  // 配置 DeepSeek 的 API Key
  apiKey: process.env.DEEPSEEK_API_KEY,
  // 配置 DeepSeek 的 Base URL
  configuration: {
    baseURL: process.env.BASE_URL,
  },
});

async function main() {
  try {
    // 调用模型
    // const response = await model.invoke("你好,请介绍一下你自己。");// 单次调用
    const response = await model.batch(["1 + 1 = 多少", "2 + 2 = 多少"]); // 批量调用
    console.log(response);
  } catch (error) {
    console.error("发生错误:", error);
  }
}

main();

llm.js流式调用

js 复制代码
import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";

// 加载环境变量
dotenv.config();

// 配置 DeepSeek 模型
const model = new ChatOpenAI({
  // 指定 DeepSeek 的模型名称,例如 deepseek-chat
  model: process.env.AI_MODEL,
  // 配置 DeepSeek 的 API Key
  apiKey: process.env.DEEPSEEK_API_KEY,
  // 配置 DeepSeek 的 Base URL
  configuration: {
    baseURL: process.env.BASE_URL,
  },
});

async function main() {
  try {
    // 调用模型
    const response = await model.stream("1 + 1 = 多少"); // 流式调用
    for await (const chunk of response) {
      console.log(chunk);
    }
  } catch (error) {
    console.error("发生错误:", error);
  }
}

main();
  • 效果图如下:

常用的属性学习

js 复制代码
import { ChatOpenAI } from "@langchain/openai";
import * as dotenv from "dotenv";

// 加载环境变量
dotenv.config();

// 配置 DeepSeek 模型
const model = new ChatOpenAI({
  // 指定 DeepSeek 的模型名称,例如 deepseek-chat
  model: process.env.AI_MODEL,

  // 配置 DeepSeek 的 API Key
  apiKey: process.env.DEEPSEEK_API_KEY,
  // 配置 DeepSeek 的 Base URL
  configuration: {
    baseURL: process.env.BASE_URL,
  },
  temperature: 0.7, // 模型温度的范围是0到1,0表示严格和事实,1表示完全创意(就是瞎吉儿编)
  maxTokens: 1024, // 最大输出token数
  verbose: true, // 允许我们调试模型
});

async function main() {
  try {
    // 调用模型
    const response = await model.invoke("1 + 1 = 多少"); // 单次调用
    console.log(response);
  } catch (error) {
    console.error("发生错误:", error);
  }
}

main();
  • 这就是我们第一个LangChain应用程序和聊天机器人
相关推荐
柒星栈10 分钟前
国产大模型“三国杀“:豆包收费、DeepSeek 开源、Kimi 2.5 万亿参数
人工智能·开源
星辰AI18 分钟前
ESLint 自定义规则实战:用 AST 分析封堵团队特有的代码坏味道
人工智能·ai·语言模型
dozenyaoyida21 分钟前
LSTM 自编码器原理详解:从三门结构到 STM32 端侧异常检测落地
人工智能·stm32·lstm
石榴32 分钟前
我用 Codex Vibe Coding 了一个 VS Code 数据库扩展:把付费墙后的工作流写成自己的工具
人工智能
ZZZMMM.zip36 分钟前
信息压缩站-长文智能摘要的HarmonyOS开发实践
人工智能·华为·harmonyos·鸿蒙·鸿蒙系统
Black蜡笔小新37 分钟前
自动化AI算法训练服务器DLTM零代码私有化赋能智慧农业用AI智能视觉守护万亩良田
人工智能·算法·自动化
气泡音人声分离42 分钟前
音频处理核心原理:变速与变调的本质区别,以及“变速不变调”是如何实现的
人工智能·音频剪辑·升降调·音频变速
拓向AI1 小时前
南京高性价比DeepSeek优化推广指南
大数据·人工智能
ZX0X学习中1 小时前
一句话让 AI 生成 Excel 多文件合并工具:码道 CLI 实战
人工智能·excel·ai编程·华为云码道
AI小码1 小时前
WAIC 2026前瞻:AI产业进入拼落地的下半场
人工智能·算法·ai·程序员·大模型·编程·智能体