跳转到内容

LLM 客户端

createLLMClient 提供 OpenAI 兼容的 HTTP 客户端,支持 Chat Completions 接口,可用于对接 OpenAI、Azure OpenAI、DeepSeek 或自建服务。

import { createLLMClient } from "@ventostack/ai";
const llm = createLLMClient({
apiKey: process.env.OPENAI_API_KEY!,
baseURL: "https://api.openai.com/v1", // 可替换为其他兼容端点
});
// 非流式调用
const response = await llm.chat({
model: "gpt-4o",
messages: [
{ role: "system", content: "你是一个有帮助的助手。" },
{ role: "user", content: "你好!" },
],
});
console.log(response.content);
const stream = await llm.chatStream({
model: "gpt-4o",
messages: [{ role: "user", content: "写一首诗" }],
});
for await (const chunk of stream) {
process.stdout.write(chunk);
}
参数类型必填说明
apiKeystringAPI 密钥
baseURLstringAPI 端点,默认 https://api.openai.com/v1
timeoutnumber请求超时(毫秒)
maxRetriesnumber最大重试次数

createLLMClient 通常与 createRAGAgent 配合使用,为 RAG 检索结果生成自然语言回答:

import { createLLMClient, createRAGAgent, createKnowledgeBase } from "@ventostack/ai";
const llm = createLLMClient({ apiKey: process.env.OPENAI_API_KEY! });
const kb = createKnowledgeBase({ executor, embedding: llm });
const agent = createRAGAgent({ llm, knowledgeBase: kb });
const answer = await agent.ask("VentoStack 的缓存策略有哪些?");