更新了个搜索工具,请在API模式里面填入可以进行网络搜索的模型,该工具会调用模型进行搜索

This commit is contained in:
gaoao-3 2025-01-01 21:29:01 +08:00
parent 1567045a60
commit b45b29be04
2 changed files with 100 additions and 78 deletions

View file

@ -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<string>} - 搜索结果或摘要
*/
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<string>} - 搜索结果或摘要
*/
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;
}

View file

@ -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;
}