mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-20 03:16:14 +00:00
Merge pull request #787 from RockChinQ/perf/claude-ability
Perf: Claude 的能力完善支持
This commit is contained in:
@@ -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.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,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
Reference in New Issue
Block a user