mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-09 15:26:03 +00:00
refactor: AI对话基本完成
This commit is contained in:
0
pkg/openai/tools/__init__.py
Normal file
0
pkg/openai/tools/__init__.py
Normal file
35
pkg/openai/tools/entities.py
Normal file
35
pkg/openai/tools/entities.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
import typing
|
||||
import asyncio
|
||||
|
||||
import pydantic
|
||||
|
||||
|
||||
class LLMFunction(pydantic.BaseModel):
|
||||
"""函数"""
|
||||
|
||||
name: str
|
||||
"""函数名"""
|
||||
|
||||
human_desc: str
|
||||
|
||||
description: str
|
||||
"""给LLM识别的函数描述"""
|
||||
|
||||
enable: typing.Optional[bool] = True
|
||||
|
||||
parameters: dict
|
||||
|
||||
func: typing.Callable
|
||||
"""供调用的python异步方法
|
||||
|
||||
此异步方法第一个参数接收当前请求的query对象,可以从其中取出session等信息。
|
||||
query参数不在parameters中,但在调用时会自动传入。
|
||||
但在当前版本中,插件提供的内容函数都是同步的,且均为请求无关的,故在此版本的实现(以及考虑了向后兼容性的版本)中,
|
||||
对插件的内容函数进行封装并存到这里来。
|
||||
"""
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
99
pkg/openai/tools/toolmgr.py
Normal file
99
pkg/openai/tools/toolmgr.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
|
||||
from ...core import app, entities as core_entities
|
||||
from . import entities
|
||||
from ..session import entities as session_entities
|
||||
|
||||
|
||||
class ToolManager:
|
||||
"""LLM工具管理器
|
||||
"""
|
||||
|
||||
ap: app.Application
|
||||
|
||||
all_functions: list[entities.LLMFunction]
|
||||
|
||||
def __init__(self, ap: app.Application):
|
||||
self.ap = ap
|
||||
self.all_functions = []
|
||||
|
||||
async def initialize(self):
|
||||
pass
|
||||
|
||||
def register_legacy_function(self, name: str, description: str, parameters: dict, func: callable):
|
||||
"""注册函数
|
||||
"""
|
||||
async def wrapper(query, **kwargs):
|
||||
return func(**kwargs)
|
||||
function = entities.LLMFunction(
|
||||
name=name,
|
||||
description=description,
|
||||
human_desc='',
|
||||
enable=True,
|
||||
parameters=parameters,
|
||||
func=wrapper
|
||||
)
|
||||
self.all_functions.append(function)
|
||||
|
||||
async def register_function(self, function: entities.LLMFunction):
|
||||
"""添加函数
|
||||
"""
|
||||
self.all_functions.append(function)
|
||||
|
||||
async def get_function(self, name: str) -> entities.LLMFunction:
|
||||
"""获取函数
|
||||
"""
|
||||
for function in self.all_functions:
|
||||
if function.name == name:
|
||||
return function
|
||||
return None
|
||||
|
||||
async def get_all_functions(self) -> list[entities.LLMFunction]:
|
||||
"""获取所有函数
|
||||
"""
|
||||
return self.all_functions
|
||||
|
||||
async def generate_tools_for_openai(self, conversation: session_entities.Conversation) -> str:
|
||||
"""生成函数列表
|
||||
"""
|
||||
tools = []
|
||||
|
||||
for function in conversation.use_funcs:
|
||||
if function.enable:
|
||||
function_schema = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": function.name,
|
||||
"description": function.description,
|
||||
"parameters": function.parameters
|
||||
}
|
||||
}
|
||||
tools.append(function_schema)
|
||||
|
||||
return tools
|
||||
|
||||
async def execute_func_call(
|
||||
self,
|
||||
query: core_entities.Query,
|
||||
name: str,
|
||||
parameters: dict
|
||||
) -> typing.Any:
|
||||
"""执行函数调用
|
||||
"""
|
||||
|
||||
# return "i'm not sure for the args "+str(parameters)
|
||||
|
||||
function = await self.get_function(name)
|
||||
if function is None:
|
||||
return None
|
||||
|
||||
parameters = parameters.copy()
|
||||
|
||||
parameters = {
|
||||
"query": query,
|
||||
**parameters
|
||||
}
|
||||
|
||||
return await function.func(**parameters)
|
||||
Reference in New Issue
Block a user