跳到主要内容

AI工具集成与开发

在前端全栈AI应用开发中,集成各种AI工具和服务是构建强大应用的关键。本文将深入探讨如何在前端项目中集成主流AI服务,包括OpenAI、Anthropic、Google AI等,以及如何构建统一的AI工具管理系统。

目录

  1. AI服务集成架构
  2. 统一AI客户端设计
  3. 主流AI服务集成
  4. 工具链管理系统
  5. API调用优化
  6. 错误处理与重试
  7. 成本控制与监控
  8. 完整集成示例
  9. 最佳实践
  10. 学习检验
  11. 项目建议
  12. 延伸阅读

AI服务集成架构

整体架构设计

// AI服务集成架构
class AIServiceArchitecture {
constructor() {
this.services = new Map();
this.router = new AIServiceRouter();
this.monitor = new AIServiceMonitor();
this.cache = new AIResponseCache();
this.rateLimiter = new RateLimiter();
}

// 注册AI服务
registerService(name, service) {
this.services.set(name, service);
console.log(`AI服务 ${name} 已注册`);
}

// 获取服务
getService(name) {
return this.services.get(name);
}

// 路由请求到最佳服务
async routeRequest(request) {
const bestService = await this.router.selectBestService(request, this.services);
return await this.executeRequest(bestService, request);
}

// 执行请求
async executeRequest(service, request) {
try {
// 检查速率限制
await this.rateLimiter.checkLimit(service.name);

// 检查缓存
const cacheKey = this.cache.generateKey(request);
const cachedResponse = await this.cache.get(cacheKey);

if (cachedResponse) {
this.monitor.recordCacheHit(service.name);
return cachedResponse;
}

// 执行请求
const response = await service.execute(request);

// 缓存响应
await this.cache.set(cacheKey, response);

// 记录监控数据
this.monitor.recordRequest(service.name, request, response);

return response;

} catch (error) {
this.monitor.recordError(service.name, error);
throw error;
}
}
}

// AI服务路由器
class AIServiceRouter {
constructor() {
this.rules = [];
this.loadBalancer = new LoadBalancer();
}

// 添加路由规则
addRule(condition, serviceNames, priority = 0) {
this.rules.push({
condition,
serviceNames,
priority
});

// 按优先级排序
this.rules.sort((a, b) => b.priority - a.priority);
}

// 选择最佳服务
async selectBestService(request, services) {
// 应用路由规则
for (const rule of this.rules) {
if (await rule.condition(request)) {
const availableServices = rule.serviceNames
.map(name => services.get(name))
.filter(service => service && service.isAvailable());

if (availableServices.length > 0) {
return this.loadBalancer.select(availableServices);
}
}
}

// 默认选择第一个可用服务
for (const service of services.values()) {
if (service.isAvailable()) {
return service;
}
}

throw new Error('没有可用的AI服务');
}
}

// 负载均衡器
class LoadBalancer {
constructor(strategy = 'round-robin') {
this.strategy = strategy;
this.counters = new Map();
}

// 选择服务
select(services) {
switch (this.strategy) {
case 'round-robin':
return this.roundRobin(services);
case 'random':
return this.random(services);
case 'least-connections':
return this.leastConnections(services);
default:
return services[0];
}
}

// 轮询策略
roundRobin(services) {
const key = services.map(s => s.name).join(',');
const counter = this.counters.get(key) || 0;
const selected = services[counter % services.length];
this.counters.set(key, counter + 1);
return selected;
}

// 随机策略
random(services) {
return services[Math.floor(Math.random() * services.length)];
}

// 最少连接策略
leastConnections(services) {
return services.reduce((min, current) =>
current.getActiveConnections() < min.getActiveConnections() ? current : min
);
}
}

统一AI客户端设计

基础AI客户端

// 基础AI服务接口
class BaseAIService {
constructor(config) {
this.name = config.name;
this.apiKey = config.apiKey;
this.baseURL = config.baseURL;
this.timeout = config.timeout || 30000;
this.retryConfig = config.retryConfig || { maxRetries: 3, delay: 1000 };
this.activeConnections = 0;
this.isEnabled = true;
}

// 检查服务是否可用
isAvailable() {
return this.isEnabled && this.apiKey;
}

// 获取活跃连接数
getActiveConnections() {
return this.activeConnections;
}

// 执行请求(抽象方法)
async execute(request) {
throw new Error('execute方法必须被子类实现');
}

// 通用HTTP请求方法
async makeRequest(endpoint, options = {}) {
this.activeConnections++;

try {
const response = await fetch(`${this.baseURL}${endpoint}`, {
timeout: this.timeout,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`,
...options.headers
},
...options
});

if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}

return await response.json();

} finally {
this.activeConnections--;
}
}

// 带重试的请求
async makeRequestWithRetry(endpoint, options = {}) {
let lastError;

for (let i = 0; i <= this.retryConfig.maxRetries; i++) {
try {
return await this.makeRequest(endpoint, options);
} catch (error) {
lastError = error;

if (i < this.retryConfig.maxRetries) {
const delay = this.retryConfig.delay * Math.pow(2, i); // 指数退避
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}

throw lastError;
}

// 流式请求
async makeStreamRequest(endpoint, options = {}) {
this.activeConnections++;

try {
const response = await fetch(`${this.baseURL}${endpoint}`, {
timeout: this.timeout,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`,
...options.headers
},
...options
});

if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}

return response.body.getReader();

} catch (error) {
this.activeConnections--;
throw error;
}
}
}

// 统一AI客户端
class UnifiedAIClient {
constructor() {
this.services = new Map();
this.defaultService = null;
this.requestInterceptors = [];
this.responseInterceptors = [];
}

// 注册服务
registerService(service) {
this.services.set(service.name, service);

if (!this.defaultService) {
this.defaultService = service.name;
}
}

// 设置默认服务
setDefaultService(serviceName) {
if (this.services.has(serviceName)) {
this.defaultService = serviceName;
} else {
throw new Error(`服务 ${serviceName} 不存在`);
}
}

// 添加请求拦截器
addRequestInterceptor(interceptor) {
this.requestInterceptors.push(interceptor);
}

// 添加响应拦截器
addResponseInterceptor(interceptor) {
this.responseInterceptors.push(interceptor);
}

// 文本生成
async generateText(prompt, options = {}) {
const serviceName = options.service || this.defaultService;
const service = this.services.get(serviceName);

if (!service) {
throw new Error(`服务 ${serviceName} 不存在`);
}

const request = {
type: 'text-generation',
prompt,
...options
};

return await this.executeRequest(service, request);
}

// 聊天对话
async chat(messages, options = {}) {
const serviceName = options.service || this.defaultService;
const service = this.services.get(serviceName);

if (!service) {
throw new Error(`服务 ${serviceName} 不存在`);
}

const request = {
type: 'chat',
messages,
...options
};

return await this.executeRequest(service, request);
}

// 流式聊天
async streamChat(messages, options = {}) {
const serviceName = options.service || this.defaultService;
const service = this.services.get(serviceName);

if (!service) {
throw new Error(`服务 ${serviceName} 不存在`);
}

const request = {
type: 'stream-chat',
messages,
...options
};

return await this.executeStreamRequest(service, request);
}

// 嵌入向量生成
async generateEmbedding(text, options = {}) {
const serviceName = options.service || this.defaultService;
const service = this.services.get(serviceName);

if (!service) {
throw new Error(`服务 ${serviceName} 不存在`);
}

const request = {
type: 'embedding',
text,
...options
};

return await this.executeRequest(service, request);
}

// 执行请求
async executeRequest(service, request) {
// 应用请求拦截器
for (const interceptor of this.requestInterceptors) {
request = await interceptor(request);
}

let response = await service.execute(request);

// 应用响应拦截器
for (const interceptor of this.responseInterceptors) {
response = await interceptor(response, request);
}

return response;
}

// 执行流式请求
async executeStreamRequest(service, request) {
// 应用请求拦截器
for (const interceptor of this.requestInterceptors) {
request = await interceptor(request);
}

return await service.executeStream(request);
}

// 获取所有服务状态
getServicesStatus() {
const status = {};

for (const [name, service] of this.services) {
status[name] = {
available: service.isAvailable(),
activeConnections: service.getActiveConnections(),
enabled: service.isEnabled
};
}

return status;
}
}

