惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Martin Fowler
Martin Fowler
爱范儿
爱范儿
IT之家
IT之家
Last Week in AI
Last Week in AI
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
The Cloudflare Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog

卡卡罗特

流行的Agent代码编辑器 eino实现Agent eino整合RAG eino整合Tools go整合向量数据库ChromaDB langchain实现Agent ollama调用各大模型 spring-ai 自定义一个MCP openCV初体验 01-Bing每日一图接口 02-腾讯API高清QQ头像https调用接口 01-获取指定网站的所有的链接 context.Context是什么? RWMutex读写锁 SyncMap的使用 defer的使用 go字符串工具类 go生成UUID go正则表达式 最后代码 | 卡卡罗特 swagger文档 | 卡卡罗特 react脚手架开发 | 卡卡罗特 python的对象增强 | 卡卡罗特 langchain实现Agent | 卡卡罗特 03.python列表集合元组字典 | 卡卡罗特 python依赖打包 | 卡卡罗特 python的异步 | 卡卡罗特 python的生成器 | 卡卡罗特 eino整合Tools | 卡卡罗特
langchainjs
灯塔码铺-敲代码的卡卡罗特 · 2026-08-23 · via 卡卡罗特

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

js调用ollama ​

所需要的依赖

json

{
    "@langchain/community": "^0.3.31",
    "@langchain/core": "^0.3.40",
    "express": "^4.21.2",
    "axios": "^1.7.9"
}

测试代码 test.js

js

// ----------------------------
// 文件:app.js
// 运行:node app.js
// 访问:POST http://localhost:3000/ask 发送 JSON { "question": "你的问题" }
// ----------------------------
import express from 'express';

import { Ollama } from '@langchain/community/llms/ollama';

// 1. 初始化 Express 和模型
const app = express();
app.use(express.json()); // 允许解析 JSON 请求体

// 连接本地 Ollama 模型(确保服务已启动)
const localModel = new Ollama({
    baseUrl: 'http://127.0.0.1:11434', // Ollama 默认端口
    model: 'deepseek-r1:latest', // 替换为你的模型名称(如 'qwen:7b')
});

// 2. 创建问答接口
app.get('/ask', async (req, res) => {
    try {
        const { question } = req.query;
        // 直接调用模型生成答案
        const answer = await localModel.invoke(question);

        res.json({
            success: true,
            question,
            answer
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            error: error.message
        });
    }
});
// app.post('/ask', async (req, res) => {
//     try {
//         const { question } = req.body;
//         // 直接调用模型生成答案
//         const answer = await localModel.invoke(question);
//
//         res.json({
//             success: true,
//             question,
//             answer
//         });
//     } catch (error) {
//         res.status(500).json({
//             success: false,
//             error: error.message
//         });
//     }
// });

// 3. 启动服务
app.listen(3000, () => {
    console.log('服务已启动:http://localhost:3000');
});