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

推荐订阅源

S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
Y
Y Combinator Blog
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
V
V2EX
腾讯CDC
F
Fortinet All Blogs
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss

博客园 - 漫思

python使用.env构建开发和生产环境 python项目的构建 nodejs构建CICD时的思考  成为 AI 智能体工程师的 10 个步骤 es6的 yield python 中的 yield 笔记本的A壳 thinkpad 更换 reduce() in Python python的字段赋值和取值的操作 python的语法类似于lodash分组展开,合并分组操作 SelectMany C# lodash 数组的常用做法 lodash里面的常用方法 技术的边界 Reduce 和 Transduce 的含义 尤雨溪创办的 VoidZero 官宣加入 Cloudflare,前端 Vite 等保持开源 Ramda 函数库参考教程 ramda es 数组的方法 flatmap map object.entity的教程 Map与FlatMap:在数据处理中的区别与联系 flat、flatmap与map的用法区别1 flat、flatmap与map的用法区别 AI不是从天而降,它经历了七十年三起三落:读懂AI的第三课 Agent 17 种架构模式 分析 & 思考 只有踩过坑才懂:前端生成唯一 ID,别用 Date.now ()了!试试它crypto.randomUUID() FastAPI python并发 代码是 AI 写的,生产事故谁背锅? AI Agent 走出 Demo 幻觉的唯一解药:Harness Engineering
nodejs的顶尖开源项目
漫思 · 2026-05-18 · via 博客园 - 漫思

1、load env文件

import { config as loadDotenv } from "dotenv";

loadDotenv();

或者

import "dotenv/config";

2、nodejs的数据的输入输出 

import { createInterface } from "node:readline/promises";

import { stdin as input, stdout as output } from "node:process";

const rl = createInterface({ input, output });

const userText = (await rl.question("你: ")).trim();

if (["exit", "quit", "q"].includes(userText.toLowerCase())) {

break;

}

console.log(`AI: ${text || "(未返回文本内容)"}\n`);

3、获取文件的拓展名

import { extname } from "node:path";

const suffix = extname(filePath).toLowerCase();

if (suffix !== ".md" && suffix !== ".txt") {

continue;

}

4、获取文件的目录

import { resolve } from "node:path";

const KNOWLEDGE_BASE_DIR = resolve(process.cwd(), "knowledge_base");

5、根据文件目录 遍历文件

walkFiles(KNOWLEDGE_BASE_DIR);

function walkFiles(dir: string): string[] {

const files: string[] = [];

for (const entry of readdirSync(dir, { withFileTypes: true })) {

const fullPath = resolve(dir, entry.name);

if (entry.isDirectory()) {

files.push(...walkFiles(fullPath));

} else {

files.push(fullPath);

}

}

return files;

}

6、文件读取

import { existsSync, readFileSync } from "node:fs";

function loadTrainingExamples(maxExamples = 20): TrainingExample[] {

if (!existsSync(TRAINING_DATA_PATH)) {

return [];

}

return readFileSync(TRAINING_DATA_PATH, "utf-8")

.split(/\r?\n/)

.map((line) => line.trim())

.filter(Boolean)

.flatMap(parseTrainingExample)

.slice(0, maxExamples);

}

6、读取env文件

const apiKey = process.env.OPENAI_API_KEY?.trim();

const baseURL = process.env.OPENAI_BASE_URL?.trim();

const model = process.env.MODEL_NAME?.trim() || "qwen-plus";

7、抛出异常

if (!apiKey || !baseURL) {

throw new Error("缺少 OPENAI_API_KEY 或 OPENAI_BASE_URL 配置");

}

漫思

posted on 2026-05-18 15:35  漫思  阅读(2)  评论()    收藏  举报