主流AI服务集成

OpenAI服务集成

// OpenAI服务实现
class OpenAIService extends BaseAIService {
constructor(config) {
super({
name: 'openai',
baseURL: 'https://api.openai.com/v1',
...config
});

this.models = {
'gpt-4': { maxTokens: 8192, costPer1kTokens: 0.03 },
'gpt-4-turbo': { maxTokens: 128000, costPer1kTokens: 0.01 },
'gpt-3.5-turbo': { maxTokens: 4096, costPer1kTokens: 0.002 },
'text-embedding-ada-002': { maxTokens: 8191, costPer1kTokens: 0.0001 }
};
}

// 执行请求
async execute(request) {
switch (request.type) {
case 'text-generation':
case 'chat':
return await this.chatCompletion(request);
case 'stream-chat':
return await this.streamChatCompletion(request);
case 'embedding':
return await this.createEmbedding(request);
default:
throw new Error(`不支持的请求类型: ${request.type}`);
}
}

// 聊天完成
async chatCompletion(request) {
const payload = {
model: request.model || 'gpt-3.5-turbo',
messages: request.messages || [{ role: 'user', content: request.prompt }],
max_tokens: request.maxTokens,
temperature: request.temperature || 0.7,
top_p: request.topP,
frequency_penalty: request.frequencyPenalty,
presence_penalty: request.presencePenalty,
stop: request.stop
};

// 移除undefined值
Object.keys(payload).forEach(key => {
if (payload[key] === undefined) {
delete payload[key];
}
});

const response = await this.makeRequestWithRetry('/chat/completions', {
method: 'POST',
body: JSON.stringify(payload)
});

return {
content: response.choices[0].message.content,
usage: response.usage,
model: response.model,
finishReason: response.choices[0].finish_reason
};
}

// 流式聊天完成
async streamChatCompletion(request) {
const payload = {
model: request.model || 'gpt-3.5-turbo',
messages: request.messages || [{ role: 'user', content: request.prompt }],
max_tokens: request.maxTokens,
temperature: request.temperature || 0.7,
stream: true
};

const reader = await this.makeStreamRequest('/chat/completions', {
method: 'POST',
body: JSON.stringify(payload)
});

return this.createStreamIterator(reader);
}

// 创建嵌入向量
async createEmbedding(request) {
const payload = {
model: request.model || 'text-embedding-ada-002',
input: request.text
};

const response = await this.makeRequestWithRetry('/embeddings', {
method: 'POST',
body: JSON.stringify(payload)
});

return {
embedding: response.data[0].embedding,
usage: response.usage,
model: response.model
};
}

// 创建流迭代器
async* createStreamIterator(reader) {
const decoder = new TextDecoder();
let buffer = '';

try {
while (true) {
const { done, value } = await reader.read();

if (done) break;

buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';

for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);

if (data === '[DONE]') {
return;
}

try {
const parsed = JSON.parse(data);
const delta = parsed.choices[0]?.delta;

if (delta?.content) {
yield {
content: delta.content,
done: false
};
}
} catch (error) {
console.warn('解析流数据失败:', error);
}
}
}
}
} finally {
this.activeConnections--;
}
}

// 获取模型信息
getModelInfo(modelName) {
return this.models[modelName] || null;
}

// 估算成本
estimateCost(modelName, tokenCount) {
const modelInfo = this.getModelInfo(modelName);
if (!modelInfo) return null;

return (tokenCount / 1000) * modelInfo.costPer1kTokens;
}
}

Anthropic服务集成

// Anthropic服务实现
class AnthropicService extends BaseAIService {
constructor(config) {
super({
name: 'anthropic',
baseURL: 'https://api.anthropic.com/v1',
...config
});

this.models = {
'claude-3-opus-20240229': { maxTokens: 200000, costPer1kTokens: 0.015 },
'claude-3-sonnet-20240229': { maxTokens: 200000, costPer1kTokens: 0.003 },
'claude-3-haiku-20240307': { maxTokens: 200000, costPer1kTokens: 0.00025 }
};
}

// 重写请求方法以适配Anthropic API
async makeRequest(endpoint, options = {}) {
this.activeConnections++;

try {
const response = await fetch(`${this.baseURL}${endpoint}`, {
timeout: this.timeout,
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
'anthropic-version': '2023-06-01',
...options.headers
},
...options
});

if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}

return await response.json();

} finally {
this.activeConnections--;
}
}

// 执行请求
async execute(request) {
switch (request.type) {
case 'text-generation':
case 'chat':
return await this.createMessage(request);
case 'stream-chat':
return await this.streamMessage(request);
default:
throw new Error(`不支持的请求类型: ${request.type}`);
}
}

// 创建消息
async createMessage(request) {
const messages = request.messages || [{ role: 'user', content: request.prompt }];

const payload = {
model: request.model || 'claude-3-sonnet-20240229',
max_tokens: request.maxTokens || 1024,
messages: messages,
temperature: request.temperature,
top_p: request.topP,
stop_sequences: request.stop
};

// 移除undefined值
Object.keys(payload).forEach(key => {
if (payload[key] === undefined) {
delete payload[key];
}
});

const response = await this.makeRequestWithRetry('/messages', {
method: 'POST',
body: JSON.stringify(payload)
});

return {
content: response.content[0].text,
usage: response.usage,
model: response.model,
finishReason: response.stop_reason
};
}

// 流式消息
async streamMessage(request) {
const messages = request.messages || [{ role: 'user', content: request.prompt }];

const payload = {
model: request.model || 'claude-3-sonnet-20240229',
max_tokens: request.maxTokens || 1024,
messages: messages,
stream: true
};

const reader = await this.makeStreamRequest('/messages', {
method: 'POST',
body: JSON.stringify(payload)
});

return this.createStreamIterator(reader);
}

// 创建流迭代器
async* createStreamIterator(reader) {
const decoder = new TextDecoder();
let buffer = '';

try {
while (true) {
const { done, value } = await reader.read();

if (done) break;

buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';

for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);

try {
const parsed = JSON.parse(data);

if (parsed.type === 'content_block_delta') {
yield {
content: parsed.delta.text,
done: false
};
} else if (parsed.type === 'message_stop') {
return;
}
} catch (error) {
console.warn('解析流数据失败:', error);
}
}
}
}
} finally {
this.activeConnections--;
}
}
}

Google AI服务集成

// Google AI服务实现
class GoogleAIService extends BaseAIService {
constructor(config) {
super({
name: 'google-ai',
baseURL: 'https://generativelanguage.googleapis.com/v1beta',
...config
});

this.models = {
'gemini-pro': { maxTokens: 30720, costPer1kTokens: 0.0005 },
'gemini-pro-vision': { maxTokens: 12288, costPer1kTokens: 0.0025 }
};
}

// 重写请求方法以适配Google AI API
async makeRequest(endpoint, options = {}) {
this.activeConnections++;

try {
const url = `${this.baseURL}${endpoint}?key=${this.apiKey}`;

const response = await fetch(url, {
timeout: this.timeout,
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
});

if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}

return await response.json();

} finally {
this.activeConnections--;
}
}

