mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-09 15:26:03 +00:00
style: restrict line-length
This commit is contained in:
@@ -160,9 +160,7 @@ class Application:
|
||||
"""打印访问 webui 的提示"""
|
||||
|
||||
if not os.path.exists(os.path.join('.', 'web/out')):
|
||||
self.logger.warning(
|
||||
'WebUI 文件缺失,请根据文档获取:https://docs.langbot.app/webui/intro.html'
|
||||
)
|
||||
self.logger.warning('WebUI 文件缺失,请根据文档获取:https://docs.langbot.app/webui/intro.html')
|
||||
return
|
||||
|
||||
host_ip = '127.0.0.1'
|
||||
|
||||
@@ -26,9 +26,7 @@ async def init_logging(extra_handlers: list[logging.Handler] = None) -> logging.
|
||||
if constants.debug_mode:
|
||||
level = logging.DEBUG
|
||||
|
||||
log_file_name = 'data/logs/langbot-%s.log' % time.strftime(
|
||||
'%Y-%m-%d', time.localtime()
|
||||
)
|
||||
log_file_name = 'data/logs/langbot-%s.log' % time.strftime('%Y-%m-%d', time.localtime())
|
||||
|
||||
qcg_logger = logging.getLogger('langbot')
|
||||
|
||||
@@ -43,9 +41,7 @@ async def init_logging(extra_handlers: list[logging.Handler] = None) -> logging.
|
||||
stream_handler = logging.StreamHandler(sys.stdout)
|
||||
# stream_handler.setLevel(level)
|
||||
# stream_handler.setFormatter(color_formatter)
|
||||
stream_handler.stream = open(
|
||||
sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=1
|
||||
)
|
||||
stream_handler.stream = open(sys.stdout.fileno(), mode='w', encoding='utf-8', buffering=1)
|
||||
|
||||
log_handlers: list[logging.Handler] = [
|
||||
stream_handler,
|
||||
|
||||
@@ -87,8 +87,7 @@ class Query(pydantic.BaseModel):
|
||||
"""使用的函数,由前置处理器阶段设置"""
|
||||
|
||||
resp_messages: (
|
||||
typing.Optional[list[llm_entities.Message]]
|
||||
| typing.Optional[list[platform_message.MessageChain]]
|
||||
typing.Optional[list[llm_entities.Message]] | typing.Optional[list[platform_message.MessageChain]]
|
||||
) = []
|
||||
"""由Process阶段生成的回复消息对象列表"""
|
||||
|
||||
@@ -130,13 +129,9 @@ class Conversation(pydantic.BaseModel):
|
||||
|
||||
messages: list[llm_entities.Message]
|
||||
|
||||
create_time: typing.Optional[datetime.datetime] = pydantic.Field(
|
||||
default_factory=datetime.datetime.now
|
||||
)
|
||||
create_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
||||
|
||||
update_time: typing.Optional[datetime.datetime] = pydantic.Field(
|
||||
default_factory=datetime.datetime.now
|
||||
)
|
||||
update_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
||||
|
||||
use_llm_model: requester.RuntimeLLMModel
|
||||
|
||||
@@ -162,17 +157,11 @@ class Session(pydantic.BaseModel):
|
||||
|
||||
using_conversation: typing.Optional[Conversation] = None
|
||||
|
||||
conversations: typing.Optional[list[Conversation]] = pydantic.Field(
|
||||
default_factory=list
|
||||
)
|
||||
conversations: typing.Optional[list[Conversation]] = pydantic.Field(default_factory=list)
|
||||
|
||||
create_time: typing.Optional[datetime.datetime] = pydantic.Field(
|
||||
default_factory=datetime.datetime.now
|
||||
)
|
||||
create_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
||||
|
||||
update_time: typing.Optional[datetime.datetime] = pydantic.Field(
|
||||
default_factory=datetime.datetime.now
|
||||
)
|
||||
update_time: typing.Optional[datetime.datetime] = pydantic.Field(default_factory=datetime.datetime.now)
|
||||
|
||||
semaphore: typing.Optional[asyncio.Semaphore] = None
|
||||
"""当前会话的信号量,用于限制并发"""
|
||||
|
||||
@@ -11,16 +11,14 @@ class SensitiveWordMigration(migration.Migration):
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return os.path.exists(
|
||||
'data/config/sensitive-words.json'
|
||||
) and not os.path.exists('data/metadata/sensitive-words.json')
|
||||
return os.path.exists('data/config/sensitive-words.json') and not os.path.exists(
|
||||
'data/metadata/sensitive-words.json'
|
||||
)
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
# 移动文件
|
||||
os.rename(
|
||||
'data/config/sensitive-words.json', 'data/metadata/sensitive-words.json'
|
||||
)
|
||||
os.rename('data/config/sensitive-words.json', 'data/metadata/sensitive-words.json')
|
||||
|
||||
# 重新加载配置
|
||||
await self.ap.sensitive_meta.load_config()
|
||||
|
||||
@@ -23,9 +23,7 @@ class OpenAIConfigMigration(migration.Migration):
|
||||
|
||||
self.ap.provider_cfg.data['keys']['openai'] = old_openai_config['api-keys']
|
||||
|
||||
self.ap.provider_cfg.data['model'] = old_openai_config[
|
||||
'chat-completions-params'
|
||||
]['model']
|
||||
self.ap.provider_cfg.data['model'] = old_openai_config['chat-completions-params']['model']
|
||||
|
||||
del old_openai_config['chat-completions-params']['model']
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@ class QCGCenterURLConfigMigration(migration.Migration):
|
||||
"""执行迁移"""
|
||||
|
||||
if 'qcg-center-url' not in self.ap.system_cfg.data:
|
||||
self.ap.system_cfg.data['qcg-center-url'] = (
|
||||
'https://api.qchatgpt.rockchin.top/api/v2'
|
||||
)
|
||||
self.ap.system_cfg.data['qcg-center-url'] = 'https://api.qchatgpt.rockchin.top/api/v2'
|
||||
|
||||
await self.ap.system_cfg.dump_config()
|
||||
|
||||
@@ -9,9 +9,7 @@ class AdFixwinConfigMigration(migration.Migration):
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return isinstance(
|
||||
self.ap.pipeline_cfg.data['rate-limit']['fixwin']['default'], int
|
||||
)
|
||||
return isinstance(self.ap.pipeline_cfg.data['rate-limit']['fixwin']['default'], int)
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
@@ -19,9 +17,7 @@ class AdFixwinConfigMigration(migration.Migration):
|
||||
for session_name in self.ap.pipeline_cfg.data['rate-limit']['fixwin']:
|
||||
temp_dict = {
|
||||
'window-size': 60,
|
||||
'limit': self.ap.pipeline_cfg.data['rate-limit']['fixwin'][
|
||||
session_name
|
||||
],
|
||||
'limit': self.ap.pipeline_cfg.data['rate-limit']['fixwin'][session_name],
|
||||
}
|
||||
|
||||
self.ap.pipeline_cfg.data['rate-limit']['fixwin'][session_name] = temp_dict
|
||||
|
||||
@@ -9,10 +9,7 @@ class HttpApiConfigMigration(migration.Migration):
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return (
|
||||
'http-api' not in self.ap.system_cfg.data
|
||||
or 'persistence' not in self.ap.system_cfg.data
|
||||
)
|
||||
return 'http-api' not in self.ap.system_cfg.data or 'persistence' not in self.ap.system_cfg.data
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
|
||||
@@ -11,8 +11,7 @@ class DifyAPITimeoutParamsMigration(migration.Migration):
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return (
|
||||
'timeout' not in self.ap.provider_cfg.data['dify-service-api']['chat']
|
||||
or 'timeout'
|
||||
not in self.ap.provider_cfg.data['dify-service-api']['workflow']
|
||||
or 'timeout' not in self.ap.provider_cfg.data['dify-service-api']['workflow']
|
||||
or 'agent' not in self.ap.provider_cfg.data['dify-service-api']
|
||||
)
|
||||
|
||||
|
||||
@@ -10,9 +10,7 @@ class SiliconFlowConfigMigration(migration.Migration):
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
|
||||
return (
|
||||
'siliconflow-chat-completions' not in self.ap.provider_cfg.data['requester']
|
||||
)
|
||||
return 'siliconflow-chat-completions' not in self.ap.provider_cfg.data['requester']
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
|
||||
@@ -13,17 +13,12 @@ class DifyThinkingConfigMigration(migration.Migration):
|
||||
if 'options' not in self.ap.provider_cfg.data['dify-service-api']:
|
||||
return True
|
||||
|
||||
if (
|
||||
'convert-thinking-tips'
|
||||
not in self.ap.provider_cfg.data['dify-service-api']['options']
|
||||
):
|
||||
if 'convert-thinking-tips' not in self.ap.provider_cfg.data['dify-service-api']['options']:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
self.ap.provider_cfg.data['dify-service-api']['options'] = {
|
||||
'convert-thinking-tips': 'plain'
|
||||
}
|
||||
self.ap.provider_cfg.data['dify-service-api']['options'] = {'convert-thinking-tips': 'plain'}
|
||||
await self.ap.provider_cfg.dump_config()
|
||||
|
||||
@@ -24,8 +24,6 @@ class GewechatFileUrlConfigMigration(migration.Migration):
|
||||
if adapter['adapter'] == 'gewechat':
|
||||
if 'gewechat_file_url' not in adapter:
|
||||
parsed_url = urlparse(adapter['gewechat_url'])
|
||||
adapter['gewechat_file_url'] = (
|
||||
f'{parsed_url.scheme}://{parsed_url.hostname}:2532'
|
||||
)
|
||||
adapter['gewechat_file_url'] = f'{parsed_url.scheme}://{parsed_url.hostname}:2532'
|
||||
|
||||
await self.ap.platform_cfg.dump_config()
|
||||
|
||||
@@ -3,24 +3,23 @@ from __future__ import annotations
|
||||
from .. import migration
|
||||
|
||||
|
||||
@migration.migration_class("tg-dingtalk-markdown", 38)
|
||||
@migration.migration_class('tg-dingtalk-markdown', 38)
|
||||
class TgDingtalkMarkdownMigration(migration.Migration):
|
||||
"""迁移"""
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
|
||||
|
||||
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||
if adapter['adapter'] in ['dingtalk','telegram']:
|
||||
if adapter['adapter'] in ['dingtalk', 'telegram']:
|
||||
if 'markdown_card' not in adapter:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移"""
|
||||
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||
if adapter['adapter'] in ['dingtalk','telegram']:
|
||||
if adapter['adapter'] in ['dingtalk', 'telegram']:
|
||||
if 'markdown_card' not in adapter:
|
||||
adapter['markdown_card'] = False
|
||||
await self.ap.platform_cfg.dump_config()
|
||||
|
||||
@@ -3,20 +3,19 @@ from __future__ import annotations
|
||||
from .. import migration
|
||||
|
||||
|
||||
@migration.migration_class("modelscope-config-completion", 39)
|
||||
@migration.migration_class('modelscope-config-completion', 39)
|
||||
class ModelScopeConfigCompletionMigration(migration.Migration):
|
||||
"""ModelScope配置迁移
|
||||
"""
|
||||
"""ModelScope配置迁移"""
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移
|
||||
"""
|
||||
return 'modelscope-chat-completions' not in self.ap.provider_cfg.data['requester'] \
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return (
|
||||
'modelscope-chat-completions' not in self.ap.provider_cfg.data['requester']
|
||||
or 'modelscope' not in self.ap.provider_cfg.data['keys']
|
||||
)
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移
|
||||
"""
|
||||
"""执行迁移"""
|
||||
if 'modelscope-chat-completions' not in self.ap.provider_cfg.data['requester']:
|
||||
self.ap.provider_cfg.data['requester']['modelscope-chat-completions'] = {
|
||||
'base-url': 'https://api-inference.modelscope.cn/v1',
|
||||
|
||||
@@ -3,20 +3,19 @@ from __future__ import annotations
|
||||
from .. import migration
|
||||
|
||||
|
||||
@migration.migration_class("ppio-config", 40)
|
||||
@migration.migration_class('ppio-config', 40)
|
||||
class PPIOConfigMigration(migration.Migration):
|
||||
"""PPIO配置迁移
|
||||
"""
|
||||
"""PPIO配置迁移"""
|
||||
|
||||
async def need_migrate(self) -> bool:
|
||||
"""判断当前环境是否需要运行此迁移
|
||||
"""
|
||||
return 'ppio-chat-completions' not in self.ap.provider_cfg.data['requester'] \
|
||||
"""判断当前环境是否需要运行此迁移"""
|
||||
return (
|
||||
'ppio-chat-completions' not in self.ap.provider_cfg.data['requester']
|
||||
or 'ppio' not in self.ap.provider_cfg.data['keys']
|
||||
)
|
||||
|
||||
async def run(self):
|
||||
"""执行迁移
|
||||
"""
|
||||
"""执行迁移"""
|
||||
if 'ppio-chat-completions' not in self.ap.provider_cfg.data['requester']:
|
||||
self.ap.provider_cfg.data['requester']['ppio-chat-completions'] = {
|
||||
'base-url': 'https://api.ppinfra.com/v3/openai',
|
||||
|
||||
@@ -35,9 +35,7 @@ class TaskContext:
|
||||
if action is not None:
|
||||
self.set_current_action(action)
|
||||
|
||||
self._log(
|
||||
f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} | {self.current_action} | {msg}'
|
||||
)
|
||||
self._log(f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} | {self.current_action} | {msg}')
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {'current_action': self.current_action, 'log': self.log}
|
||||
@@ -104,9 +102,7 @@ class TaskWrapper:
|
||||
name: str = '',
|
||||
label: str = '',
|
||||
context: TaskContext = None,
|
||||
scopes: list[core_entities.LifecycleControlScope] = [
|
||||
core_entities.LifecycleControlScope.APPLICATION
|
||||
],
|
||||
scopes: list[core_entities.LifecycleControlScope] = [core_entities.LifecycleControlScope.APPLICATION],
|
||||
):
|
||||
self.id = TaskWrapper._id_index
|
||||
TaskWrapper._id_index += 1
|
||||
@@ -141,7 +137,9 @@ class TaskWrapper:
|
||||
exception_traceback = 'Traceback (most recent call last):\n'
|
||||
|
||||
for frame in self.task_stack:
|
||||
exception_traceback += f' File "{frame.f_code.co_filename}", line {frame.f_lineno}, in {frame.f_code.co_name}\n'
|
||||
exception_traceback += (
|
||||
f' File "{frame.f_code.co_filename}", line {frame.f_lineno}, in {frame.f_code.co_name}\n'
|
||||
)
|
||||
|
||||
exception_traceback += f' {self.assume_exception().__str__()}\n'
|
||||
|
||||
@@ -156,13 +154,9 @@ class TaskWrapper:
|
||||
'runtime': {
|
||||
'done': self.task.done(),
|
||||
'state': self.task._state,
|
||||
'exception': self.assume_exception().__str__()
|
||||
if self.assume_exception() is not None
|
||||
else None,
|
||||
'exception': self.assume_exception().__str__() if self.assume_exception() is not None else None,
|
||||
'exception_traceback': exception_traceback,
|
||||
'result': self.assume_result().__str__()
|
||||
if self.assume_result() is not None
|
||||
else None,
|
||||
'result': self.assume_result().__str__() if self.assume_result() is not None else None,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -191,13 +185,9 @@ class AsyncTaskManager:
|
||||
name: str = '',
|
||||
label: str = '',
|
||||
context: TaskContext = None,
|
||||
scopes: list[core_entities.LifecycleControlScope] = [
|
||||
core_entities.LifecycleControlScope.APPLICATION
|
||||
],
|
||||
scopes: list[core_entities.LifecycleControlScope] = [core_entities.LifecycleControlScope.APPLICATION],
|
||||
) -> TaskWrapper:
|
||||
wrapper = TaskWrapper(
|
||||
self.ap, coro, task_type, kind, name, label, context, scopes
|
||||
)
|
||||
wrapper = TaskWrapper(self.ap, coro, task_type, kind, name, label, context, scopes)
|
||||
self.tasks.append(wrapper)
|
||||
return wrapper
|
||||
|
||||
@@ -208,9 +198,7 @@ class AsyncTaskManager:
|
||||
name: str = '',
|
||||
label: str = '',
|
||||
context: TaskContext = None,
|
||||
scopes: list[core_entities.LifecycleControlScope] = [
|
||||
core_entities.LifecycleControlScope.APPLICATION
|
||||
],
|
||||
scopes: list[core_entities.LifecycleControlScope] = [core_entities.LifecycleControlScope.APPLICATION],
|
||||
) -> TaskWrapper:
|
||||
return self.create_task(coro, 'user', kind, name, label, context, scopes)
|
||||
|
||||
@@ -225,9 +213,7 @@ class AsyncTaskManager:
|
||||
type: str = None,
|
||||
) -> dict:
|
||||
return {
|
||||
'tasks': [
|
||||
t.to_dict() for t in self.tasks if type is None or t.task_type == type
|
||||
],
|
||||
'tasks': [t.to_dict() for t in self.tasks if type is None or t.task_type == type],
|
||||
'id_index': TaskWrapper._id_index,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user