flux-agent
Version:
FluxAgent - 一个可灵活插拔的AI Agent系统框架,基于TypeScript开发,支持流式执行、事件系统、插件系统、知识库管理等功能 (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (Protected Release) (
166 lines (165 loc) • 6.57 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAILLM = void 0;
const openai_1 = __importDefault(require("openai"));
class OpenAILLM {
constructor(config, llmName, onUsage) {
this.llmName = llmName;
this.client = new openai_1.default({
apiKey: config.apiKey,
baseURL: config.baseURL
});
this.onUsage = onUsage;
this.modelName = config.modelName;
this.temperature = config.temperature || 0.7;
this.maxTokens = config.maxTokens;
this.options = {
frequency_penalty: config.frequency_penalty,
presence_penalty: config.presence_penalty,
top_p: config.top_p,
response_format: config.response_format
};
}
async chat(messages, tools, options) {
const response = await this.client.chat.completions.create({
model: this.modelName,
messages: messages,
tools: tools,
temperature: this.temperature,
max_tokens: this.maxTokens,
response_format: options?.response_format,
...this.options
});
const choice = response.choices[0];
const message = choice.message;
const usage = response.usage;
// 触发 usage 事件
if (this.onUsage && usage) {
this.onUsage(this.llmName, usage);
}
// 处理工具调用
if (message.tool_calls && message.tool_calls.length > 0) {
return {
content: message.content,
toolCalls: message.tool_calls.map(tool => ({
name: tool.function.name,
arguments: JSON.parse(tool.function.arguments)
})),
usage
};
}
return {
content: message.content,
usage
};
}
// 新增:流式聊天实现
async streamChat(messages, tools, callbacks, toolChoice) {
try {
const stream = await this.client.chat.completions.create({
model: this.modelName,
messages: messages,
tools: tools,
temperature: this.temperature,
max_tokens: this.maxTokens,
stream: true,
stream_options: {
include_usage: true
},
tool_choice: toolChoice,
...this.options
});
let fullContent = '';
const toolCalls = [];
let currentToolCall = null;
let toolCallBuffer = '';
let finished;
let usage;
let streamId; // 存储流式调用的 ID
for await (const chunk of stream) {
// 提取 chunk 的 ID(如果存在)
if (chunk.id && !streamId) {
streamId = chunk.id;
}
const choice = chunk.choices[0];
if (chunk.usage) {
usage = chunk.usage;
}
if (choice && !finished) {
const delta = choice.delta;
// 处理普通内容token
if (delta.content && toolChoice !== 'required') {
const token = delta.content;
fullContent += token;
// 回调单个token,传递 streamId
if (callbacks?.onToken) {
callbacks.onToken(token, streamId);
}
// 回调部分响应,传递 streamId
if (callbacks?.onPartialResponse) {
callbacks.onPartialResponse(fullContent, streamId);
}
}
// 处理工具调用
if (delta.tool_calls && toolChoice !== 'none') {
for (const toolCall of delta.tool_calls) {
if (toolCall.function) {
// 新工具调用开始
if (toolCall.function.name && !currentToolCall) {
currentToolCall = {
id: toolCall.id,
name: toolCall.function.name,
arguments: ''
};
}
// 积累工具调用参数
if (toolCall.function.arguments && currentToolCall) {
toolCallBuffer += toolCall.function.arguments;
}
}
}
}
if (choice.finish_reason === 'stop' || choice.finish_reason === 'tool_calls') {
finished = true;
}
}
}
// 完成工具调用处理
if (currentToolCall && toolCallBuffer) {
try {
currentToolCall.arguments = JSON.parse(toolCallBuffer);
toolCalls.push(currentToolCall);
}
catch (error) {
toolCalls.push(currentToolCall);
console.error('解析工具调用参数失败:', error, toolCallBuffer);
console.log('修复后的工具调用参数:', currentToolCall.arguments);
}
}
if (this.onUsage && usage) {
this.onUsage(this.llmName, usage);
}
const response = {
content: fullContent || null,
streamId,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
usage
};
// 回调完成,传递 streamId
if (callbacks?.onComplete && toolChoice !== 'required') {
callbacks.onComplete(response, streamId);
}
return response;
}
catch (error) {
if (callbacks?.onError) {
callbacks.onError(error, undefined); // 错误时可能没有 streamId
}
throw error;
}
}
}
exports.OpenAILLM = OpenAILLM;