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应用程序和聊天机器人
相关推荐
ZhengEnCi14 分钟前
AI Agent(AI智能体) 记忆管理系统设计 — 从向量库边界到生产级 Memory(记忆) 架构
人工智能
凉菜lc34 分钟前
对比文《IM Bot 框架怎么选?Zhin vs Koishi vs NoneBot》
人工智能
阿里云大数据AI技术37 分钟前
DataWorks Data Agent 实战课堂(三):对话式完成数据同步与智能诊断
人工智能·agent
骇客野人1 小时前
AI全栈开发指定技术架构
人工智能
Escape1 小时前
为什么你的 AI 越聊越傻?从 Token 到 Agent,彻底搞懂 AI Agent的秘密㊙️
前端·人工智能·后端
2501_926978332 小时前
认知边缘的双向耦合:从核技术类比到智力货币时代
人工智能·经验分享·笔记·ai写作
SQDN2 小时前
Chatbox 1.22.1 获取不到模型?先验 /models,再对齐精确 model ID
人工智能·测试工具·机器学习·chatgpt·json
颜酱2 小时前
08 | 把维度值同步到 Elasticsearch(生成阶段)
人工智能·python·langchain
小马哥crazymxm2 小时前
Arxiv论文周选 (2026-W28)
论文阅读·人工智能·科技
ASKED_20192 小时前
一文阐述国内网络环境下使用NIM方法
人工智能·系统架构