首页 > 其他分享 >构建插件函数

构建插件函数

时间:2024-04-25 13:14:04浏览次数:30  
标签:function 插件 函数 plugin module getattr 构建 result type

 1 def get_response_from_plugins(name_space_p, post_type_p, user_state_p, data):
 2     # 存储每个函数的结果
 3     try:
 4         message = str(data["message"])
 5     except:
 6         message = ""
 7 
 8     plugin_dir = 'plugins'
 9 
10 
11     results = []
12     # 遍历plugins目录下的所有文件
13     for filename in os.listdir(plugin_dir):
14         if filename.endswith('.py'):
15             plugin_path = os.path.join(plugin_dir, filename)
16             # 动态导入模块
17             spec = importlib.util.spec_from_file_location("plugin_module", plugin_path)
18             plugin_module = importlib.util.module_from_spec(spec)
19             spec.loader.exec_module(plugin_module)
20             
21             # 获取模块中的所有函数及其优先级
22             functions_with_priority = [(getattr(plugin_module, func), getattr(plugin_module, func)._name_space, getattr(plugin_module, func)._priority, getattr(plugin_module, func)._function_type, getattr(plugin_module, func)._post_type, getattr(plugin_module, func)._user_state, getattr(plugin_module, func)._block) for func in dir(plugin_module) if callable(getattr(plugin_module, func)) and hasattr(getattr(plugin_module, func), '_priority')]
23             
24             # 根据优先级对函数进行排序
25             functions_with_priority.sort(key=lambda x: x[1])
26             
27             result_serial = None  # 初始值设为None
28             result_parallel = ''  # 用于并行执行的结果串联
29             # 依次执行函数
30             for function, name_space, priority, function_type, post_type, user_state, block in functions_with_priority:
31                 # 判断function_type、post_type和user_state是否满足特定条件
32                 if function_type == "serial" and post_type == post_type_p and user_state == user_state_p and name_space == name_space_p:
33                     if result_serial is None:
34                         # 如果result为None,则根据函数参数类型设定初始值
35                         if 'dict' in str(function.__annotations__.values()):
36                             result_serial = {}
37                         elif 'str' in str(function.__annotations__.values()):
38                             result_serial = ''
39                         # 可以根据其他可能的参数类型继续添加条件
40                     result_serial = function(data=result_serial)  # 将data作为参数传递给函数
41                     # 如果block=True,则结束循环,不再执行后续函数
42                     if getattr(function, '_block', True):
43                         break
44                 elif function_type == "parallel" and post_type == post_type_p and user_state == user_state_p and name_space == name_space_p:
45                     result_parallel += f"{function(data)}"
46                     result_parallel += "\n"
47 
48                     # 如果block=True,则结束循环,不再执行后续函数
49                     if getattr(function, '_block', True):
50                         break
51             
52             # 将每个函数的结果存储起来
53             results.append(f"{result_parallel}" + "\n" + f"{result_serial}")
54     
55     # 将所有结果组合起来
56     result = "\n".join(results)
57 
58     # 输出结果
59     print(f"插件返回结果:{result}")
60     # 准备问题(将从插件获取的结果与当前问题拼接成上下文供LLM推理)
61     query = f"{result}" + f"{message}"
62     return query

 

标签:function,插件,函数,plugin,module,getattr,构建,result,type
From: https://www.cnblogs.com/Zhouzg-2018/p/18157426

相关文章

  • 利用 Amazon EMR Serverless、Amazon Athena、Apache Dolphinscheduler 以及本地 TiDB
    引言在数据驱动的世界中,企业正在寻求可靠且高性能的解决方案来管理其不断增长的数据需求。本系列博客从一个重视数据安全和合规性的B2C金融科技客户的角度来讨论云上云下混合部署的情况下如何利用亚马逊云科技云原生服务、开源社区产品以及第三方工具构建无服务器数据仓库的解......
  • 助力数智化转型:使用检索增强生成【RAG】构建物业行业大模型
    ​本文作者:蔡冠杰,碧桂园服务后端开发高级工程师,拥有8年开发经验。1 (RAG)检索增强生成技术介绍1.1检索增强生成是什么?举个例子,比如我作为员工,直接问大模型:提问:我们公司几点下班?大模型在没有“接触”过我们公司《员工手册》等企业私有数据的情况下,大模型往往难以给出正确......
  • 输入‘(’和‘)’判断字符串有效的函数算法
    ``//设置一个函数,通过输入键盘中的‘(’和‘)’判断字符串是否有效//顺序表中的元素数据类型是char类型typedefcharDataType_t;//创建顺序栈SequenceStack各项数据(栈底地址栈容量栈顶元素下标)的结构体typedefstructSequenceStack{DataType_t*Bottom;//记录栈......
  • dotnet 修复 GitHub Action 构建过程提示 NETSDK1127 错误
    本文告诉大家,如何修复GitHubAction构建过程提示errorNETSDK1127:ThetargetingpackMicrosoft.WindowsDesktop.App.WindowsFormsisnotinstalled.Pleaserestoreandtryagain.错误在进行GitHubAction构建时,如果自己的项目是一个旧项目,采用旧的.NETSDK版本,将可......
  • dotnet 修复多框架 TargetFrameworks 包含不受支持平台导致构建失败
    本文将告诉大家如何修复dotnet项目里的多框架TargetFrameworks如果包含了当前系统无法支持的平台时,如何进行跳过。解决在Linux平台构建时提示MacCatalyst不受支持而构建失败故事的背景是我期望在GitHub的Action里面构建一个项目,我期望能够在Windows和Linux和Ma......
  • golang工具函数,把一个金额整型,单位为分,转成"1,231,111.00"格式的字符串
    这个函数首先将整数除以100来获取代表元的浮点数,然后格式化此数值为两位小数的字符串。接下来,函数将字符串分成整数和小数部分,并且为整数部分添加千位分隔符。最后,如果存在小数部分,它会将这两部分重新组合并返回正确格式化的金额字符串。为了正确地处理负数,我们需要先检查金额是......
  • 力扣-396. 旋转函数
    1.题目介绍题目地址(396.旋转函数-力扣(LeetCode))https://leetcode.cn/problems/rotate-function/题目描述给定一个长度为n的整数数组 nums 。假设 arrk 是数组 nums 顺时针旋转k个位置后的数组,我们定义 nums 的旋转函数  F 为:F(k)=0*arrk[0]+1*......
  • C++多态与虚拟:函数重载(Function Overloading)
    重载(Overloading):所谓重载是指不同的函数实体共用一个函数名称。例如以下代码所提到的CPoint之中,有两个memberfunctions的名称同为x():1classCPoint{23public:4floatx();5voidx(floatxval);67};  其两个memberfunctions实现代码如下:1f......
  • AWS S3 Lambda Python脚本函数执行时报错AttributeError: module ‘PIL‘ has no attr
    背景代码示例如下importPILdefadd_image(self,tag,img,step):summary=Summary()bio=BytesIO()iftype(img)==str:img=PIL.Image.open(img)eliftype(img)==PIL.Image.Image:passelse:img=scipy.misc.......
  • 自定义双向循环链表基本函数接口
    自定义双向循环链表的函数接口/********************************************************************* 文件名称: 双向循环链表的函数接口* 文件作者:[email protected]* 创建日期:2024/04/24* 文件功能:对双向链表的增删改查功能的定义* 注意事项:No......