// 执行请求
async execute(request) {
switch (request.type) {
case 'text-generation':
case 'chat':
return await this.generateContent(request);
case 'stream-chat':
return await this.streamGenerateContent(request);
default:
throw new Error(`不支持的请求类型: ${request.type}`);
}
}

// 生成内容
async generateContent(request) {
const model = request.model || 'gemini-pro';
const contents = this.formatMessages(request.messages || [{ role: 'user', content: request.prompt }]);

const payload = {
contents,
generationConfig: {
temperature: request.temperature,
topP: request.topP,
maxOutputTokens: request.maxTokens,
stopSequences: request.stop
}
};

// 移除undefined值
this.cleanPayload(payload);

const response = await this.makeRequestWithRetry(`/models/${model}:generateContent`, {
method: 'POST',
body: JSON.stringify(payload)
});

return {
content: response.candidates[0].content.parts[0].text,
usage: response.usageMetadata,
model: model,
finishReason: response.candidates[0].finishReason
};
}

// 流式生成内容
async streamGenerateContent(request) {
const model = request.model || 'gemini-pro';
const contents = this.formatMessages(request.messages || [{ role: 'user', content: request.prompt }]);

const payload = {
contents,
generationConfig: {
temperature: request.temperature,
topP: request.topP,
maxOutputTokens: request.maxTokens
}
};

this.cleanPayload(payload);

const reader = await this.makeStreamRequest(`/models/${model}:streamGenerateContent`, {
method: 'POST',
body: JSON.stringify(payload)
});

return this.createStreamIterator(reader);
}

// 格式化消息
formatMessages(messages) {
return messages.map(msg => ({
role: msg.role === 'assistant' ? 'model' : 'user',
parts: [{ text: msg.content }]
}));
}

// 清理载荷
cleanPayload(payload) {
const clean = (obj) => {
Object.keys(obj).forEach(key => {
if (obj[key] === undefined) {
delete obj[key];
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
clean(obj[key]);
}
});
};
clean(payload);
}

// 创建流迭代器
async* createStreamIterator(reader) {
const decoder = new TextDecoder();
let buffer = '';

try {
while (true) {
const { done, value } = await reader.read();

if (done) break;

buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';

for (const line of lines) {
if (line.trim() && line.startsWith('{')) {
try {
const parsed = JSON.parse(line);

if (parsed.candidates && parsed.candidates[0]?.content?.parts[0]?.text) {
yield {
content: parsed.candidates[0].content.parts[0].text,
done: false
};
}
} catch (error) {
console.warn('解析流数据失败:', error);
}
}
}
}
} finally {
this.activeConnections--;
}
}
}

工具链管理系统

AI工具管理器

// AI工具管理器
class AIToolManager {
constructor() {
this.tools = new Map();
this.categories = new Map();
this.workflows = new Map();
this.history = [];
}

// 注册工具
registerTool(tool) {
this.tools.set(tool.id, tool);

// 按类别分组
if (!this.categories.has(tool.category)) {
this.categories.set(tool.category, []);
}
this.categories.get(tool.category).push(tool.id);

console.log(`AI工具 ${tool.name} 已注册`);
}

// 获取工具
getTool(toolId) {
return this.tools.get(toolId);
}

// 按类别获取工具
getToolsByCategory(category) {
const toolIds = this.categories.get(category) || [];
return toolIds.map(id => this.tools.get(id));
}

// 搜索工具
searchTools(query) {
const results = [];
const lowerQuery = query.toLowerCase();

for (const tool of this.tools.values()) {
if (tool.name.toLowerCase().includes(lowerQuery) ||
tool.description.toLowerCase().includes(lowerQuery) ||
tool.tags.some(tag => tag.toLowerCase().includes(lowerQuery))) {
results.push(tool);
}
}

return results;
}

// 执行工具
async executeTool(toolId, input, options = {}) {
const tool = this.tools.get(toolId);

if (!tool) {
throw new Error(`工具 ${toolId} 不存在`);
}

if (!tool.isAvailable()) {
throw new Error(`工具 ${tool.name} 当前不可用`);
}

const startTime = Date.now();

try {
const result = await tool.execute(input, options);

// 记录历史
this.history.push({
toolId,
toolName: tool.name,
input,
result,
timestamp: new Date(),
duration: Date.now() - startTime,
success: true
});

return result;

} catch (error) {
// 记录错误历史
this.history.push({
toolId,
toolName: tool.name,
input,
error: error.message,
timestamp: new Date(),
duration: Date.now() - startTime,
success: false
});

throw error;
}
}

// 创建工作流
createWorkflow(name, steps) {
const workflow = new AIWorkflow(name, steps, this);
this.workflows.set(name, workflow);
return workflow;
}

// 执行工作流
async executeWorkflow(workflowName, input) {
const workflow = this.workflows.get(workflowName);

if (!workflow) {
throw new Error(`工作流 ${workflowName} 不存在`);
}

return await workflow.execute(input);
}

// 获取使用统计
getUsageStats() {
const stats = {
totalExecutions: this.history.length,
successfulExecutions: this.history.filter(h => h.success).length,
failedExecutions: this.history.filter(h => !h.success).length,
averageDuration: 0,
toolUsage: {},
recentActivity: this.history.slice(-10)
};

if (this.history.length > 0) {
const totalDuration = this.history.reduce((sum, h) => sum + h.duration, 0);
stats.averageDuration = totalDuration / this.history.length;
}

// 统计各工具使用次数
for (const record of this.history) {
if (!stats.toolUsage[record.toolId]) {
stats.toolUsage[record.toolId] = {
name: record.toolName,
count: 0,
successCount: 0,
failureCount: 0
};
}

stats.toolUsage[record.toolId].count++;

if (record.success) {
stats.toolUsage[record.toolId].successCount++;
} else {
stats.toolUsage[record.toolId].failureCount++;
}
}

return stats;
}
}

// 基础AI工具类
class BaseAITool {
constructor(config) {
this.id = config.id;
this.name = config.name;
this.description = config.description;
this.category = config.category;
this.tags = config.tags || [];
this.version = config.version || '1.0.0';
this.author = config.author;
this.dependencies = config.dependencies || [];
this.isEnabled = true;
}

// 检查工具是否可用
isAvailable() {
return this.isEnabled && this.checkDependencies();
}

// 检查依赖
checkDependencies() {
// 子类可以重写此方法
return true;
}

// 执行工具(抽象方法)
async execute(input, options = {}) {
throw new Error('execute方法必须被子类实现');
}

// 验证输入
validateInput(input) {
// 子类可以重写此方法
return true;
}

// 获取工具信息
getInfo() {
return {
id: this.id,
name: this.name,
description: this.description,
category: this.category,
tags: this.tags,
version: this.version,
author: this.author,
available: this.isAvailable()
};
}
}

