fix;优化Web搜索工具和URL解析工具的API Key处理逻辑,确保API Key为有效字符串并添加相应的警告日志。同时,增强URL输入处理,支持字符串和列表格式,确保有效URL的提取和验证。(同时改了一点reply提示词)

This commit is contained in:
minecraft1024a
2025-08-12 16:23:16 +08:00
committed by Windpicker-owo
parent a588a2a695
commit afca560305
3 changed files with 51 additions and 31 deletions

View File

@@ -86,32 +86,12 @@ def init_prompt():
{keywords_reaction_prompt} {keywords_reaction_prompt}
请注意不要输出多余内容(包括前后缀冒号和引号at或 @等 )。只输出回复内容。 请注意不要输出多余内容(包括前后缀冒号和引号at或 @等 )。只输出回复内容。
{moderation_prompt} {moderation_prompt}
不要输出多余内容(包括前后缀,冒号和引号,括号()表情包at或 @等 )。只输出一条回复就好 你的核心任务是针对 {reply_target_block} 中提到的内容,生成一段紧密相关且能推动对话的回复。你的回复应该:
现在,你说: 1. 明确回应目标消息,而不是宽泛地评论。
""", 2. 可以分享你的看法、提出相关问题,或者开个合适的玩笑。
"replyer_prompt", 3. 目的是让对话更有趣、更深入。
) 4. 不要浮夸,不要夸张修辞,不要输出多余内容(包括前后缀,冒号和引号,括号()表情包at或 @等 )。
最终请输出一条简短、完整且口语化的回复。
Prompt(
"""
{expression_habits_block}{tool_info_block}
{knowledge_prompt}{memory_block}{relation_info_block}
{extra_info_block}
{identity}
{action_descriptions}
{time_block}
你现在正在一个QQ群里聊天以下是正在进行的聊天内容
{background_dialogue_prompt}
你现在想补充说明你刚刚自己的发言内容:{target},原因是{reason}
请你根据聊天内容,组织一条新回复。注意,{target} 是刚刚你自己的发言,你要在这基础上进一步发言,请按照你自己的角度来继续进行回复。
注意保持上下文的连贯性。
你现在的心情是:{mood_state}
{reply_style}
{keywords_reaction_prompt}
请注意不要输出多余内容(包括前后缀冒号和引号at或 @等 )。只输出回复内容。
{moderation_prompt}
不要输出多余内容(包括前后缀,冒号和引号,括号()表情包at或 @等 )。只输出一条回复就好
现在,你说: 现在,你说:
""", """,
"replyer_self_prompt", "replyer_self_prompt",

View File

@@ -6,3 +6,5 @@ if "openai" in used_client_types:
from . import openai_client # noqa: F401 from . import openai_client # noqa: F401
if "gemini" in used_client_types: if "gemini" in used_client_types:
from . import gemini_client # noqa: F401 from . import gemini_client # noqa: F401
if "aiohttp_gemini" in used_client_types:
from . import aiohttp_gemini_client # noqa: F401

View File

@@ -44,7 +44,11 @@ class WebSurfingTool(BaseTool):
def __init__(self, plugin_config=None): def __init__(self, plugin_config=None):
super().__init__(plugin_config) super().__init__(plugin_config)
EXA_API_KEY = self.get_config("exa.api_key", None) EXA_API_KEY = self.get_config("exa.api_key", None)
self.exa = Exa(api_key=EXA_API_KEY) if EXA_API_KEY else None # 确保API key是字符串类型
if EXA_API_KEY and isinstance(EXA_API_KEY, str) and EXA_API_KEY.strip() != "None":
self.exa = Exa(api_key=str(EXA_API_KEY).strip())
else:
self.exa = None
if not self.exa: if not self.exa:
logger.warning("Exa API Key 未配置Exa 搜索功能将不可用。") logger.warning("Exa API Key 未配置Exa 搜索功能将不可用。")
@@ -177,11 +181,14 @@ class URLParserTool(BaseTool):
def __init__(self, plugin_config=None): def __init__(self, plugin_config=None):
super().__init__(plugin_config) super().__init__(plugin_config)
EXA_API_KEY = self.get_config("exa.api_key", None) EXA_API_KEY = self.get_config("exa.api_key", None)
if not EXA_API_KEY or EXA_API_KEY == "YOUR_API_KEY_HERE": # 确保API key是字符串类型
if (not EXA_API_KEY or
not isinstance(EXA_API_KEY, str) or
EXA_API_KEY.strip() in ("YOUR_API_KEY_HERE", "None", "")):
self.exa = None self.exa = None
logger.error("Exa API Key 未配置URL解析功能将受限。") logger.error("Exa API Key 未配置URL解析功能将受限。")
else: else:
self.exa = Exa(api_key=EXA_API_KEY) self.exa = Exa(api_key=str(EXA_API_KEY).strip())
async def _local_parse_and_summarize(self, url: str) -> Dict[str, Any]: async def _local_parse_and_summarize(self, url: str) -> Dict[str, Any]:
""" """
使用本地库(httpx, BeautifulSoup)解析URL并调用LLM进行总结。 使用本地库(httpx, BeautifulSoup)解析URL并调用LLM进行总结。
@@ -243,10 +250,41 @@ class URLParserTool(BaseTool):
""" """
执行URL内容提取和总结。优先使用Exa失败后尝试本地解析。 执行URL内容提取和总结。优先使用Exa失败后尝试本地解析。
""" """
urls = function_args.get("urls") urls_input = function_args.get("urls")
if not urls: if not urls_input:
return {"error": "URL列表不能为空。"} return {"error": "URL列表不能为空。"}
# 处理URL输入确保是列表格式
if isinstance(urls_input, str):
# 如果是字符串尝试解析为URL列表
import re
# 提取所有HTTP/HTTPS URL
url_pattern = r'https?://[^\s\],]+'
urls = re.findall(url_pattern, urls_input)
if not urls:
# 如果没有找到标准URL将整个字符串作为单个URL
if urls_input.strip().startswith(('http://', 'https://')):
urls = [urls_input.strip()]
else:
return {"error": "提供的字符串中未找到有效的URL。"}
elif isinstance(urls_input, list):
urls = [url.strip() for url in urls_input if isinstance(url, str) and url.strip()]
else:
return {"error": "URL格式不正确应为字符串或列表。"}
# 验证URL格式
valid_urls = []
for url in urls:
if url.startswith(('http://', 'https://')):
valid_urls.append(url)
else:
logger.warning(f"跳过无效URL: {url}")
if not valid_urls:
return {"error": "未找到有效的URL。"}
urls = valid_urls
logger.info(f"准备解析 {len(urls)} 个URL: {urls}")
successful_results = [] successful_results = []
error_messages = [] error_messages = []