公司建立网站的作用有,登录免费注册网址,网站服务器连接被重置,重庆建设工程信息网官网查询系统网址目录 一、前言二、两个核心概念2.1 Routines#xff08;1#xff09;清晰的Prompt#xff08;2#xff09;工具调用json schema自动生成#xff08;3#xff09;解析模型的toolcall指令#xff08;4#xff09;单Agent的循环决策与输出 PS.扩展阅读ps1.六自由度机器人相… 目录 一、前言二、两个核心概念2.1 Routines1清晰的Prompt2工具调用json schema自动生成3解析模型的toolcall指令4单Agent的循环决策与输出 PS.扩展阅读ps1.六自由度机器人相关文章资源ps2.四轴机器相关文章资源ps3.移动小车相关文章资源ps3.wifi小车控制相关文章资源 一、前言
现在大语言模型中的第一性原理:Scaling laws正在失效的论调四起大模型大有迎来瓶颈期的感觉。然而世界在AI领域都在较劲虚虚实实不可信其有也不可信其无。但是有个方向是一致的那就是多Agent的路线。无论是AI头部企业OpenAI、Google、Facbook、Microsoft还是业界大佬Andrew FeiFeiLi、Michael Winikoff等都对多Agent技术路线作了充分的肯定。本文是对阅读Ilan Bigio的《Orchestrating Agents: Routines and Handoffs》的回炉理解和分享其文章平实未有半点修饰基础阐述了多Agent协作的底层算法逻辑。而OpenAI推出的教育框架Swarm就是源于此Idea.
二、两个核心概念
多Agent协作Idea引入了概念 routines和handoffs通过基于这两个概念的python代码实现完成了多个智能体间的转移、协作和完整的用户交互。
2.1 Routines
这个词通过体会可以理解为简单的机械的任务列表。通过向LLM描述一些比较清晰的简单的先后任务Prompt和提供完成这些任务表所需的function或者tools实现单个Agent完成某项“技能”的能力。这里的核心要点主要有两个
1清晰的Prompt
需要向LLM提供一个较为明确没有歧义容易操作的system的Promt描述这个相当于对一个社会上的普通人雇用后对其进行业务的培训让他/她明白这个岗位的职责和操作步骤使其成为一个公司的特定岗位的业务员。
system_message (You are a customer support agent for ACME Inc.Always answer in a sentence or less.Follow the following routine with the user:1. First, ask probing questions and understand the users problem deeper.\n - unless the user has already provided a reason.\n2. Propose a fix (make one up).\n3. ONLY if not satesfied, offer a refund.\n4. If accepted, search for the ID and then execute refund.
)
2工具调用json schema自动生成
LLM现在都支持外部的tool/函数调用了而且很多都是遵循OpenAi的规范格式就是json schema格式可以认为是大模型的结构化输出通讯协议的一种。 大模型JSON Schema格式是一种用于描述和验证JSON数据结构的规范。它定义了JSON数据中各个元素的类型、格式、约束和关系确保了数据的一致性和可靠性。在软件开发、API设计以及数据交换过程中JSON Schema发挥着重要作用来自网络的定义不知道说些什么 “协议”的格式如下 {type: function,function: {name: sample_function,#工具名称description: This is my docstring. Call this function when you want.,#工具描述parameters: {#工具行参数描述type: object,properties: {param_1: {#第1个参数type: string},param_2: {#第2个参数type: string},the_third_one: {#第3个参数type: integer},some_optional: {#可选参数type: string}},required: [param_1,param_2,the_third_one] {#必须传入的参数}}
}其实就是对应的一个python的普通的funciton
def sample_function(param_1, param_2, the_third_one: int, some_optionalJohn Doe):This is my docstring. Call this function when you want.print(Hello, world)区别与需要手动定义这个JSON Schema可以用一个python函数自动生成实现JSON Schema这个也是用到了swarm框架里了
import inspect
#实现一个自动JSON Schema生成
def function_to_schema(func) - dict:type_map {str: string,int: integer,float: number,bool: boolean,list: array,dict: object,type(None): null,}try:signature inspect.signature(func)except ValueError as e:raise ValueError(fFailed to get signature for function {func.__name__}: {str(e)})parameters {}for param in signature.parameters.values():try:param_type type_map.get(param.annotation, string)except KeyError as e:raise KeyError(fUnknown type annotation {param.annotation} for parameter {param.name}: {str(e)})parameters[param.name] {type: param_type}required [param.namefor param in signature.parameters.values()if param.default inspect._empty]return {type: function,function: {name: func.__name__,description: (func.__doc__ or ).strip(),parameters: {type: object,properties: parameters,required: required,},},}以上的自动生成函数适合任何一个普通函数
def add(a:int,b:int,isaddTrue):this funciton is used to do add method when isadd is true or minuse method when isadd is false return the resultif isadd:return abelse:return a-bschema function_to_schema(add)
print(json.dumps(schema, indent2))打印结果如下 有了以上两个法宝后就可以轻松实现agent的外部函数调用了
# -*- coding: utf-8 -*-Created on Fri Nov 15 16:47:17 2024author: 18268
import inspect
import jsondef function_to_schema(func) - dict:type_map {str: string,int: integer,float: number,bool: boolean,list: array,dict: object,type(None): null,}try:signature inspect.signature(func)except ValueError as e:raise ValueError(fFailed to get signature for function {func.__name__}: {str(e)})parameters {}for param in signature.parameters.values():try:param_type type_map.get(param.annotation, string)except KeyError as e:raise KeyError(fUnknown type annotation {param.annotation} for parameter {param.name}: {str(e)})parameters[param.name] {type: param_type}required [param.namefor param in signature.parameters.values()if param.default inspect._empty]return {type: function,function: {name: func.__name__,description: (func.__doc__ or ).strip(),parameters: {type: object,properties: parameters,required: required,},},}def add(a:int,b:int,isaddTrue):this funciton is used to do add method when isadd is true or minuse method when isadd is false return the resultif isadd:return abelse:return a-bschema function_to_schema(add)
print(json.dumps(schema, indent2))from openai import OpenAI
# 定义模型
MODEL llama3.2:latest
ollama_client OpenAI(base_url http://localhost:11434/v1,api_keyNone, # required, but unused
)
messages []tools [add]
tool_schemas [function_to_schema(tool) for tool in tools]response ollama_client.chat.completions.create(modelMODEL,messages[{role: user, content: 1加1等于几}],toolstool_schemas,)
message response.choices[0].messageprint(message.tool_calls[0].function)最后模型根据用户输入1加1等于几会去查找工具的tool_schemas并自主决定了调用add这个工具输出如下 这个是openai自定义的一个type:openai.types.chat.chat_completion_message_tool_call.Function
3解析模型的toolcall指令
这个就是当模型认为要调用工具时会吐出要调用的某个函数的信息
包含一个function属性及对应名字和参数。接下来就是根据它去调用实体的函数
tools[add]
tools_map {tool.__name__: tool for tool in tools}#这里搞了一个tools_map,用于存多个funciton的名字def execute_tool_call(tool_call, tools_map):#根据openai的LLM返回格式调用相应函数name tool_call.function.nameargs json.loads(tool_call.function.arguments)print(fAssistant: {name}({args}))# call corresponding function with provided argumentsreturn tools_map[name](**args)
execute_tool_call(message.tool_calls[0], tools_map)如下调用了add函数执行并输出了结果。
4单Agent的循环决策与输出
以上实现了LLM自动调用工具库的function如果需要多个工具库的调用还需要做一个while循环首先需要将前一个工具执行输出结果输入给LLM然后再让LLM对照routines的任务表判断是否还要继续调用其它工具直到它认为可以输出结果返给user为止 def run_full_turn(system_message, tools, messages):num_init_messages len(messages)messages messages.copy()while True:# turn python functions into tools and save a reverse maptool_schemas [function_to_schema(tool) for tool in tools]tools_map {tool.__name__: tool for tool in tools}# 1. get openai completion response ollama_client.chat.completions.create(modelMODEL,#或者qwen2.5等本地模型messages[{role: system, content: system_message}] messages,toolstool_schemas or None,)message response.choices[0].messagemessages.append(message)if message.content: # print assistant responseprint(Assistant:, message.content)if not message.tool_calls: # if finished handling tool calls, breakbreak# 2. handle tool calls for tool_call in message.tool_calls:result execute_tool_call(tool_call, tools_map)result_message {role: tool,tool_call_id: tool_call.id,content: result,}print(result_message:,result_message)messages.append(result_message)# 3. return new messages return messages[num_init_messages:]
PS.扩展阅读
————————————————————————————————————————
对于python机器人编程感兴趣的小伙伴可以进入如下链接阅读相关咨询
ps1.六自由度机器人相关文章资源
(1) 对六自由度机械臂的运动控制及python实现附源码)
(2) N轴机械臂的MDH正向建模及python算法
ps2.四轴机器相关文章资源
(1) 文章python机器人编程——用python实现一个写字机器人 2python机器人实战——0到1创建一个自动是色块机器人项目-CSDN直播
(3)博文《我从0开始搭建了一个色块自动抓取机器人并实现了大模型的接入和语音控制-(上基础篇)》的vrep基础环境 (3)博文《我从0开始搭建了一个色块自动抓取机器人并实现了大模型的接入和语音控制-(上基础篇)》的vrep基础环境 (4)实现了语音输入大模型指令解析机器视觉机械臂流程打通
ps3.移动小车相关文章资源
1python做了一个极简的栅格地图行走机器人到底能干啥[第五弹]——解锁蒙特卡洛定位功能-CSDN博客 (2) 对应python资源源码地址
(3)python机器人编程——差速AGV机器、基于视觉和预测控制的循迹、自动行驶上篇_agv编程-CSDN博客 (4)python机器人编程——差速AGV机器、基于视觉和预测控制的循迹、自动行驶下篇_agv路线规划原则python-CSDN博客 对应python及仿真环境资源源码链接
ps3.wifi小车控制相关文章资源
web端配套资源源代码已经上传(竖屏版)下载地址 仿真配套资源已经上传下载地址 web端配套资源源代码已经上传(横屏版)下载地址