// AI工作流
class AIWorkflow {
constructor(name, steps, toolManager) {
this.name = name;
this.steps = steps;
this.toolManager = toolManager;
this.history = [];
}

// 执行工作流
async execute(input) {
const execution = {
id: Date.now().toString(),
startTime: new Date(),
input,
steps: [],
output: null,
success: false
};

try {
let currentInput = input;

for (let i = 0; i < this.steps.length; i++) {
const step = this.steps[i];
const stepExecution = {
stepIndex: i,
toolId: step.toolId,
input: currentInput,
startTime: new Date()
};

try {
const result = await this.toolManager.executeTool(
step.toolId,
currentInput,
step.options
);

stepExecution.output = result;
stepExecution.success = true;
stepExecution.endTime = new Date();

// 应用输出转换
if (step.outputTransform) {
currentInput = step.outputTransform(result, currentInput);
} else {
currentInput = result;
}

} catch (error) {
stepExecution.error = error.message;
stepExecution.success = false;
stepExecution.endTime = new Date();

execution.steps.push(stepExecution);
throw error;
}

execution.steps.push(stepExecution);
}

execution.output = currentInput;
execution.success = true;

} catch (error) {
execution.error = error.message;
execution.success = false;
} finally {
execution.endTime = new Date();
execution.duration = execution.endTime - execution.startTime;
this.history.push(execution);
}

return execution;
}

// 获取执行历史
getHistory() {
return this.history;
}

// 获取成功率
getSuccessRate() {
if (this.history.length === 0) return 0;

const successCount = this.history.filter(h => h.success).length;
return successCount / this.history.length;
}
}

具体工具实现示例

// 文本摘要工具
class TextSummaryTool extends BaseAITool {
constructor(aiClient) {
super({
id: 'text-summary',
name: '文本摘要',
description: '使用AI生成文本摘要',
category: 'text-processing',
tags: ['summary', 'text', 'ai']
});

this.aiClient = aiClient;
}

// 验证输入
validateInput(input) {
if (!input || typeof input !== 'object') {
throw new Error('输入必须是对象');
}

if (!input.text || typeof input.text !== 'string') {
throw new Error('必须提供text字段');
}

if (input.text.length < 100) {
throw new Error('文本长度至少需要100个字符');
}

return true;
}

// 执行工具
async execute(input, options = {}) {
this.validateInput(input);

const { text, maxLength = 200, language = 'zh' } = input;
const { service = 'openai' } = options;

const prompt = `请为以下文本生成一个简洁的摘要,长度不超过${maxLength}个字符:

${text}`;

const response = await this.aiClient.generateText(prompt, {
service,
maxTokens: Math.ceil(maxLength * 1.5),
temperature: 0.3
});

return {
summary: response.content.trim(),
originalLength: text.length,
summaryLength: response.content.trim().length,
compressionRatio: text.length / response.content.trim().length,
usage: response.usage
};
}
}

// 语言翻译工具
class TranslationTool extends BaseAITool {
constructor(aiClient) {
super({
id: 'translation',
name: '语言翻译',
description: '使用AI进行多语言翻译',
category: 'language',
tags: ['translation', 'language', 'ai']
});

this.aiClient = aiClient;
this.supportedLanguages = {
'zh': '中文',
'en': '英文',
'ja': '日文',
'ko': '韩文',
'fr': '法文',
'de': '德文',
'es': '西班牙文',
'ru': '俄文'
};
}

// 验证输入
validateInput(input) {
if (!input || typeof input !== 'object') {
throw new Error('输入必须是对象');
}

if (!input.text || typeof input.text !== 'string') {
throw new Error('必须提供text字段');
}

if (!input.targetLanguage) {
throw new Error('必须提供targetLanguage字段');
}

if (!this.supportedLanguages[input.targetLanguage]) {
throw new Error(`不支持的目标语言: ${input.targetLanguage}`);
}

return true;
}

// 执行工具
async execute(input, options = {}) {
this.validateInput(input);

const { text, targetLanguage, sourceLanguage = 'auto' } = input;
const { service = 'openai' } = options;

const targetLangName = this.supportedLanguages[targetLanguage];
const sourceLangName = sourceLanguage === 'auto' ? '自动检测' : this.supportedLanguages[sourceLanguage];

const prompt = `请将以下文本翻译成${targetLangName},保持原文的语气和风格:

${text}`;

const response = await this.aiClient.generateText(prompt, {
service,
temperature: 0.2
});

return {
translatedText: response.content.trim(),
sourceLanguage: sourceLangName,
targetLanguage: targetLangName,
originalText: text,
usage: response.usage
};
}

// 获取支持的语言列表
getSupportedLanguages() {
return this.supportedLanguages;
}
}

// 代码生成工具
class CodeGenerationTool extends BaseAITool {
constructor(aiClient) {
super({
id: 'code-generation',
name: '代码生成',
description: '根据描述生成代码',
category: 'development',
tags: ['code', 'generation', 'programming']
});

this.aiClient = aiClient;
this.supportedLanguages = [
'javascript', 'python', 'java', 'cpp', 'csharp',
'go', 'rust', 'php', 'ruby', 'swift', 'kotlin'
];
}

// 验证输入
validateInput(input) {
if (!input || typeof input !== 'object') {
throw new Error('输入必须是对象');
}

if (!input.description || typeof input.description !== 'string') {
throw new Error('必须提供description字段');
}

if (input.language && !this.supportedLanguages.includes(input.language)) {
throw new Error(`不支持的编程语言: ${input.language}`);
}

return true;
}

// 执行工具
async execute(input, options = {}) {
this.validateInput(input);

const { description, language = 'javascript', style = 'clean' } = input;
const { service = 'openai' } = options;

const prompt = `请根据以下描述生成${language}代码,要求代码${style === 'clean' ? '简洁清晰' : '详细注释'}

${description}

请只返回代码,不要包含其他解释。`;

const response = await this.aiClient.generateText(prompt, {
service,
temperature: 0.1,
maxTokens: 2000
});

// 提取代码块
let code = response.content.trim();
const codeBlockMatch = code.match(/```(?:\w+)?\n([\s\S]*?)\n```/);
if (codeBlockMatch) {
code = codeBlockMatch[1].trim();
}

return {
code,
language,
description,
usage: response.usage
};
}

// 获取支持的编程语言
getSupportedLanguages() {
return this.supportedLanguages;
}
}

API调用优化

请求优化策略

// API调用优化器
class APIOptimizer {
constructor() {
this.requestQueue = [];
this.batchConfig = {
maxBatchSize: 10,
maxWaitTime: 1000
};
this.compressionEnabled = true;
this.cachingEnabled = true;
}

// 批量请求处理
async batchRequests(requests) {
const batches = this.createBatches(requests);
const results = [];

for (const batch of batches) {
const batchResults = await Promise.allSettled(
batch.map(request => this.optimizeRequest(request))
);

results.push(...batchResults);
}

return results;
}

// 创建批次
createBatches(requests) {
const batches = [];

for (let i = 0; i < requests.length; i += this.batchConfig.maxBatchSize) {
batches.push(requests.slice(i, i + this.batchConfig.maxBatchSize));
}

return batches;
}

// 优化单个请求
async optimizeRequest(request) {
// 请求压缩
if (this.compressionEnabled && request.data) {
request.data = await this.compressData(request.data);
}

// 请求去重
const requestHash = this.generateRequestHash(request);
if (this.isDuplicateRequest(requestHash)) {
return await this.getDuplicateResponse(requestHash);
}

// 执行请求
const response = await this.executeRequest(request);

// 缓存响应
if (this.cachingEnabled) {
await this.cacheResponse(requestHash, response);
}

return response;
}

// 数据压缩
async compressData(data) {
if (typeof data === 'string' && data.length > 1000) {
// 简单的文本压缩示例
return data.replace(/\s+/g, ' ').trim();
}
return data;
}

// 生成请求哈希
generateRequestHash(request) {
const key = JSON.stringify({
url: request.url,
method: request.method,
data: request.data
});

return this.simpleHash(key);
}

// 简单哈希函数
simpleHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // 转换为32位整数
}
return hash.toString();
}

