diff --git a/utils/tools/CustomSearchTool.js b/utils/tools/CustomSearchTool.js new file mode 100644 index 0000000..6217dda --- /dev/null +++ b/utils/tools/CustomSearchTool.js @@ -0,0 +1,100 @@ +import { AbstractTool } from './AbstractTool.js'; +import fetch from 'node-fetch'; +import { Config } from '../config.js'; + +/** + * 自定义搜索工具类 + * @class CustomSearchTool + * @extends {AbstractTool} + */ +export class CustomSearchTool extends AbstractTool { + // 工具名称 + name = 'CustomSearchTool'; + + // 工具参数 + parameters = { + properties: { + query: { + type: 'string', + description: 'Search keyword or URL', + }, + length: { + type: 'integer', + description: 'The desired length of the summary in sentences. Defaults to 3.', + }, + }, + required: ['query'], + }; + + /** + * 工具执行函数 + * @param {Object} opt - 工具参数 + * @param {Object} ai - AI对象(未使用) + * @returns {Promise} - 搜索结果或摘要 + */ + func = async function (opt, ai) { + let { query, length = 3 } = opt; + if (!query) { + return 'The query parameter is required.'; + } + + try { + // 使用OpenAI API进行搜索或摘要 + const result = await searchOrSummarize(query, length); + console.log(`Search or summarization result: ${result}`); + + // 返回搜索结果或摘要给AI + return result; + } catch (error) { + console.error('Search or summarization failed:', error); + return `Search or summarization failed, please check the logs. ${error.message}`; + } + }; + + // 工具描述 + description = 'Use OpenAI API for custom search or summarize URL content, providing comprehensive search results or summaries.'; +} + +/** + * 使用OpenAI API进行搜索或摘要 + * @param {string} query - 搜索关键词或URL + * @param {number} length - 期望的摘要长度(以句子为单位) + * @returns {Promise} - 搜索结果或摘要 + */ +async function searchOrSummarize(query, length) { + const apiKey = Config.apiKey; + const apiBaseUrl = Config.openAiBaseUrl; + const apiUrl = `${apiBaseUrl}/chat/completions`; // 将/chat/completions连接到基本URL + const model = Config.model; + + // 判断是URL还是关键词 + const isUrl = /^(https?:\/\/)/i.test(query); + + const response = await fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${apiKey}`, + }, + body: JSON.stringify({ + model: model, + messages: [ + { + role: 'system', + content: `You are a search and summarization assistant. ${isUrl ? `Please summarize the content of the following URL in English, with a length of ${length} sentences.` : `Please use English to search based on the following keywords and return a summary of ${length} sentences.`}`, + }, + { + role: 'user', + content: `${isUrl ? `Summarize this article: ${query}` : `Search: ${query}`}`, // 根据是URL还是关键词发送不同的内容 + }, + ], + max_tokens: 1000 * length, + }), + }); + + const data = await response.json(); + if (data.error) { + throw new Error(`OpenAI API Error: ${data.error.message}`); + } + return data.choices[0].message.content; +} \ No newline at end of file diff --git a/utils/tools/URLSummarizer.js b/utils/tools/URLSummarizer.js deleted file mode 100644 index 0439268..0000000 --- a/utils/tools/URLSummarizer.js +++ /dev/null @@ -1,78 +0,0 @@ -import { AbstractTool } from './AbstractTool.js'; -import fetch from 'node-fetch'; -import { Config } from '../config.js'; - -export class URLSummarizerTool extends AbstractTool { - name = 'URLSummarizer'; - - parameters = { - properties: { - url: { - type: 'string', - description: 'The URL to be summarized, cannot be empty.', - }, - length: { - type: 'integer', - description: 'The desired length of the summary in sentences. Defaults to 3.', - }, - }, - required: ['url'], - }; - - func = async function (opt, ai) { - let { url, length = 3 } = opt; - if (!url) { - return 'URL parameter is required.'; - } - - try { - // Directly use OpenAI API to summarize the URL - const summarizedText = await summarizeURL(url, length); - console.log(`Summarized text: ${summarizedText}`); - - // Return the summarized text to the AI - return summarizedText; - } catch (error) { - console.error('Summarization failed:', error); - return `Summarization failed, please check the logs. ${error.message}`; - } - }; - - description = 'Summarizes the content of a URL using OpenAI API, providing a comprehensiveness summary.'; -} - -// Use OpenAI API to summarize the URL directly -async function summarizeURL(url, length) { - const apiKey = Config.apiKey; - const apiBaseUrl = Config.openAiBaseUrl; - const apiUrl = `${apiBaseUrl}/chat/completions`; // Concatenate /chat/completions to the base URL - const model = Config.model; - - const response = await fetch(apiUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${apiKey}`, - }, - body: JSON.stringify({ - model: model, - messages: [ - { - role: 'system', - content: `You are a summarization assistant. Summarize the content of the following URL in ${length} sentences in Chinese.`, - }, - { - role: 'user', - content: `Summarize this article: ${url}`, // Directly send the URL - }, - ], - max_tokens: 1000 * length, - }), - }); - - const data = await response.json(); - if (data.error) { - throw new Error(`OpenAI API Error: ${data.error.message}`); - } - return data.choices[0].message.content; -} \ No newline at end of file