UNPKG

aliyun-bailian-mcp-server

Version:
151 lines (128 loc) 4.88 kB
/** * 百炼API客户端 - 对接通义千问API */ const axios = require('axios'); class BailianClient { constructor(apiKey, appId, baseUrl = 'https://dashscope.aliyuncs.com/compatible-mode/v1') { this.apiKey = apiKey; this.appId = appId; this.baseUrl = baseUrl; console.log(`初始化百炼客户端, baseUrl: ${baseUrl}, appId: ${appId ? '已设置' : '未设置'}`); this.client = axios.create({ baseURL: this.baseUrl, headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, // 增加超时设置 timeout: 30000 }); } /** * 调用通义千问对话接口 * @param {Array} messages 对话历史 * @param {Object} options 额外选项 * @returns {Promise<Object>} API响应 */ async chatCompletion(messages, options = {}) { try { const modelName = options.modelName || 'qwen-plus'; const temperature = options.temperature || 0.7; const stream = options.stream || false; const enableSearch = options.enableSearch || false; console.log(`准备调用百炼API, 模型: ${modelName}, 消息数: ${messages.length}`); const requestBody = { model: modelName, messages: messages, temperature: temperature, stream: stream }; // 如果设置了appId,添加到请求中 if (this.appId) { requestBody.extra_params = { app_id: this.appId }; } // 如果启用了搜索增强,添加相关配置 if (enableSearch) { if (!requestBody.extra_params) { requestBody.extra_params = {}; } requestBody.extra_params.enable_search = true; } console.log(`发送API请求: ${this.baseUrl}/chat/completions`); const response = await this.client.post('/chat/completions', requestBody); console.log(`百炼API调用成功, 状态码: ${response.status}`); return response.data; } catch (error) { console.error('百炼API调用失败:', error.message); if (error.response) { console.error('错误状态码:', error.response.status); console.error('错误详情:', JSON.stringify(error.response.data)); console.error('请求URL:', error.config?.url); console.error('请求头:', JSON.stringify(error.config?.headers, (key, value) => { // 隐藏敏感信息 if (key === 'Authorization') return 'Bearer ***'; return value; })); console.error('请求数据:', JSON.stringify(error.config?.data)); throw new Error(`API错误: ${error.response.status} - ${JSON.stringify(error.response.data)}`); } else if (error.request) { console.error('请求已发送但未收到响应', error.request); throw new Error('无法连接到百炼API服务器'); } else { console.error('请求配置错误:', error.message); throw new Error(`请求配置错误: ${error.message}`); } } } /** * 调用通义千问流式对话接口 * @param {Array} messages 对话历史 * @param {Object} options 额外选项 * @returns {Promise<ReadableStream>} 流式API响应 */ async streamChatCompletion(messages, options = {}) { try { const modelName = options.modelName || 'qwen-plus'; const temperature = options.temperature || 0.7; const enableSearch = options.enableSearch || false; console.log(`准备流式调用百炼API, 模型: ${modelName}, 消息数: ${messages.length}`); const requestBody = { model: modelName, messages, temperature, stream: true, }; // 添加appId (如果有) if (this.appId) { requestBody.extra_params = { app_id: this.appId }; } // 添加联网搜索功能(如果启用) if (enableSearch) { if (!requestBody.extra_params) { requestBody.extra_params = {}; } requestBody.extra_params.enable_search = true; } // 添加函数调用功能(如果提供) if (options.tools) { requestBody.tools = options.tools; } console.log(`发送流式API请求: ${this.baseUrl}/chat/completions`); const response = await this.client.post('/chat/completions', requestBody, { responseType: 'stream' }); console.log(`流式API连接建立成功`); return response.data; } catch (error) { console.error('百炼API流式调用失败:', error.message); if (error.response) { console.error('错误状态码:', error.response.status); console.error('错误详情:', JSON.stringify(error.response.data || '无详细信息')); } throw error; } } } module.exports = BailianClient;