// 检查重复请求
isDuplicateRequest(hash) {
return this.requestCache && this.requestCache.has(hash);
}

// 获取重复响应
async getDuplicateResponse(hash) {
return this.requestCache.get(hash);
}

// 缓存响应
async cacheResponse(hash, response) {
if (!this.requestCache) {
this.requestCache = new Map();
}

this.requestCache.set(hash, response);

// 设置过期时间
setTimeout(() => {
this.requestCache.delete(hash);
}, 300000); // 5分钟过期
}

// 执行请求
async executeRequest(request) {
// 这里应该调用实际的API
return await fetch(request.url, request);
}
}

// 连接池管理
class ConnectionPool {
constructor(maxConnections = 10) {
this.maxConnections = maxConnections;
this.activeConnections = 0;
this.waitingQueue = [];
}

// 获取连接
async acquireConnection() {
if (this.activeConnections < this.maxConnections) {
this.activeConnections++;
return new Connection(this);
}

// 等待连接可用
return new Promise((resolve) => {
this.waitingQueue.push(resolve);
});
}

// 释放连接
releaseConnection() {
this.activeConnections--;

if (this.waitingQueue.length > 0) {
const resolve = this.waitingQueue.shift();
this.activeConnections++;
resolve(new Connection(this));
}
}

// 获取状态
getStatus() {
return {
activeConnections: this.activeConnections,
maxConnections: this.maxConnections,
waitingRequests: this.waitingQueue.length
};
}
}

// 连接类
class Connection {
constructor(pool) {
this.pool = pool;
this.createdAt = new Date();
}

// 执行请求
async execute(request) {
try {
return await fetch(request.url, request);
} finally {
this.release();
}
}

// 释放连接
release() {
this.pool.releaseConnection();
}
}

缓存策略

// 智能缓存管理器
class SmartCacheManager {
constructor(options = {}) {
this.maxSize = options.maxSize || 100;
this.defaultTTL = options.defaultTTL || 300000; // 5分钟
this.cache = new Map();
this.accessTimes = new Map();
this.hitCount = 0;
this.missCount = 0;
}

// 生成缓存键
generateKey(request) {
const keyData = {
service: request.service,
type: request.type,
model: request.model,
prompt: request.prompt || request.messages,
temperature: request.temperature,
maxTokens: request.maxTokens
};

return this.hashObject(keyData);
}

// 对象哈希
hashObject(obj) {
const str = JSON.stringify(obj, Object.keys(obj).sort());
return this.simpleHash(str);
}

// 简单哈希
simpleHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash.toString();
}

// 获取缓存
async get(key) {
const entry = this.cache.get(key);

if (!entry) {
this.missCount++;
return null;
}

// 检查过期
if (Date.now() > entry.expiresAt) {
this.cache.delete(key);
this.accessTimes.delete(key);
this.missCount++;
return null;
}

// 更新访问时间
this.accessTimes.set(key, Date.now());
this.hitCount++;

return entry.data;
}

// 设置缓存
async set(key, data, ttl = this.defaultTTL) {
// 检查缓存大小
if (this.cache.size >= this.maxSize) {
this.evictLRU();
}

const entry = {
data,
createdAt: Date.now(),
expiresAt: Date.now() + ttl,
accessCount: 1
};

this.cache.set(key, entry);
this.accessTimes.set(key, Date.now());
}

// LRU淘汰
evictLRU() {
let oldestKey = null;
let oldestTime = Date.now();

for (const [key, time] of this.accessTimes) {
if (time < oldestTime) {
oldestTime = time;
oldestKey = key;
}
}

if (oldestKey) {
this.cache.delete(oldestKey);
this.accessTimes.delete(oldestKey);
}
}

// 清理过期缓存
cleanup() {
const now = Date.now();
const expiredKeys = [];

for (const [key, entry] of this.cache) {
if (now > entry.expiresAt) {
expiredKeys.push(key);
}
}

for (const key of expiredKeys) {
this.cache.delete(key);
this.accessTimes.delete(key);
}

return expiredKeys.length;
}

// 获取缓存统计
getStats() {
const totalRequests = this.hitCount + this.missCount;

return {
size: this.cache.size,
maxSize: this.maxSize,
hitCount: this.hitCount,
missCount: this.missCount,
hitRate: totalRequests > 0 ? this.hitCount / totalRequests : 0,
memoryUsage: this.estimateMemoryUsage()
};
}

// 估算内存使用
estimateMemoryUsage() {
let totalSize = 0;

for (const [key, entry] of this.cache) {
totalSize += key.length * 2; // 字符串大小估算
totalSize += JSON.stringify(entry.data).length * 2;
}

return totalSize;
}

// 清空缓存
clear() {
this.cache.clear();
this.accessTimes.clear();
this.hitCount = 0;
this.missCount = 0;
}
}

错误处理与重试

智能错误处理

// 智能错误处理器
class SmartErrorHandler {
constructor() {
this.errorPatterns = new Map();
this.retryStrategies = new Map();
this.errorHistory = [];
this.setupDefaultPatterns();
}

// 设置默认错误模式
setupDefaultPatterns() {
// 速率限制错误
this.addErrorPattern('rate_limit', {
patterns: [/rate limit/i, /too many requests/i, /quota exceeded/i],
strategy: 'exponential_backoff',
maxRetries: 5,
baseDelay: 1000
});

// 网络错误
this.addErrorPattern('network', {
patterns: [/network error/i, /connection failed/i, /timeout/i],
strategy: 'linear_backoff',
maxRetries: 3,
baseDelay: 2000
});

// 服务器错误
this.addErrorPattern('server', {
patterns: [/internal server error/i, /service unavailable/i, /502|503|504/],
strategy: 'exponential_backoff',
maxRetries: 3,
baseDelay: 5000
});

// 认证错误
this.addErrorPattern('auth', {
patterns: [/unauthorized/i, /invalid api key/i, /authentication failed/i],
strategy: 'no_retry',
maxRetries: 0
});

// 输入错误
this.addErrorPattern('input', {
patterns: [/invalid input/i, /bad request/i, /validation error/i],
strategy: 'no_retry',
maxRetries: 0
});
}

// 添加错误模式
addErrorPattern(name, config) {
this.errorPatterns.set(name, config);
}

// 分类错误
classifyError(error) {
const errorMessage = error.message || error.toString();

for (const [name, config] of this.errorPatterns) {
for (const pattern of config.patterns) {
if (pattern.test(errorMessage)) {
return { type: name, config };
}
}
}

return { type: 'unknown', config: { strategy: 'linear_backoff', maxRetries: 1, baseDelay: 1000 } };
}

// 处理错误
async handleError(error, context = {}) {
const classification = this.classifyError(error);

// 记录错误历史
this.errorHistory.push({
error: error.message,
type: classification.type,
timestamp: new Date(),
context
});

// 根据策略处理
switch (classification.config.strategy) {
case 'no_retry':
throw new EnhancedError(error, classification.type, 'No retry strategy');

case 'exponential_backoff':
return await this.exponentialBackoff(error, classification.config, context);

case 'linear_backoff':
return await this.linearBackoff(error, classification.config, context);

default:
throw new EnhancedError(error, classification.type, 'Unknown strategy');
}
}

// 指数退避重试
async exponentialBackoff(error, config, context) {
const { maxRetries, baseDelay } = config;
let attempt = context.attempt || 0;

if (attempt >= maxRetries) {
throw new EnhancedError(error, 'max_retries_exceeded', `Failed after ${maxRetries} attempts`);
}

const delay = baseDelay * Math.pow(2, attempt);
await this.sleep(delay);

context.attempt = attempt + 1;
return context;
}

// 线性退避重试
async linearBackoff(error, config, context) {
const { maxRetries, baseDelay } = config;
let attempt = context.attempt || 0;

if (attempt >= maxRetries) {
throw new EnhancedError(error, 'max_retries_exceeded', `Failed after ${maxRetries} attempts`);
}

const delay = baseDelay * (attempt + 1);
await this.sleep(delay);

context.attempt = attempt + 1;
return context;
}

// 睡眠函数
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

// 获取错误统计
getErrorStats() {
const stats = {
totalErrors: this.errorHistory.length,
errorsByType: {},
recentErrors: this.errorHistory.slice(-10),
errorRate: 0
};

// 按类型统计
for (const record of this.errorHistory) {
if (!stats.errorsByType[record.type]) {
stats.errorsByType[record.type] = 0;
}
stats.errorsByType[record.type]++;
}

// 计算最近1小时的错误率
const oneHourAgo = new Date(Date.now() - 3600000);
const recentErrors = this.errorHistory.filter(record => record.timestamp > oneHourAgo);
stats.errorRate = recentErrors.length;

return stats;
}
}

