mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-04 12:56:02 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6761949ca | ||
|
|
6afac1f593 | ||
|
|
4d1a270d22 | ||
|
|
a7888f5536 | ||
|
|
b9049e91cf | ||
|
|
7db56c8e77 | ||
|
|
50563cb957 | ||
|
|
18ae2299a7 | ||
|
|
7463e0aab9 | ||
|
|
c92d47bb95 | ||
|
|
0b1af7df91 | ||
|
|
a9104eb2da | ||
|
|
abbd15d5cc | ||
|
|
aadfa14d59 | ||
|
|
4cd10bbe25 |
@@ -39,7 +39,8 @@
|
|||||||
|
|
||||||
<a href="https://github.com/RockChinQ/qcg-installer">安装器源码</a> |
|
<a href="https://github.com/RockChinQ/qcg-installer">安装器源码</a> |
|
||||||
<a href="https://github.com/RockChinQ/qcg-tester">测试工程源码</a> |
|
<a href="https://github.com/RockChinQ/qcg-tester">测试工程源码</a> |
|
||||||
|
<a href="https://github.com/RockChinQ/qcg-center">遥测服务端源码</a> |
|
||||||
<a href="https://github.com/the-lazy-me/QChatGPT-Wiki">官方文档储存库</a>
|
<a href="https://github.com/the-lazy-me/QChatGPT-Wiki">官方文档储存库</a>
|
||||||
|
|
||||||
<img alt="回复效果(带有联网插件)" src="https://qchatgpt.rockchin.top/assets/image/QChatGPT-1211.png" width="500px"/>
|
<img alt="回复效果(带有联网插件)" src="https://qchatgpt.rockchin.top/assets/image/QChatGPT-0516.png" width="500px"/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ from .groups import plugin
|
|||||||
from ...core import app
|
from ...core import app
|
||||||
|
|
||||||
|
|
||||||
BACKEND_URL = "https://api.qchatgpt.rockchin.top/api/v2"
|
|
||||||
|
|
||||||
class V2CenterAPI:
|
class V2CenterAPI:
|
||||||
"""中央服务器 v2 API 交互类"""
|
"""中央服务器 v2 API 交互类"""
|
||||||
|
|
||||||
@@ -23,7 +21,7 @@ class V2CenterAPI:
|
|||||||
plugin: plugin.V2PluginDataAPI = None
|
plugin: plugin.V2PluginDataAPI = None
|
||||||
"""插件 API 组"""
|
"""插件 API 组"""
|
||||||
|
|
||||||
def __init__(self, ap: app.Application, basic_info: dict = None, runtime_info: dict = None):
|
def __init__(self, ap: app.Application, backend_url: str, basic_info: dict = None, runtime_info: dict = None):
|
||||||
"""初始化"""
|
"""初始化"""
|
||||||
|
|
||||||
logging.debug("basic_info: %s, runtime_info: %s", basic_info, runtime_info)
|
logging.debug("basic_info: %s, runtime_info: %s", basic_info, runtime_info)
|
||||||
@@ -31,7 +29,7 @@ class V2CenterAPI:
|
|||||||
apigroup.APIGroup._basic_info = basic_info
|
apigroup.APIGroup._basic_info = basic_info
|
||||||
apigroup.APIGroup._runtime_info = runtime_info
|
apigroup.APIGroup._runtime_info = runtime_info
|
||||||
|
|
||||||
self.main = main.V2MainDataAPI(BACKEND_URL, ap)
|
self.main = main.V2MainDataAPI(backend_url, ap)
|
||||||
self.usage = usage.V2UsageDataAPI(BACKEND_URL, ap)
|
self.usage = usage.V2UsageDataAPI(backend_url, ap)
|
||||||
self.plugin = plugin.V2PluginDataAPI(BACKEND_URL, ap)
|
self.plugin = plugin.V2PluginDataAPI(backend_url, ap)
|
||||||
|
|
||||||
|
|||||||
20
pkg/config/migrations/m007_qcg_center_url.py
Normal file
20
pkg/config/migrations/m007_qcg_center_url.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from .. import migration
|
||||||
|
|
||||||
|
|
||||||
|
@migration.migration_class("qcg-center-url-config", 7)
|
||||||
|
class QCGCenterURLConfigMigration(migration.Migration):
|
||||||
|
"""迁移"""
|
||||||
|
|
||||||
|
async def need_migrate(self) -> bool:
|
||||||
|
"""判断当前环境是否需要运行此迁移"""
|
||||||
|
return "qcg-center-url" not in self.ap.system_cfg.data
|
||||||
|
|
||||||
|
async def run(self):
|
||||||
|
"""执行迁移"""
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
|
await self.ap.system_cfg.dump_config()
|
||||||
@@ -67,7 +67,7 @@ class Query(pydantic.BaseModel):
|
|||||||
use_funcs: typing.Optional[list[tools_entities.LLMFunction]] = None
|
use_funcs: typing.Optional[list[tools_entities.LLMFunction]] = None
|
||||||
"""使用的函数,由前置处理器阶段设置"""
|
"""使用的函数,由前置处理器阶段设置"""
|
||||||
|
|
||||||
resp_messages: typing.Optional[list[llm_entities.Message]] = []
|
resp_messages: typing.Optional[list[llm_entities.Message]] | typing.Optional[list[mirai.MessageChain]] = []
|
||||||
"""由Process阶段生成的回复消息对象列表"""
|
"""由Process阶段生成的回复消息对象列表"""
|
||||||
|
|
||||||
resp_message_chain: typing.Optional[list[mirai.MessageChain]] = None
|
resp_message_chain: typing.Optional[list[mirai.MessageChain]] = None
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ class BuildAppStage(stage.BootingStage):
|
|||||||
|
|
||||||
center_v2_api = center_v2.V2CenterAPI(
|
center_v2_api = center_v2.V2CenterAPI(
|
||||||
ap,
|
ap,
|
||||||
|
backend_url=ap.system_cfg.data["qcg-center-url"],
|
||||||
basic_info={
|
basic_info={
|
||||||
"host_id": identifier.identifier["host_id"],
|
"host_id": identifier.identifier["host_id"],
|
||||||
"instance_id": identifier.identifier["instance_id"],
|
"instance_id": identifier.identifier["instance_id"],
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import importlib
|
|||||||
|
|
||||||
from .. import stage, app
|
from .. import stage, app
|
||||||
from ...config import migration
|
from ...config import migration
|
||||||
from ...config.migrations import m001_sensitive_word_migration, m002_openai_config_migration, m003_anthropic_requester_cfg_completion, m004_moonshot_cfg_completion, m006_vision_config
|
from ...config.migrations import m001_sensitive_word_migration, m002_openai_config_migration, m003_anthropic_requester_cfg_completion, m004_moonshot_cfg_completion
|
||||||
from ...config.migrations import m005_deepseek_cfg_completion
|
from ...config.migrations import m005_deepseek_cfg_completion, m006_vision_config, m007_qcg_center_url
|
||||||
|
|
||||||
|
|
||||||
@stage.stage_class("MigrationStage")
|
@stage.stage_class("MigrationStage")
|
||||||
|
|||||||
@@ -163,13 +163,13 @@ class ContentFilterStage(stage.PipelineStage):
|
|||||||
)
|
)
|
||||||
elif stage_inst_name == 'PostContentFilterStage':
|
elif stage_inst_name == 'PostContentFilterStage':
|
||||||
# 仅处理 query.resp_messages[-1].content 是 str 的情况
|
# 仅处理 query.resp_messages[-1].content 是 str 的情况
|
||||||
if isinstance(query.resp_messages[-1].content, str):
|
if isinstance(query.resp_messages[-1], llm_entities.Message) and isinstance(query.resp_messages[-1].content, str):
|
||||||
return await self._post_process(
|
return await self._post_process(
|
||||||
query.resp_messages[-1].content,
|
query.resp_messages[-1].content,
|
||||||
query
|
query
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.ap.logger.debug(f"resp_messages[-1] 不是 str 类型,跳过内容过滤器检查。")
|
self.ap.logger.debug(f"resp_messages[-1] 不是 Message 类型或 query.resp_messages[-1].content 不是 str 类型,跳过内容过滤器检查。")
|
||||||
return entities.StageProcessResult(
|
return entities.StageProcessResult(
|
||||||
result_type=entities.ResultType.CONTINUE,
|
result_type=entities.ResultType.CONTINUE,
|
||||||
new_query=query
|
new_query=query
|
||||||
|
|||||||
@@ -34,18 +34,18 @@ class LongTextProcessStage(stage.PipelineStage):
|
|||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
use_font = "C:/Windows/Fonts/msyh.ttc"
|
use_font = "C:/Windows/Fonts/msyh.ttc"
|
||||||
if not os.path.exists(use_font):
|
if not os.path.exists(use_font):
|
||||||
self.ap.logger.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。")
|
self.ap.logger.warn("未找到字体文件,且无法使用Windows自带字体,更换为转发消息组件以发送长消息,您可以在配置文件中调整相关设置。")
|
||||||
config['blob_message_strategy'] = "forward"
|
config['blob_message_strategy'] = "forward"
|
||||||
else:
|
else:
|
||||||
self.ap.logger.info("使用Windows自带字体:" + use_font)
|
self.ap.logger.info("使用Windows自带字体:" + use_font)
|
||||||
config['font-path'] = use_font
|
config['font-path'] = use_font
|
||||||
else:
|
else:
|
||||||
self.ap.logger.warn("未找到字体文件,且无法使用系统自带字体,更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。")
|
self.ap.logger.warn("未找到字体文件,且无法使用系统自带字体,更换为转发消息组件以发送长消息,您可以在配置文件中调整相关设置。")
|
||||||
|
|
||||||
self.ap.platform_cfg.data['long-text-process']['strategy'] = "forward"
|
self.ap.platform_cfg.data['long-text-process']['strategy'] = "forward"
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
self.ap.logger.error("加载字体文件失败({}),更换为转发消息组件以发送长消息,您可以在config.py中调整相关设置。".format(use_font))
|
self.ap.logger.error("加载字体文件失败({}),更换为转发消息组件以发送长消息,您可以在配置文件中调整相关设置。".format(use_font))
|
||||||
|
|
||||||
self.ap.platform_cfg.data['long-text-process']['strategy'] = "forward"
|
self.ap.platform_cfg.data['long-text-process']['strategy'] = "forward"
|
||||||
|
|
||||||
|
|||||||
@@ -42,12 +42,7 @@ class ChatMessageHandler(handler.MessageHandler):
|
|||||||
if event_ctx.event.reply is not None:
|
if event_ctx.event.reply is not None:
|
||||||
mc = mirai.MessageChain(event_ctx.event.reply)
|
mc = mirai.MessageChain(event_ctx.event.reply)
|
||||||
|
|
||||||
query.resp_messages.append(
|
query.resp_messages.append(mc)
|
||||||
llm_entities.Message(
|
|
||||||
role='plugin',
|
|
||||||
content=mc,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
yield entities.StageProcessResult(
|
yield entities.StageProcessResult(
|
||||||
result_type=entities.ResultType.CONTINUE,
|
result_type=entities.ResultType.CONTINUE,
|
||||||
|
|||||||
@@ -48,12 +48,7 @@ class CommandHandler(handler.MessageHandler):
|
|||||||
if event_ctx.event.reply is not None:
|
if event_ctx.event.reply is not None:
|
||||||
mc = mirai.MessageChain(event_ctx.event.reply)
|
mc = mirai.MessageChain(event_ctx.event.reply)
|
||||||
|
|
||||||
query.resp_messages.append(
|
query.resp_messages.append(mc)
|
||||||
llm_entities.Message(
|
|
||||||
role='command',
|
|
||||||
content=str(mc),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
yield entities.StageProcessResult(
|
yield entities.StageProcessResult(
|
||||||
result_type=entities.ResultType.CONTINUE,
|
result_type=entities.ResultType.CONTINUE,
|
||||||
|
|||||||
@@ -32,80 +32,49 @@ class ResponseWrapper(stage.PipelineStage):
|
|||||||
) -> typing.AsyncGenerator[entities.StageProcessResult, None]:
|
) -> typing.AsyncGenerator[entities.StageProcessResult, None]:
|
||||||
"""处理
|
"""处理
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if query.resp_messages[-1].role == 'command':
|
# 如果 resp_messages[-1] 已经是 MessageChain 了
|
||||||
# query.resp_message_chain.append(mirai.MessageChain("[bot] "+query.resp_messages[-1].content))
|
if isinstance(query.resp_messages[-1], mirai.MessageChain):
|
||||||
query.resp_message_chain.append(query.resp_messages[-1].get_content_mirai_message_chain(prefix_text='[bot] '))
|
query.resp_message_chain.append(query.resp_messages[-1])
|
||||||
|
|
||||||
yield entities.StageProcessResult(
|
yield entities.StageProcessResult(
|
||||||
result_type=entities.ResultType.CONTINUE,
|
result_type=entities.ResultType.CONTINUE,
|
||||||
new_query=query
|
new_query=query
|
||||||
)
|
)
|
||||||
elif query.resp_messages[-1].role == 'plugin':
|
|
||||||
# if not isinstance(query.resp_messages[-1].content, mirai.MessageChain):
|
|
||||||
# query.resp_message_chain.append(mirai.MessageChain(query.resp_messages[-1].content))
|
|
||||||
# else:
|
|
||||||
# query.resp_message_chain.append(query.resp_messages[-1].content)
|
|
||||||
query.resp_message_chain.append(query.resp_messages[-1].get_content_mirai_message_chain())
|
|
||||||
|
|
||||||
yield entities.StageProcessResult(
|
|
||||||
result_type=entities.ResultType.CONTINUE,
|
|
||||||
new_query=query
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
if query.resp_messages[-1].role == 'command':
|
||||||
|
# query.resp_message_chain.append(mirai.MessageChain("[bot] "+query.resp_messages[-1].content))
|
||||||
|
query.resp_message_chain.append(query.resp_messages[-1].get_content_mirai_message_chain(prefix_text='[bot] '))
|
||||||
|
|
||||||
if query.resp_messages[-1].role == 'assistant':
|
yield entities.StageProcessResult(
|
||||||
result = query.resp_messages[-1]
|
result_type=entities.ResultType.CONTINUE,
|
||||||
session = await self.ap.sess_mgr.get_session(query)
|
new_query=query
|
||||||
|
)
|
||||||
|
elif query.resp_messages[-1].role == 'plugin':
|
||||||
|
# if not isinstance(query.resp_messages[-1].content, mirai.MessageChain):
|
||||||
|
# query.resp_message_chain.append(mirai.MessageChain(query.resp_messages[-1].content))
|
||||||
|
# else:
|
||||||
|
# query.resp_message_chain.append(query.resp_messages[-1].content)
|
||||||
|
query.resp_message_chain.append(query.resp_messages[-1].get_content_mirai_message_chain())
|
||||||
|
|
||||||
reply_text = ''
|
yield entities.StageProcessResult(
|
||||||
|
result_type=entities.ResultType.CONTINUE,
|
||||||
|
new_query=query
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
|
||||||
if result.content is not None: # 有内容
|
if query.resp_messages[-1].role == 'assistant':
|
||||||
reply_text = str(result.get_content_mirai_message_chain())
|
result = query.resp_messages[-1]
|
||||||
|
session = await self.ap.sess_mgr.get_session(query)
|
||||||
|
|
||||||
# ============= 触发插件事件 ===============
|
reply_text = ''
|
||||||
event_ctx = await self.ap.plugin_mgr.emit_event(
|
|
||||||
event=events.NormalMessageResponded(
|
|
||||||
launcher_type=query.launcher_type.value,
|
|
||||||
launcher_id=query.launcher_id,
|
|
||||||
sender_id=query.sender_id,
|
|
||||||
session=session,
|
|
||||||
prefix='',
|
|
||||||
response_text=reply_text,
|
|
||||||
finish_reason='stop',
|
|
||||||
funcs_called=[fc.function.name for fc in result.tool_calls] if result.tool_calls is not None else [],
|
|
||||||
query=query
|
|
||||||
)
|
|
||||||
)
|
|
||||||
if event_ctx.is_prevented_default():
|
|
||||||
yield entities.StageProcessResult(
|
|
||||||
result_type=entities.ResultType.INTERRUPT,
|
|
||||||
new_query=query
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
if event_ctx.event.reply is not None:
|
|
||||||
|
|
||||||
query.resp_message_chain.append(mirai.MessageChain(event_ctx.event.reply))
|
|
||||||
|
|
||||||
else:
|
if result.content is not None: # 有内容
|
||||||
|
reply_text = str(result.get_content_mirai_message_chain())
|
||||||
|
|
||||||
query.resp_message_chain.append(result.get_content_mirai_message_chain())
|
# ============= 触发插件事件 ===============
|
||||||
|
|
||||||
yield entities.StageProcessResult(
|
|
||||||
result_type=entities.ResultType.CONTINUE,
|
|
||||||
new_query=query
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.tool_calls is not None: # 有函数调用
|
|
||||||
|
|
||||||
function_names = [tc.function.name for tc in result.tool_calls]
|
|
||||||
|
|
||||||
reply_text = f'调用函数 {".".join(function_names)}...'
|
|
||||||
|
|
||||||
query.resp_message_chain.append(mirai.MessageChain([mirai.Plain(reply_text)]))
|
|
||||||
|
|
||||||
if self.ap.platform_cfg.data['track-function-calls']:
|
|
||||||
|
|
||||||
event_ctx = await self.ap.plugin_mgr.emit_event(
|
event_ctx = await self.ap.plugin_mgr.emit_event(
|
||||||
event=events.NormalMessageResponded(
|
event=events.NormalMessageResponded(
|
||||||
launcher_type=query.launcher_type.value,
|
launcher_type=query.launcher_type.value,
|
||||||
@@ -119,7 +88,6 @@ class ResponseWrapper(stage.PipelineStage):
|
|||||||
query=query
|
query=query
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if event_ctx.is_prevented_default():
|
if event_ctx.is_prevented_default():
|
||||||
yield entities.StageProcessResult(
|
yield entities.StageProcessResult(
|
||||||
result_type=entities.ResultType.INTERRUPT,
|
result_type=entities.ResultType.INTERRUPT,
|
||||||
@@ -132,9 +100,52 @@ class ResponseWrapper(stage.PipelineStage):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
query.resp_message_chain.append(mirai.MessageChain([mirai.Plain(reply_text)]))
|
query.resp_message_chain.append(result.get_content_mirai_message_chain())
|
||||||
|
|
||||||
yield entities.StageProcessResult(
|
yield entities.StageProcessResult(
|
||||||
result_type=entities.ResultType.CONTINUE,
|
result_type=entities.ResultType.CONTINUE,
|
||||||
new_query=query
|
new_query=query
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if result.tool_calls is not None: # 有函数调用
|
||||||
|
|
||||||
|
function_names = [tc.function.name for tc in result.tool_calls]
|
||||||
|
|
||||||
|
reply_text = f'调用函数 {".".join(function_names)}...'
|
||||||
|
|
||||||
|
query.resp_message_chain.append(mirai.MessageChain([mirai.Plain(reply_text)]))
|
||||||
|
|
||||||
|
if self.ap.platform_cfg.data['track-function-calls']:
|
||||||
|
|
||||||
|
event_ctx = await self.ap.plugin_mgr.emit_event(
|
||||||
|
event=events.NormalMessageResponded(
|
||||||
|
launcher_type=query.launcher_type.value,
|
||||||
|
launcher_id=query.launcher_id,
|
||||||
|
sender_id=query.sender_id,
|
||||||
|
session=session,
|
||||||
|
prefix='',
|
||||||
|
response_text=reply_text,
|
||||||
|
finish_reason='stop',
|
||||||
|
funcs_called=[fc.function.name for fc in result.tool_calls] if result.tool_calls is not None else [],
|
||||||
|
query=query
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
if event_ctx.is_prevented_default():
|
||||||
|
yield entities.StageProcessResult(
|
||||||
|
result_type=entities.ResultType.INTERRUPT,
|
||||||
|
new_query=query
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if event_ctx.event.reply is not None:
|
||||||
|
|
||||||
|
query.resp_message_chain.append(mirai.MessageChain(event_ctx.event.reply))
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
query.resp_message_chain.append(mirai.MessageChain([mirai.Plain(reply_text)]))
|
||||||
|
|
||||||
|
yield entities.StageProcessResult(
|
||||||
|
result_type=entities.ResultType.CONTINUE,
|
||||||
|
new_query=query
|
||||||
|
)
|
||||||
|
|||||||
@@ -31,13 +31,15 @@ class AiocqhttpMessageConverter(adapter.MessageConverter):
|
|||||||
msg_time = msg.time
|
msg_time = msg.time
|
||||||
elif type(msg) is mirai.Image:
|
elif type(msg) is mirai.Image:
|
||||||
arg = ''
|
arg = ''
|
||||||
|
if msg.base64:
|
||||||
if msg.url:
|
arg = msg.base64
|
||||||
|
msg_list.append(aiocqhttp.MessageSegment.image(f"base64://{arg}"))
|
||||||
|
elif msg.url:
|
||||||
arg = msg.url
|
arg = msg.url
|
||||||
|
msg_list.append(aiocqhttp.MessageSegment.image(arg))
|
||||||
elif msg.path:
|
elif msg.path:
|
||||||
arg = msg.path
|
arg = msg.path
|
||||||
|
msg_list.append(aiocqhttp.MessageSegment.image(arg))
|
||||||
msg_list.append(aiocqhttp.MessageSegment.image(arg))
|
|
||||||
elif type(msg) is mirai.At:
|
elif type(msg) is mirai.At:
|
||||||
msg_list.append(aiocqhttp.MessageSegment.at(msg.target))
|
msg_list.append(aiocqhttp.MessageSegment.at(msg.target))
|
||||||
elif type(msg) is mirai.AtAll:
|
elif type(msg) is mirai.AtAll:
|
||||||
|
|||||||
@@ -322,7 +322,7 @@ class NakuruProjectAdapter(adapter_model.MessageSourceAdapter):
|
|||||||
proxies=None
|
proxies=None
|
||||||
)
|
)
|
||||||
if resp.status_code == 403:
|
if resp.status_code == 403:
|
||||||
raise Exception("go-cqhttp拒绝访问,请检查config.py中nakuru_config的token是否与go-cqhttp设置的access-token匹配")
|
raise Exception("go-cqhttp拒绝访问,请检查配置文件中nakuru适配器的配置")
|
||||||
self.bot_account_id = int(resp.json()['data']['user_id'])
|
self.bot_account_id = int(resp.json()['data']['user_id'])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception("获取go-cqhttp账号信息失败, 请检查是否已启动go-cqhttp并配置正确")
|
raise Exception("获取go-cqhttp账号信息失败, 请检查是否已启动go-cqhttp并配置正确")
|
||||||
|
|||||||
@@ -96,7 +96,16 @@ class Message(pydantic.BaseModel):
|
|||||||
if ce.type == 'text':
|
if ce.type == 'text':
|
||||||
mc.append(mirai.Plain(ce.text))
|
mc.append(mirai.Plain(ce.text))
|
||||||
elif ce.type == 'image':
|
elif ce.type == 'image':
|
||||||
mc.append(mirai.Image(url=ce.image_url))
|
if ce.image_url.url.startswith("http"):
|
||||||
|
mc.append(mirai.Image(url=ce.image_url.url))
|
||||||
|
else: # base64
|
||||||
|
|
||||||
|
b64_str = ce.image_url.url
|
||||||
|
|
||||||
|
if b64_str.startswith("data:"):
|
||||||
|
b64_str = b64_str.split(",")[1]
|
||||||
|
|
||||||
|
mc.append(mirai.Image(base64=b64_str))
|
||||||
|
|
||||||
# 找第一个文字组件
|
# 找第一个文字组件
|
||||||
if prefix_text:
|
if prefix_text:
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from .. import api, entities, errors
|
|||||||
from ....core import entities as core_entities
|
from ....core import entities as core_entities
|
||||||
from ... import entities as llm_entities
|
from ... import entities as llm_entities
|
||||||
from ...tools import entities as tools_entities
|
from ...tools import entities as tools_entities
|
||||||
|
from ....utils import image
|
||||||
|
|
||||||
|
|
||||||
@api.requester_class("anthropic-messages")
|
@api.requester_class("anthropic-messages")
|
||||||
@@ -54,29 +55,45 @@ class AnthropicMessages(api.LLMAPIRequester):
|
|||||||
and isinstance(system_role_message.content, str):
|
and isinstance(system_role_message.content, str):
|
||||||
args['system'] = system_role_message.content
|
args['system'] = system_role_message.content
|
||||||
|
|
||||||
# 其他消息
|
|
||||||
# req_messages = [
|
|
||||||
# m.dict(exclude_none=True) for m in messages \
|
|
||||||
# if (isinstance(m.content, str) and m.content.strip() != "") \
|
|
||||||
# or (isinstance(m.content, list) and )
|
|
||||||
# ]
|
|
||||||
# 暂时不支持vision,仅保留纯文字的content
|
|
||||||
req_messages = []
|
req_messages = []
|
||||||
|
|
||||||
for m in messages:
|
for m in messages:
|
||||||
if isinstance(m.content, str) and m.content.strip() != "":
|
if isinstance(m.content, str) and m.content.strip() != "":
|
||||||
req_messages.append(m.dict(exclude_none=True))
|
req_messages.append(m.dict(exclude_none=True))
|
||||||
elif isinstance(m.content, list):
|
elif isinstance(m.content, list):
|
||||||
# 删除m.content中的type!=text的元素
|
# m.content = [
|
||||||
m.content = [
|
# c for c in m.content if c.type == "text"
|
||||||
c for c in m.content if c.get("type") == "text"
|
# ]
|
||||||
]
|
|
||||||
|
|
||||||
if len(m.content) > 0:
|
# if len(m.content) > 0:
|
||||||
req_messages.append(m.dict(exclude_none=True))
|
# req_messages.append(m.dict(exclude_none=True))
|
||||||
|
|
||||||
|
msg_dict = m.dict(exclude_none=True)
|
||||||
|
|
||||||
|
for i, ce in enumerate(m.content):
|
||||||
|
if ce.type == "image_url":
|
||||||
|
alter_image_ele = {
|
||||||
|
"type": "image",
|
||||||
|
"source": {
|
||||||
|
"type": "base64",
|
||||||
|
"media_type": "image/jpeg",
|
||||||
|
"data": await image.qq_image_url_to_base64(ce.image_url.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
msg_dict["content"][i] = alter_image_ele
|
||||||
|
|
||||||
|
req_messages.append(msg_dict)
|
||||||
|
|
||||||
args["messages"] = req_messages
|
args["messages"] = req_messages
|
||||||
|
|
||||||
|
# anthropic的tools处在beta阶段,sdk不稳定,故暂时不支持
|
||||||
|
#
|
||||||
|
# if funcs:
|
||||||
|
# tools = await self.ap.tool_mgr.generate_tools_for_openai(funcs)
|
||||||
|
|
||||||
|
# if tools:
|
||||||
|
# args["tools"] = tools
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = await self.client.messages.create(**args)
|
resp = await self.client.messages.create(**args)
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,10 @@ from ...plugin import context as plugin_context
|
|||||||
|
|
||||||
|
|
||||||
class ToolManager:
|
class ToolManager:
|
||||||
"""LLM工具管理器
|
"""LLM工具管理器"""
|
||||||
"""
|
|
||||||
|
|
||||||
ap: app.Application
|
ap: app.Application
|
||||||
|
|
||||||
def __init__(self, ap: app.Application):
|
def __init__(self, ap: app.Application):
|
||||||
self.ap = ap
|
self.ap = ap
|
||||||
self.all_functions = []
|
self.all_functions = []
|
||||||
@@ -22,35 +21,33 @@ class ToolManager:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
async def get_function(self, name: str) -> entities.LLMFunction:
|
async def get_function(self, name: str) -> entities.LLMFunction:
|
||||||
"""获取函数
|
"""获取函数"""
|
||||||
"""
|
|
||||||
for function in await self.get_all_functions():
|
for function in await self.get_all_functions():
|
||||||
if function.name == name:
|
if function.name == name:
|
||||||
return function
|
return function
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def get_function_and_plugin(self, name: str) -> typing.Tuple[entities.LLMFunction, plugin_context.BasePlugin]:
|
async def get_function_and_plugin(
|
||||||
"""获取函数和插件
|
self, name: str
|
||||||
"""
|
) -> typing.Tuple[entities.LLMFunction, plugin_context.BasePlugin]:
|
||||||
|
"""获取函数和插件"""
|
||||||
for plugin in self.ap.plugin_mgr.plugins:
|
for plugin in self.ap.plugin_mgr.plugins:
|
||||||
for function in plugin.content_functions:
|
for function in plugin.content_functions:
|
||||||
if function.name == name:
|
if function.name == name:
|
||||||
return function, plugin.plugin_inst
|
return function, plugin.plugin_inst
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
async def get_all_functions(self) -> list[entities.LLMFunction]:
|
async def get_all_functions(self) -> list[entities.LLMFunction]:
|
||||||
"""获取所有函数
|
"""获取所有函数"""
|
||||||
"""
|
|
||||||
all_functions: list[entities.LLMFunction] = []
|
all_functions: list[entities.LLMFunction] = []
|
||||||
|
|
||||||
for plugin in self.ap.plugin_mgr.plugins:
|
for plugin in self.ap.plugin_mgr.plugins:
|
||||||
all_functions.extend(plugin.content_functions)
|
all_functions.extend(plugin.content_functions)
|
||||||
|
|
||||||
return all_functions
|
return all_functions
|
||||||
|
|
||||||
async def generate_tools_for_openai(self, use_funcs: entities.LLMFunction) -> str:
|
async def generate_tools_for_openai(self, use_funcs: list[entities.LLMFunction]) -> list:
|
||||||
"""生成函数列表
|
"""生成函数列表"""
|
||||||
"""
|
|
||||||
tools = []
|
tools = []
|
||||||
|
|
||||||
for function in use_funcs:
|
for function in use_funcs:
|
||||||
@@ -60,40 +57,71 @@ class ToolManager:
|
|||||||
"function": {
|
"function": {
|
||||||
"name": function.name,
|
"name": function.name,
|
||||||
"description": function.description,
|
"description": function.description,
|
||||||
"parameters": function.parameters
|
"parameters": function.parameters,
|
||||||
}
|
},
|
||||||
|
}
|
||||||
|
tools.append(function_schema)
|
||||||
|
|
||||||
|
return tools
|
||||||
|
|
||||||
|
async def generate_tools_for_anthropic(
|
||||||
|
self, use_funcs: list[entities.LLMFunction]
|
||||||
|
) -> list:
|
||||||
|
"""为anthropic生成函数列表
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "get_stock_price",
|
||||||
|
"description": "Get the current stock price for a given ticker symbol.",
|
||||||
|
"input_schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"ticker": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["ticker"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"""
|
||||||
|
|
||||||
|
tools = []
|
||||||
|
|
||||||
|
for function in use_funcs:
|
||||||
|
if function.enable:
|
||||||
|
function_schema = {
|
||||||
|
"name": function.name,
|
||||||
|
"description": function.description,
|
||||||
|
"input_schema": function.parameters,
|
||||||
}
|
}
|
||||||
tools.append(function_schema)
|
tools.append(function_schema)
|
||||||
|
|
||||||
return tools
|
return tools
|
||||||
|
|
||||||
async def execute_func_call(
|
async def execute_func_call(
|
||||||
self,
|
self, query: core_entities.Query, name: str, parameters: dict
|
||||||
query: core_entities.Query,
|
|
||||||
name: str,
|
|
||||||
parameters: dict
|
|
||||||
) -> typing.Any:
|
) -> typing.Any:
|
||||||
"""执行函数调用
|
"""执行函数调用"""
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
function, plugin = await self.get_function_and_plugin(name)
|
function, plugin = await self.get_function_and_plugin(name)
|
||||||
if function is None:
|
if function is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
parameters = parameters.copy()
|
parameters = parameters.copy()
|
||||||
|
|
||||||
parameters = {
|
parameters = {"query": query, **parameters}
|
||||||
"query": query,
|
|
||||||
**parameters
|
|
||||||
}
|
|
||||||
|
|
||||||
return await function.func(plugin, **parameters)
|
return await function.func(plugin, **parameters)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.ap.logger.error(f'执行函数 {name} 时发生错误: {e}')
|
self.ap.logger.error(f"执行函数 {name} 时发生错误: {e}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return f'error occurred when executing function {name}: {e}'
|
return f"error occurred when executing function {name}: {e}"
|
||||||
finally:
|
finally:
|
||||||
|
|
||||||
plugin = None
|
plugin = None
|
||||||
@@ -107,11 +135,11 @@ class ToolManager:
|
|||||||
|
|
||||||
await self.ap.ctr_mgr.usage.post_function_record(
|
await self.ap.ctr_mgr.usage.post_function_record(
|
||||||
plugin={
|
plugin={
|
||||||
'name': plugin.plugin_name,
|
"name": plugin.plugin_name,
|
||||||
'remote': plugin.plugin_source,
|
"remote": plugin.plugin_source,
|
||||||
'version': plugin.plugin_version,
|
"version": plugin.plugin_version,
|
||||||
'author': plugin.plugin_author
|
"author": plugin.plugin_author,
|
||||||
},
|
},
|
||||||
function_name=function.name,
|
function_name=function.name,
|
||||||
function_description=function.description,
|
function_description=function.description,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
semantic_version = "v3.2.0"
|
semantic_version = "v3.2.1"
|
||||||
|
|||||||
@@ -83,32 +83,38 @@
|
|||||||
{
|
{
|
||||||
"name": "claude-3-opus-20240229",
|
"name": "claude-3-opus-20240229",
|
||||||
"requester": "anthropic-messages",
|
"requester": "anthropic-messages",
|
||||||
"token_mgr": "anthropic"
|
"token_mgr": "anthropic",
|
||||||
|
"vision_supported": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "claude-3-sonnet-20240229",
|
"name": "claude-3-sonnet-20240229",
|
||||||
"requester": "anthropic-messages",
|
"requester": "anthropic-messages",
|
||||||
"token_mgr": "anthropic"
|
"token_mgr": "anthropic",
|
||||||
|
"vision_supported": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "claude-3-haiku-20240307",
|
"name": "claude-3-haiku-20240307",
|
||||||
"requester": "anthropic-messages",
|
"requester": "anthropic-messages",
|
||||||
"token_mgr": "anthropic"
|
"token_mgr": "anthropic",
|
||||||
|
"vision_supported": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "moonshot-v1-8k",
|
"name": "moonshot-v1-8k",
|
||||||
"requester": "moonshot-chat-completions",
|
"requester": "moonshot-chat-completions",
|
||||||
"token_mgr": "moonshot"
|
"token_mgr": "moonshot",
|
||||||
|
"tool_call_supported": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "moonshot-v1-32k",
|
"name": "moonshot-v1-32k",
|
||||||
"requester": "moonshot-chat-completions",
|
"requester": "moonshot-chat-completions",
|
||||||
"token_mgr": "moonshot"
|
"token_mgr": "moonshot",
|
||||||
|
"tool_call_supported": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "moonshot-v1-128k",
|
"name": "moonshot-v1-128k",
|
||||||
"requester": "moonshot-chat-completions",
|
"requester": "moonshot-chat-completions",
|
||||||
"token_mgr": "moonshot"
|
"token_mgr": "moonshot",
|
||||||
|
"tool_call_supported": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "deepseek-chat",
|
"name": "deepseek-chat",
|
||||||
|
|||||||
@@ -10,5 +10,6 @@
|
|||||||
"default": 1
|
"default": 1
|
||||||
},
|
},
|
||||||
"pipeline-concurrency": 20,
|
"pipeline-concurrency": 20,
|
||||||
|
"qcg-center-url": "https://api.qchatgpt.rockchin.top/api/v2",
|
||||||
"help-message": "QChatGPT - 😎高稳定性、🧩支持插件、🌏实时联网的 ChatGPT QQ 机器人🤖\n链接:https://q.rkcn.top"
|
"help-message": "QChatGPT - 😎高稳定性、🧩支持插件、🌏实时联网的 ChatGPT QQ 机器人🤖\n链接:https://q.rkcn.top"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user