style: restrict line-length

This commit is contained in:
Junyan Qin
2025-05-10 18:04:58 +08:00
parent b30016ed08
commit 055b389353
134 changed files with 1096 additions and 2595 deletions
+8 -16
View File
@@ -26,7 +26,9 @@ class DashScopeAPIRunner(runner.RequestRunner):
app_type: str # 应用类型
app_id: str # 应用ID
api_key: str # API Key
references_quote: str # 引用资料提示(当展示回答来源功能开启时,这个变量会作为引用资料名前的提示,可在provider.json中配置)
references_quote: (
str # 引用资料提示(当展示回答来源功能开启时,这个变量会作为引用资料名前的提示,可在provider.json中配置)
)
def __init__(self, ap: app.Application, pipeline_config: dict):
"""初始化"""
@@ -42,9 +44,7 @@ class DashScopeAPIRunner(runner.RequestRunner):
# 初始化Dashscope 参数配置
self.app_id = self.pipeline_config['ai']['dashscope-app-api']['app-id']
self.api_key = self.pipeline_config['ai']['dashscope-app-api']['api-key']
self.references_quote = self.pipeline_config['ai']['dashscope-app-api'][
'references_quote'
]
self.references_quote = self.pipeline_config['ai']['dashscope-app-api']['references_quote']
def _replace_references(self, text, references_dict):
"""阿里云百炼平台的自定义应用支持资料引用,此函数可以将引用标签替换为参考资料"""
@@ -65,9 +65,7 @@ class DashScopeAPIRunner(runner.RequestRunner):
# 使用 re.sub() 进行替换
return pattern.sub(replacement, text)
async def _preprocess_user_message(
self, query: core_entities.Query
) -> tuple[str, list[str]]:
async def _preprocess_user_message(self, query: core_entities.Query) -> tuple[str, list[str]]:
"""预处理用户消息,提取纯文本,阿里云提供的上传文件方法过于复杂,暂不支持上传文件(包括图片)"""
plain_text = ''
image_ids = []
@@ -91,9 +89,7 @@ class DashScopeAPIRunner(runner.RequestRunner):
return plain_text, image_ids
async def _agent_messages(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def _agent_messages(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""Dashscope 智能体对话请求"""
# 局部变量
@@ -151,9 +147,7 @@ class DashScopeAPIRunner(runner.RequestRunner):
content=pending_content,
)
async def _workflow_messages(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def _workflow_messages(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""Dashscope 工作流对话请求"""
# 局部变量
@@ -216,9 +210,7 @@ class DashScopeAPIRunner(runner.RequestRunner):
content=pending_content,
)
async def run(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def run(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""运行"""
if self.app_type == 'agent':
async for msg in self._agent_messages(query):
+16 -53
View File
@@ -26,10 +26,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
self.pipeline_config = pipeline_config
valid_app_types = ['chat', 'agent', 'workflow']
if (
self.pipeline_config['ai']['dify-service-api']['app-type']
not in valid_app_types
):
if self.pipeline_config['ai']['dify-service-api']['app-type'] not in valid_app_types:
raise errors.DifyAPIError(
f'不支持的 Dify 应用类型: {self.pipeline_config["ai"]["dify-service-api"]["app-type"]}'
)
@@ -48,16 +45,10 @@ class DifyServiceAPIRunner(runner.RequestRunner):
):
return resp_text
if (
self.pipeline_config['ai']['dify-service-api']['thinking-convert']
== 'original'
):
if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'original':
return resp_text
if (
self.pipeline_config['ai']['dify-service-api']['thinking-convert']
== 'remove'
):
if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'remove':
return re.sub(
r'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>.*?</details>',
'',
@@ -65,18 +56,13 @@ class DifyServiceAPIRunner(runner.RequestRunner):
flags=re.DOTALL,
)
if (
self.pipeline_config['ai']['dify-service-api']['thinking-convert']
== 'plain'
):
if self.pipeline_config['ai']['dify-service-api']['thinking-convert'] == 'plain':
pattern = r'<details style="color:gray;background-color: #f8f8f8;padding: 8px;border-radius: 4px;" open> <summary> Thinking... </summary>(.*?)</details>'
thinking_text = re.search(pattern, resp_text, flags=re.DOTALL)
content_text = re.sub(pattern, '', resp_text, flags=re.DOTALL)
return f'<think>{thinking_text.group(1)}</think>\n{content_text}'
async def _preprocess_user_message(
self, query: core_entities.Query
) -> tuple[str, list[str]]:
async def _preprocess_user_message(self, query: core_entities.Query) -> tuple[str, list[str]]:
"""预处理用户消息,提取纯文本,并将图片上传到 Dify 服务
Returns:
@@ -90,9 +76,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
if ce.type == 'text':
plain_text += ce.text
elif ce.type == 'image_base64':
image_b64, image_format = await image.extract_b64_and_format(
ce.image_base64
)
image_b64, image_format = await image.extract_b64_and_format(ce.image_base64)
file_bytes = base64.b64decode(image_b64)
file = ('img.png', file_bytes, f'image/{image_format}')
file_upload_resp = await self.dify_client.upload_file(
@@ -106,9 +90,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
return plain_text, image_ids
async def _chat_messages(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def _chat_messages(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""调用聊天助手"""
cov_id = query.session.using_conversation.uuid or ''
@@ -151,9 +133,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
if chunk['data']['node_type'] == 'answer':
yield llm_entities.Message(
role='assistant',
content=self._try_convert_thinking(
chunk['data']['outputs']['answer']
),
content=self._try_convert_thinking(chunk['data']['outputs']['answer']),
)
elif mode == 'basic':
if chunk['event'] == 'message':
@@ -166,9 +146,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
basic_mode_pending_chunk = ''
if chunk is None:
raise errors.DifyAPIError(
'Dify API 没有返回任何响应,请检查网络连接和API配置'
)
raise errors.DifyAPIError('Dify API 没有返回任何响应,请检查网络连接和API配置')
query.session.using_conversation.uuid = chunk['conversation_id']
@@ -217,9 +195,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
pending_agent_message += chunk['answer']
else:
if pending_agent_message.strip() != '':
pending_agent_message = pending_agent_message.replace(
'</details>Action:', '</details>'
)
pending_agent_message = pending_agent_message.replace('</details>Action:', '</details>')
yield llm_entities.Message(
role='assistant',
content=self._try_convert_thinking(pending_agent_message),
@@ -227,9 +203,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
pending_agent_message = ''
if chunk['event'] == 'agent_thought':
if (
chunk['tool'] != '' and chunk['observation'] != ''
): # 工具调用结果,跳过
if chunk['tool'] != '' and chunk['observation'] != '': # 工具调用结果,跳过
continue
if chunk['tool']:
@@ -258,23 +232,17 @@ class DifyServiceAPIRunner(runner.RequestRunner):
yield llm_entities.Message(
role='assistant',
content=[
llm_entities.ContentElement.from_image_url(image_url)
],
content=[llm_entities.ContentElement.from_image_url(image_url)],
)
if chunk['event'] == 'error':
raise errors.DifyAPIError('dify 服务错误: ' + chunk['message'])
if chunk is None:
raise errors.DifyAPIError(
'Dify API 没有返回任何响应,请检查网络连接和API配置'
)
raise errors.DifyAPIError('Dify API 没有返回任何响应,请检查网络连接和API配置')
query.session.using_conversation.uuid = chunk['conversation_id']
async def _workflow_messages(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def _workflow_messages(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""调用工作流"""
if not query.session.using_conversation.uuid:
@@ -315,10 +283,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
continue
if chunk['event'] == 'node_started':
if (
chunk['data']['node_type'] == 'start'
or chunk['data']['node_type'] == 'end'
):
if chunk['data']['node_type'] == 'start' or chunk['data']['node_type'] == 'end':
continue
msg = llm_entities.Message(
@@ -349,9 +314,7 @@ class DifyServiceAPIRunner(runner.RequestRunner):
yield msg
async def run(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def run(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""运行请求"""
if self.pipeline_config['ai']['dify-service-api']['app-type'] == 'chat':
async for msg in self._chat_messages(query):
+4 -12
View File
@@ -12,15 +12,11 @@ from .. import entities as llm_entities
class LocalAgentRunner(runner.RequestRunner):
"""本地Agent请求运行器"""
async def run(
self, query: core_entities.Query
) -> typing.AsyncGenerator[llm_entities.Message, None]:
async def run(self, query: core_entities.Query) -> typing.AsyncGenerator[llm_entities.Message, None]:
"""运行请求"""
pending_tool_calls = []
req_messages = (
query.prompt.messages.copy() + query.messages.copy() + [query.user_message]
)
req_messages = query.prompt.messages.copy() + query.messages.copy() + [query.user_message]
# 首次请求
msg = await query.use_llm_model.requester.invoke_llm(
@@ -45,9 +41,7 @@ class LocalAgentRunner(runner.RequestRunner):
parameters = json.loads(func.arguments)
func_ret = await self.ap.tool_mgr.execute_func_call(
query, func.name, parameters
)
func_ret = await self.ap.tool_mgr.execute_func_call(query, func.name, parameters)
msg = llm_entities.Message(
role='tool',
@@ -60,9 +54,7 @@ class LocalAgentRunner(runner.RequestRunner):
req_messages.append(msg)
except Exception as e:
# 工具调用出错,添加一个报错信息到 req_messages
err_msg = llm_entities.Message(
role='tool', content=f'err: {e}', tool_call_id=tool_call.id
)
err_msg = llm_entities.Message(role='tool', content=f'err: {e}', tool_call_id=tool_call.id)
yield err_msg