// 增强错误类
class EnhancedError extends Error {
constructor(originalError, type, details) {
super(originalError.message);
this.name = 'EnhancedError';
this.type = type;
this.details = details;
this.originalError = originalError;
this.timestamp = new Date();
}

// 获取用户友好的错误消息
getUserFriendlyMessage() {
switch (this.type) {
case 'rate_limit':
return '请求过于频繁,请稍后再试';
case 'network':
return '网络连接异常,请检查网络设置';
case 'server':
return '服务器暂时不可用,请稍后再试';
case 'auth':
return 'API密钥无效或已过期,请检查配置';
case 'input':
return '输入参数有误,请检查输入内容';
default:
return '发生未知错误,请联系技术支持';
}
}
}

// 重试装饰器
function withRetry(maxRetries = 3, delay = 1000) {
return function(target, propertyName, descriptor) {
const method = descriptor.value;

descriptor.value = async function(...args) {
let lastError;

for (let i = 0; i <= maxRetries; i++) {
try {
return await method.apply(this, args);
} catch (error) {
lastError = error;

if (i < maxRetries) {
await new Promise(resolve => setTimeout(resolve, delay * Math.pow(2, i)));
}
}
}

throw lastError;
};

return descriptor;
};
}

成本控制与监控

成本监控系统

// 成本监控器
class CostMonitor {
constructor() {
this.usage = new Map();
this.budgets = new Map();
this.alerts = [];
this.pricingRules = new Map();
this.setupDefaultPricing();
}

// 设置默认定价
setupDefaultPricing() {
// OpenAI定价
this.setPricingRule('openai', 'gpt-4', {
inputCostPer1k: 0.03,
outputCostPer1k: 0.06
});

this.setPricingRule('openai', 'gpt-3.5-turbo', {
inputCostPer1k: 0.0015,
outputCostPer1k: 0.002
});

// Anthropic定价
this.setPricingRule('anthropic', 'claude-3-opus', {
inputCostPer1k: 0.015,
outputCostPer1k: 0.075
});

this.setPricingRule('anthropic', 'claude-3-sonnet', {
inputCostPer1k: 0.003,
outputCostPer1k: 0.015
});
}

// 设置定价规则
setPricingRule(service, model, pricing) {
const key = `${service}:${model}`;
this.pricingRules.set(key, pricing);
}

// 记录使用情况
recordUsage(service, model, inputTokens, outputTokens, metadata = {}) {
const key = `${service}:${model}`;
const pricing = this.pricingRules.get(key);

if (!pricing) {
console.warn(`未找到 ${key} 的定价信息`);
return;
}

const inputCost = (inputTokens / 1000) * pricing.inputCostPer1k;
const outputCost = (outputTokens / 1000) * pricing.outputCostPer1k;
const totalCost = inputCost + outputCost;

const record = {
timestamp: new Date(),
service,
model,
inputTokens,
outputTokens,
totalTokens: inputTokens + outputTokens,
inputCost,
outputCost,
totalCost,
metadata
};

if (!this.usage.has(key)) {
this.usage.set(key, []);
}

this.usage.get(key).push(record);

// 检查预算警告
this.checkBudgetAlerts(service, model, totalCost);

return record;
}

// 设置预算
setBudget(service, model, budget, period = 'monthly') {
const key = `${service}:${model}`;
this.budgets.set(key, {
amount: budget,
period,
startDate: new Date(),
alertThresholds: [0.5, 0.8, 0.9] // 50%, 80%, 90%
});
}

// 检查预算警告
checkBudgetAlerts(service, model, newCost) {
const key = `${service}:${model}`;
const budget = this.budgets.get(key);

if (!budget) return;

const currentUsage = this.getCurrentPeriodUsage(service, model);
const totalCost = currentUsage.totalCost + newCost;
const usagePercentage = totalCost / budget.amount;

for (const threshold of budget.alertThresholds) {
if (usagePercentage >= threshold && !this.hasAlert(key, threshold)) {
this.createAlert({
type: 'budget_warning',
service,
model,
threshold: threshold * 100,
currentUsage: totalCost,
budgetAmount: budget.amount,
usagePercentage: usagePercentage * 100,
timestamp: new Date()
});
}
}
}

// 获取当前周期使用情况
getCurrentPeriodUsage(service, model) {
const key = `${service}:${model}`;
const records = this.usage.get(key) || [];
const budget = this.budgets.get(key);

if (!budget) {
return this.calculateUsageStats(records);
}

// 根据预算周期过滤记录
const periodStart = this.getPeriodStart(budget.period, budget.startDate);
const periodRecords = records.filter(record => record.timestamp >= periodStart);

return this.calculateUsageStats(periodRecords);
}

// 获取周期开始时间
getPeriodStart(period, startDate) {
const now = new Date();

switch (period) {
case 'daily':
return new Date(now.getFullYear(), now.getMonth(), now.getDate());
case 'weekly':
const weekStart = new Date(now);
weekStart.setDate(now.getDate() - now.getDay());
return weekStart;
case 'monthly':
return new Date(now.getFullYear(), now.getMonth(), 1);
case 'yearly':
return new Date(now.getFullYear(), 0, 1);
default:
return startDate;
}
}

// 计算使用统计
calculateUsageStats(records) {
return records.reduce((stats, record) => {
stats.totalRequests++;
stats.totalTokens += record.totalTokens;
stats.inputTokens += record.inputTokens;
stats.outputTokens += record.outputTokens;
stats.totalCost += record.totalCost;
return stats;
}, {
totalRequests: 0,
totalTokens: 0,
inputTokens: 0,
outputTokens: 0,
totalCost: 0
});
}

// 检查是否已有警告
hasAlert(key, threshold) {
return this.alerts.some(alert =>
alert.service === key.split(':')[0] &&
alert.model === key.split(':')[1] &&
alert.threshold === threshold * 100
);
}

// 创建警告
createAlert(alert) {
this.alerts.push(alert);
console.warn(`预算警告: ${alert.service}/${alert.model} 已使用 ${alert.usagePercentage.toFixed(1)}% 的预算`);
}

// 获取成本报告
getCostReport(timeRange = 'monthly') {
const report = {
timeRange,
totalCost: 0,
totalTokens: 0,
totalRequests: 0,
serviceBreakdown: {},
modelBreakdown: {},
dailyUsage: {},
topModels: [],
budgetStatus: {}
};

const now = new Date();
const startDate = this.getPeriodStart(timeRange, now);

// 汇总所有使用记录
for (const [key, records] of this.usage) {
const [service, model] = key.split(':');
const periodRecords = records.filter(record => record.timestamp >= startDate);

if (periodRecords.length === 0) continue;

const stats = this.calculateUsageStats(periodRecords);

// 总计
report.totalCost += stats.totalCost;
report.totalTokens += stats.totalTokens;
report.totalRequests += stats.totalRequests;

// 按服务分组
if (!report.serviceBreakdown[service]) {
report.serviceBreakdown[service] = { cost: 0, tokens: 0, requests: 0 };
}
report.serviceBreakdown[service].cost += stats.totalCost;
report.serviceBreakdown[service].tokens += stats.totalTokens;
report.serviceBreakdown[service].requests += stats.totalRequests;

// 按模型分组
report.modelBreakdown[key] = stats;

// 预算状态
const budget = this.budgets.get(key);
if (budget) {
report.budgetStatus[key] = {
budget: budget.amount,
used: stats.totalCost,
remaining: budget.amount - stats.totalCost,
percentage: (stats.totalCost / budget.amount) * 100
};
}
}

// 排序最常用模型
report.topModels = Object.entries(report.modelBreakdown)
.sort(([,a], [,b]) => b.totalCost - a.totalCost)
.slice(0, 10)
.map(([key, stats]) => ({ model: key, ...stats }));

return report;
}

// 获取实时成本
getRealTimeCost() {
const today = new Date();
today.setHours(0, 0, 0, 0);

let totalCost = 0;

for (const records of this.usage.values()) {
const todayRecords = records.filter(record => record.timestamp >= today);
totalCost += todayRecords.reduce((sum, record) => sum + record.totalCost, 0);
}

return totalCost;
}

// 预测成本
predictCost(service, model, estimatedTokens) {
const key = `${service}:${model}`;
const pricing = this.pricingRules.get(key);

if (!pricing) {
return null;
}

// 假设输入输出比例为1:1
const inputTokens = estimatedTokens / 2;
const outputTokens = estimatedTokens / 2;

const inputCost = (inputTokens / 1000) * pricing.inputCostPer1k;
const outputCost = (outputTokens / 1000) * pricing.outputCostPer1k;

return {
inputCost,
outputCost,
totalCost: inputCost + outputCost,
estimatedTokens
};
}
}

