
Company News
Free Business Plan Upgrades for Open Source Maintainers
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.
huxy-llm-api
Advanced tools
一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。支持流式响应和自定义配置,适用于快速集成大语言模型服务。
Undici 实现高性能 HTTP 请求npm install huxy-llm-api
# 或
pnpm add huxy-llm-api
# 或
yarn add huxy-llm-api
import startApi from 'huxy-llm-api';
// 初始化 Ollama API
const ollamaApi = startApi('ollama', {
apiKey: 'your-api-key',
host: 'http://localhost:11434',
dispatcher: {
headersTimeout: 10 * 60 * 1000,
},
}, {
model: 'qwen3-vl:latest',
options: {
num_ctx: 4096,
},
});
// 初始化 OpenAI API
const openaiApi = startApi('openai', {
apiKey: 'your-api-key',
baseURL: 'https://api.openai.com/v1',
});
const result = await ollamaApi.generate('你好', {
model: 'qwen3-vl',
stream: false,
options: {
temperature: 0.15,
top_p: 0.9,
},
}, (message) => {
console.log('实时响应:', message);
});
console.log('最终结果:', result);
const response = await openaiApi.chat('你是谁', {
model: 'gpt-3.5-turbo',
temperature: 0.7,
stream: true,
}, (message, rawResponse) => {
console.log('实时消息:', message);
console.log('原始响应:', rawResponse);
});
console.log('对话结果:', response);
saveImage(base64String, outputDir = './images', name): 保存 base64 图片到本地。imageToBase64(imagePath, includeMimeType = false): 将本地图片转为 base64 字符串。使用:
// imageToBase64
const image = await ollamaApi.imageToBase64('./example.png');
const result = await ollamaApi.generate('你好', {
model: 'qwen3-vl',
stream: false,
image,
options: {
temperature: 0.15,
top_p: 0.9,
},
}, (message) => {
console.log('实时响应:', message);
});
// saveImage
const result = await ollamaApi.generate('你好', {
model: 'qwen3-vl',
stream: false,
options: {
temperature: 0.15,
top_p: 0.9,
},
}, (message) => {
console.log('实时响应:', message);
});
saveImage(result.image);
startApi(apiType, userConfig)初始化 LLM API 客户端。
参数:
apiType: 'ollama' 或 'openai' - 指定要使用的 API 类型userConfig: 对象 - 自定义 API 接口配置,如 apiKey、baseURL、fetch 等userOption: 对象 - 通用模型参数配置返回: API 客户端实例,包含以下方法:
generate(prompt, configs, callback): 文本生成chat(prompt, configs, callback): 聊天对话responses(prompt, configs, callback): 结构化响应chat(prompt, configs, callback): 聊天对话responses(prompt, configs, callback): 结构化响应prompt: 字符串或消息数组 - 输入提示configs: 对象 - 模型参数配置
model: 模型名称stream: 是否流式响应(默认: false)system: 系统提示(聊天模式)options: 其他模型参数(OpenAI 可使用 extra_body)
temperature: 生成温度(0-1)top_p: 核采样概率callback: 函数 - 流式响应回调项目提供了默认配置,可以通过环境变量或参数覆盖:
Ollama 默认配置:
{
apiKey: process.env.OLLM_API_KEY || '1234',
host: process.env.OLLM_API_HOST || 'http://localhost:11434',
params: {
// keep_alive: -1,
},
options: {
// temperature: 0.6,
}
}
OpenAI 默认配置:
{
apiKey: process.env.LLM_API_KEY || '1234',
baseURL: process.env.LLM_API_BASEURL || 'http://localhost:11434/v1',
params: {
// temperature: 1,
},
options: {
// thinking: true,
}
}
支持通过环境变量配置 API 密钥和地址:
# Ollama
export OLLM_API_KEY="your-key"
export OLLM_API_HOST="http://localhost:11434"
# OpenAI
export LLM_API_KEY="your-key"
export LLM_API_BASEURL="https://api.openai.com/v1"
查看 example.js 了解完整用法示例。
欢迎提交 Issue 和 Pull Request。
MIT License © ahyiru
FAQs
一个简洁、易用的用于简化 Ollama 和 OpenAI API 调用的 Node.js 库。
The npm package huxy-llm-api receives a total of 12 weekly downloads. As such, huxy-llm-api popularity was classified as not popular.
We found that huxy-llm-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.

Security News
The supply chain control that delays freshly published gems now covers lockfile generation and gem vendoring in Ruby projects.

Security News
During a UK cyber test, a Mythos 5 agent used sockpuppets, social engineering, and prompt injection to try to get a maintainer to merge malware.