From 4e68d972deead862f1a41717f473120de1ed759e Mon Sep 17 00:00:00 2001 From: gaoao-3 <140887777+gaoao-3@users.noreply.github.com> Date: Wed, 1 Jan 2025 20:27:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E4=BA=86=E4=B8=80=E4=B8=8B=E5=93=88?= =?UTF-8?q?=E5=93=88=E5=93=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/tools/URLSummarizer.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/utils/tools/URLSummarizer.js b/utils/tools/URLSummarizer.js index b22da17..ed31fba 100644 --- a/utils/tools/URLSummarizer.js +++ b/utils/tools/URLSummarizer.js @@ -9,7 +9,7 @@ export class URLSummarizerTool extends AbstractTool { properties: { url: { type: 'string', - description: 'The URL to be summarized. Cannot be empty.', + description: 'The URL to be summarized, cannot be empty.', }, length: { type: 'integer', @@ -26,11 +26,11 @@ export class URLSummarizerTool extends AbstractTool { } try { - // 使用 OpenAI API 进行文本摘要 + // Directly use OpenAI API to summarize the URL const summarizedText = await summarizeURL(url, length); console.log(`Summarized text: ${summarizedText}`); - // 将摘要结果返回给 AI + // Return the summarized text to the AI return summarizedText; } catch (error) { console.error('Summarization failed:', error); @@ -41,10 +41,11 @@ export class URLSummarizerTool extends AbstractTool { description = 'Summarizes the content of a URL using OpenAI API, providing a concise summary.'; } -// 使用 OpenAI API 进行 URL 摘要 -const summarizeURL = async (url, length) => { +// Use OpenAI API to summarize the URL directly +async function summarizeURL(url, length) { const apiKey = Config.apiKey; - const apiUrl = Config.openAiBaseUrl; + 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, { @@ -58,11 +59,14 @@ const summarizeURL = async (url, length) => { messages: [ { role: 'system', - content: `You are a helpful assistant that summarizes web pages. Please summarize the content of this URL in ${length} sentences. Provide a concise and clear summary.`, + content: `You are a helpful assistant that summarizes web pages. Please summarize the content of the following URL in ${length} sentences. Provide a concise and clear summary.`, + }, + { + role: 'user', + content: `Summarize this URL: ${url}`, // Directly send the URL }, - { role: 'user', content: `Summarize this URL: ${url}` }, ], - max_tokens: 150 * length, // 粗略估计每个句子 150 个 token + max_tokens: 150 * length, }), }); @@ -71,4 +75,4 @@ const summarizeURL = async (url, length) => { throw new Error(`OpenAI API Error: ${data.error.message}`); } return data.choices[0].message.content; -}; \ No newline at end of file +} \ No newline at end of file