完整集成示例

完整的AI工具集成应用

// 完整的AI工具集成应用
class AIToolIntegrationApp {
constructor() {
this.aiClient = new UnifiedAIClient();
this.toolManager = new AIToolManager();
this.costMonitor = new CostMonitor();
this.errorHandler = new SmartErrorHandler();
this.optimizer = new APIOptimizer();
this.cache = new SmartCacheManager();

this.init();
}

// 初始化应用
async init() {
try {
// 注册AI服务
await this.setupAIServices();

// 注册AI工具
await this.setupAITools();

// 设置预算
await this.setupBudgets();

// 设置拦截器
this.setupInterceptors();

// 启动监控
this.startMonitoring();

console.log('AI工具集成应用初始化完成');

} catch (error) {
console.error('应用初始化失败:', error);
throw error;
}
}

// 设置AI服务
async setupAIServices() {
// OpenAI服务
const openaiService = new OpenAIService({
apiKey: process.env.OPENAI_API_KEY
});
this.aiClient.registerService(openaiService);

// Anthropic服务
const anthropicService = new AnthropicService({
apiKey: process.env.ANTHROPIC_API_KEY
});
this.aiClient.registerService(anthropicService);

// Google AI服务
const googleService = new GoogleAIService({
apiKey: process.env.GOOGLE_AI_API_KEY
});
this.aiClient.registerService(googleService);

// 设置默认服务
this.aiClient.setDefaultService('openai');
}

// 设置AI工具
async setupAITools() {
// 文本摘要工具
const summaryTool = new TextSummaryTool(this.aiClient);
this.toolManager.registerTool(summaryTool);

// 翻译工具
const translationTool = new TranslationTool(this.aiClient);
this.toolManager.registerTool(translationTool);

// 代码生成工具
const codeGenTool = new CodeGenerationTool(this.aiClient);
this.toolManager.registerTool(codeGenTool);

// 创建示例工作流
this.toolManager.createWorkflow('content-processing', [
{
toolId: 'text-summary',
options: { maxLength: 200 },
outputTransform: (result, input) => ({
...input,
summary: result.summary
})
},
{
toolId: 'translation',
options: { targetLanguage: 'en' },
outputTransform: (result, input) => ({
...input,
translation: result.translatedText
})
}
]);
}

// 设置预算
async setupBudgets() {
// 设置月度预算
this.costMonitor.setBudget('openai', 'gpt-4', 100, 'monthly');
this.costMonitor.setBudget('openai', 'gpt-3.5-turbo', 50, 'monthly');
this.costMonitor.setBudget('anthropic', 'claude-3-sonnet', 75, 'monthly');
}

// 设置拦截器
setupInterceptors() {
// 请求拦截器 - 缓存检查
this.aiClient.addRequestInterceptor(async (request) => {
const cacheKey = this.cache.generateKey(request);
const cachedResponse = await this.cache.get(cacheKey);

if (cachedResponse) {
request._cached = true;
request._cacheKey = cacheKey;
return request;
}

request._cacheKey = cacheKey;
return request;
});

// 响应拦截器 - 成本记录和缓存
this.aiClient.addResponseInterceptor(async (response, request) => {
// 如果是缓存响应,直接返回
if (request._cached) {
return response;
}

// 记录成本
if (response.usage) {
this.costMonitor.recordUsage(
request.service || 'openai',
request.model || 'gpt-3.5-turbo',
response.usage.prompt_tokens || response.usage.input_tokens || 0,
response.usage.completion_tokens || response.usage.output_tokens || 0,
{ requestType: request.type }
);
}

// 缓存响应
if (request._cacheKey) {
await this.cache.set(request._cacheKey, response);
}

return response;
});
}

// 启动监控
startMonitoring() {
// 每5分钟清理过期缓存
setInterval(() => {
const cleaned = this.cache.cleanup();
if (cleaned > 0) {
console.log(`清理了 ${cleaned} 个过期缓存项`);
}
}, 300000);

// 每小时生成成本报告
setInterval(() => {
const report = this.costMonitor.getCostReport('daily');
console.log('每日成本报告:', {
totalCost: report.totalCost.toFixed(4),
totalRequests: report.totalRequests,
topModel: report.topModels[0]?.model
});
}, 3600000);
}

// 处理聊天请求
async handleChat(messages, options = {}) {
try {
const response = await this.aiClient.chat(messages, options);
return {
success: true,
data: response
};
} catch (error) {
const context = { attempt: 0, messages, options };

try {
await this.errorHandler.handleError(error, context);
// 重试请求
const response = await this.aiClient.chat(messages, {
...options,
_retryContext: context
});

return {
success: true,
data: response,
retried: true
};
} catch (retryError) {
return {
success: false,
error: retryError.getUserFriendlyMessage(),
details: retryError
};
}
}
}

// 执行工具
async executeTool(toolId, input, options = {}) {
try {
const result = await this.toolManager.executeTool(toolId, input, options);
return {
success: true,
data: result
};
} catch (error) {
return {
success: false,
error: error.message
};
}
}

// 执行工作流
async executeWorkflow(workflowName, input) {
try {
const result = await this.toolManager.executeWorkflow(workflowName, input);
return {
success: true,
data: result
};
} catch (error) {
return {
success: false,
error: error.message
};
}
}

// 获取应用状态
getStatus() {
return {
services: this.aiClient.getServicesStatus(),
tools: this.toolManager.getUsageStats(),
cache: this.cache.getStats(),
cost: {
today: this.costMonitor.getRealTimeCost(),
report: this.costMonitor.getCostReport('monthly')
},
errors: this.errorHandler.getErrorStats()
};
}

// 获取成本预测
predictCost(service, model, estimatedTokens) {
return this.costMonitor.predictCost(service, model, estimatedTokens);
}

// 清理资源
cleanup() {
this.cache.clear();
console.log('应用资源已清理');
}
}

// 使用示例
const app = new AIToolIntegrationApp();

// 聊天示例
app.handleChat([
{ role: 'user', content: '请帮我写一个JavaScript函数来计算斐波那契数列' }
], {
service: 'openai',
model: 'gpt-4',
temperature: 0.1
}).then(result => {
console.log('聊天结果:', result);
});

// 工具执行示例
app.executeTool('text-summary', {
text: '这是一段很长的文本内容,需要生成摘要...',
maxLength: 100
}).then(result => {
console.log('摘要结果:', result);
});

// 工作流执行示例
app.executeWorkflow('content-processing', {
text: '这是需要处理的内容...',
targetLanguage: 'en'
}).then(result => {
console.log('工作流结果:', result);
});

最佳实践

1. 架构设计原则

  • 模块化设计: 将不同功能拆分为独立模块,便于维护和扩展
  • 接口统一: 使用统一的接口规范,简化不同AI服务的集成
  • 错误隔离: 确保单个服务的故障不会影响整个系统
  • 可观测性: 实现全面的监控和日志记录

2. 性能优化策略

  • 智能缓存: 实现多层缓存策略,减少重复请求
  • 请求合并: 将相似请求合并处理,提高效率
  • 连接池: 使用连接池管理HTTP连接,避免频繁建立连接
  • 异步处理: 使用异步编程模式,提高并发处理能力

3. 成本控制措施

  • 预算管理: 设置合理的预算限制和警告阈值
  • 使用监控: 实时监控API使用情况和成本
  • 模型选择: 根据任务复杂度选择合适的模型
  • 缓存策略: 通过缓存减少不必要的API调用

4. 安全考虑

  • API密钥管理: 安全存储和管理API密钥
  • 输入验证: 严格验证用户输入,防止注入攻击
  • 访问控制: 实现适当的访问控制和权限管理
  • 数据加密: 对敏感数据进行加密处理

5. 错误处理策略

  • 分类处理: 根据错误类型采用不同的处理策略
  • 智能重试: 实现指数退避等智能重试机制
  • 用户友好: 提供用户友好的错误提示信息
  • 错误记录: 详细记录错误信息用于问题排查

学习检验

理论问题

架构设计类

  1. 如何设计一个支持多AI服务的统一客户端?需要考虑哪些因素?
  2. 解释负载均衡在AI服务集成中的作用,常见的负载均衡策略有哪些?
  3. 如何实现AI服务的故障转移和服务降级?
  4. 描述微服务架构在AI工具集成中的优势和挑战。

性能优化类

  1. 解释不同缓存策略(LRU、LFU、TTL)在AI应用中的适用场景。
  2. 如何优化大批量AI请求的处理性能?
  3. 连接池在AI服务调用中的作用是什么?如何配置连接池参数?
  4. 描述请求合并和批处理在成本控制中的重要性。

错误处理类

  1. 如何设计一个智能的错误分类和处理系统?
  2. 解释指数退避和线性退避的区别,各自适用于什么场景?
  3. 如何处理AI服务的速率限制问题?
  4. 描述断路器模式在AI服务集成中的应用。

成本控制类

  1. 如何设计一个有效的AI使用成本监控系统?
  2. 解释不同AI模型的定价模式,如何选择最经济的模型?
  3. 如何实现基于预算的自动化成本控制?
  4. 描述缓存策略对成本控制的影响。

实践练习

初级练习

  1. 基础服务集成

    • 实现一个简单的OpenAI客户端
    • 添加基本的错误处理和重试机制
    • 实现简单的使用统计功能
  2. 工具开发

    • 开发一个文本摘要工具
    • 实现输入验证和错误处理
    • 添加使用历史记录功能
  3. 缓存实现

    • 实现一个基于内存的简单缓存系统
    • 添加TTL(生存时间)功能
    • 实现缓存统计和清理功能

中级练习

  1. 多服务集成

    • 集成OpenAI、Anthropic和Google AI服务
    • 实现统一的客户端接口
    • 添加服务路由和负载均衡功能
  2. 工作流系统

    • 设计和实现AI工具工作流系统
    • 支持条件分支和并行执行
    • 实现工作流的可视化配置
  3. 成本监控

    • 实现详细的成本监控和报告系统
    • 添加预算警告和自动限制功能
    • 实现成本预测和优化建议

高级练习

  1. 企业级集成平台

    • 构建支持插件的AI工具集成平台
    • 实现用户权限管理和多租户支持
    • 添加API网关和服务治理功能
  2. 智能调度系统

    • 实现基于负载和成本的智能请求调度
    • 添加动态扩缩容和资源优化
    • 实现预测性维护和故障预防
  3. 分布式AI服务

    • 设计分布式AI服务架构
    • 实现服务发现和配置管理
    • 添加分布式追踪和监控

项目建议

初级项目

1. 个人AI助手工具箱

项目描述: 开发一个集成多种AI工具的个人助手应用 核心功能:

  • 文本摘要和翻译
  • 代码生成和解释
  • 问答和对话功能
  • 使用统计和成本跟踪

技术要点:

  • 统一AI客户端设计
  • 基础错误处理和重试
  • 简单的缓存机制
  • 用户友好的界面设计

2. 内容创作辅助平台

项目描述: 为内容创作者提供AI辅助工具的平台 核心功能:

  • 文章大纲生成
  • 内容扩写和改写
  • SEO优化建议
  • 多语言翻译

技术要点:

  • 工具链管理
  • 模板和预设管理
  • 批量处理功能
  • 导出和分享功能

中级项目

1. 企业AI工具集成平台

项目描述: 为企业提供统一的AI工具集成和管理平台 核心功能:

  • 多AI服务集成和管理
  • 工作流设计和执行
  • 用户权限和团队管理
  • 详细的使用分析和报告

技术要点:

  • 微服务架构设计
  • API网关和服务治理
  • 工作流引擎开发
  • 监控和告警系统

2. 智能客服系统

项目描述: 基于多AI服务的智能客服解决方案 核心功能:

  • 多轮对话管理
  • 知识库集成
  • 情感分析和意图识别
  • 人工客服无缝切换

技术要点:

  • 对话状态管理
  • 实时通信实现
  • AI服务编排
  • 性能优化和扩展

高级项目

1. AI驱动的开发平台

项目描述: 为开发者提供AI辅助的全栈开发平台 核心功能:

  • 代码生成和优化
  • 自动化测试生成
  • 文档自动生成
  • 性能分析和建议

技术要点:

  • 代码分析和理解
  • 多语言支持
  • IDE集成
  • 持续集成/部署

2. 分布式AI服务网格

项目描述: 构建大规模分布式AI服务管理系统 核心功能:

  • 服务发现和注册
  • 动态负载均衡
  • 故障检测和恢复
  • 全链路监控

技术要点:

  • 服务网格架构
  • 分布式系统设计
  • 容器化和编排
  • 可观测性实现

延伸阅读

技术文档

学习资源

工具和库

社区和论坛


通过本文的学习,你应该能够掌握AI工具集成与开发的核心技术,包括多服务集成、性能优化、错误处理、成本控制等关键方面。这些知识将帮助你构建高效、可靠、经济的AI应用系统。