探索大模型的应用与限制以及大模型与Agent的融合

随着人工智能技术的飞速发展,大模型已经成为了研究和应用的热点。然而,大模型在实际操作中存在一定局限性。通过引入Agent概念,我们或许可以突破这些限制,开启智能化应用的新纪元。

上周日我们社群举办了一场线上直播活动,其中费益州大佬详细介绍了大模型在执行任务时的内部机制及其优势。他指出,尽管大模型在语言生成等方面表现出色,但数据训练的滞后性导致了信息更新不够实时,限制了其在某些场景下的应用。

费益州进一步阐释了Agent在人工智能领域中的角色和重要性。Agent是指至少具备一项功能,在特定规则下自动运行的个体。通过结合大模型和Agent,可以弥补单一大模型在实际操作中遇到的问题。例如,Lang Chain项目通过Agent底层原理与大模型结合,在没有人为介入的情况下自动回答问题,并根据需要调用相应工具和功能。

此外,费益州还介绍了两种Agent类型:一种是逐步调用各个功能;另一种则是提前规划好整个执行链路。他强调,"这些Agent不仅增强了大模型处理问题的灵活性,还提供了更精确地定义和调用函数所需参数的可能。"

下面是关于大模型与Agent以及langchain的一些介绍:

1、大模型

  • 优点

    1. 优秀的推理能力
    2. 优秀的计算能力
    3. 优秀的自然语言理解能力
    4. 优秀的信息重点抓取能力
    5. 优秀的语言生成组织能力 ...
  • 缺点

    1. 训练数据无法实时更新(数据量巨大),导致的数据延迟
    2. 无法拥有用户权限去执行具体的操作
    3. 无法为用户定制化功能

总体来说,对于大模型本身,大模型具备很多优秀的特性,但是在应用侧落地的时候,由于各种原因,大模型无法达到定制化的功能,此时就需要借助别的方式来补齐大模型的能力短板,能够在应用侧更好的使用大模型本身的优秀特性。

2、Agent

Agent是一个在人工智能领域内早已经存在的概念,泛指具备至少一个功能,且运行在一定规则制度内的单独个体。

此处的Agent,特指具备一个或多个定制化功能的自定义组件,借助大模型的优秀能力,去完成一些大模型能力边界以外的工作或任务,即可以将Agent看作是大模型能力边界的拓展和延伸。

2.1 langchain

2.1.1定义Tool

python 复制代码
# 发送邮件tool
class SendEmailTool(BaseTool):
    name = "发送邮件"
    description = """
    如果是发送邮件,请使用此工具
    从用户输入中提取出收件人名称、邮件主题、邮件内容三个参数传递给工具:
    "receiver": 邮件接收人,联系人名单: ["mywife@163.com", "myboss@163.com"]
    "subject": 邮件主题
    "content": 邮件内容
    请提供能转换为json对象的字符串
    请把不能直接提取到的参数设置为null
    请不要制造参数
    """

    def _run(
            self,
            query: str,
            run_manager: Optional[CallbackManagerForToolRun] = None,
    ) -> str:
        return ""

      
# 天气查询tool
class WeatherTool(BaseTool):
    name = "查询天气情况"
    description = """
    如果是根据地区查询天气,请使用此工具
    从用户输入中提取出地区参数传递给工具:
    "location": 用户所在地区,默认值: 成都
    请提供能转换为json对象的字符串
    请把不能直接提取到的参数设置为默认值
    请不要制造参数
    """

    def _run(
            self,
            query: str,
            run_manager: Optional[CallbackManagerForToolRun] = None,
    ) -> str:
        weather = {
            "high_temperature": 14,
            "low_temperature": 8,
            "rainfall_probability": 0,
            "pollution_level": "middle"
        }
        return json.dumps(weather)

2.1.2 使用tool

发送邮件

python 复制代码
tool_arr = [send_email, weather]
agent = initialize_agent(
  tools=tool_arr,
  agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
  llm=llm,
  verbose=True
)
# 执行具体操作
agent('给我老板发一个邮件,告诉他我不干了')
shell 复制代码
> Entering new AgentExecutor chain...
I need to use the 发送邮件 tool to send an email to my boss with the subject and content specified in the input question.
Action: 发送邮件
Action Input: 
{
    "receiver": "myboss@163.com",
    "subject": "辞职申请",
    "content": "尊敬的老板,我想向您提出辞职申请。"
}
Observation: 
Thought:I successfully sent an email to my boss.
Final Answer: Email sent to my boss with the subject "辞职申请" and content "尊敬的老板,我想向您提出辞职申请。"

> Finished chain.

天气查询

python 复制代码
tool_arr = [search, send_email, weather]
agent = initialize_agent(
  tools=tool_arr,
  agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
  llm=llm,
  verbose=True
)
# 执行具体操作
agent('今天天气怎么样')
shell 复制代码
> Entering new AgentExecutor chain...
This question is asking for the weather, so we should use the "查询天气情况" tool.
Action: 查询天气情况
Action Input: {"location": "成都"}
Observation: {"high_temperature": 14, "low_temperature": 8, "rainfall_probability": 0, "pollution_level": "middle"}
Thought:The weather in Chengdu today has a high temperature of 14°C, a low temperature of 8°C, a 0% chance of rainfall, and a middle pollution level.
Final Answer: The weather in Chengdu today has a high temperature of 14°C, a low temperature of 8°C, a 0% chance of rainfall, and a middle pollution level.

> Finished chain.

2.2 Function Call

2.2.1定义function

发送邮件

go 复制代码
var SendEmailFunctionInfo = openai.FunctionDefinition{
	Name:        "send_email",
	Description: "send email with receiver, subject and content",
	Parameters: openai.Parameters{
		Type: "object",
		Properties: map[string]openai.Properties{
			"receiver": {
				Type:        "string",
				Description: "who can receive this email",
				Enum: []string{
					"mywife@163.com",
					"myboss@163.com",
				},
			},
			"subject": {
				Type:        "string",
				Description: "the subject of this email",
			},
			"content": {
				Type:        "string",
				Description: "the content of thie email",
			},
		},
		Required: []string{"receiver", "subject", "content"},
	},
}

天气查询

go 复制代码
var WeatherCheckFunctionInfo = openai.FunctionDefinition{
	Name:        "weather_check",
	Description: "check the weather info with user's input",
	Parameters: openai.Parameters{
		Type: "object",
		Properties: map[string]openai.Properties{
			"location": {
				Type:        "string",
				Description: "the location where user lived in, default value is 成都",
			},
		},
		Required: []string{"location"},
	},
}

2.2.2使用function

发送邮件

  1. 判断执行的函数

    json 复制代码
    // 请求体
    {
        "model": "gpt-3.5-turbo-1106",
        "messages": [
            {
                "role": "user",
                "content": "给我老板发一个邮件,告诉他我不干了"
            }
        ],
        "max_tokens": 1024,
        "n": 1,
        "stream": false,
        "functions": [
            {
                "name": "send_email",
                "description": "根据接受人、主题和内容发送邮件",
                "parameters": {
                    "type": "object",
                    "required": [
                        "topic"
                    ],
                    "properties": {
                        "receiver": {
                            "type": "string",
                            "description": "邮件联系人",
                            "enum": [
                                "mywife@163.com",
                                "myboss@163.com"
                            ]
                        },
                        "subject": {
                            "type": "string",
                            "description": "邮件主题"
                        },
                        "content": {
                            "type": "string",
                            "description": "邮件内容"
                        }
                    }
                }
            }
        ]
    }
    
    // 响应
    {
        "id": "chatcmpl-8UBDwKy6scSc4wRRA88Iy2pBXW0ap",
        "object": "chat.completion",
        "created": 1702203676,
        "model": "gpt-3.5-turbo-1106",
        "choices": [
            {
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": null,
                    "function_call": {
                        "name": "send_email",
                        "arguments": "{\"receiver\":\"myboss@163.com\",\"subject\":\"辞职通知\",\"content\":\"尊敬的老板,我想通过这封邮件向您表达我辞去目前的工作岗位的决定。我在公司工作期间收获颇丰,感谢您和公司给予我的机会和支持。希望公司事业蒸蒸日上,祝愿您工作顺利。\"}"
                    }
                },
                "finish_reason": "function_call"
            }
        ],
        "usage": {
            "prompt_tokens": 108,
            "completion_tokens": 59,
            "total_tokens": 167
        },
        "system_fingerprint": "fp_eeff13170a"
    }
  2. 获取最终结果

    json 复制代码
    # 发送请求
    {
        "model": "gpt-3.5-turbo-1106",
        "messages": [
            {
                "role": "user",
                "content": "给我老板发一个邮件,告诉他我不干了"
            },
            {
                "role": "assistant",
                "function_call": {
                    "name": "send_email",
                    "arguments": "{\"receiver\":\"myboss@163.com\",\"subject\":\"辞职通知\",\"content\":\"尊敬的老板,我想通过这封邮件向您表达我辞去目前的工作岗位的决定。我在公司工作期间收获颇丰,感谢您和公司给予我的机会和支持。希望公司事业蒸蒸日上,祝愿您工作顺利。\"}"
                }
            },
            {
                "role": "function",
                "name": "send_email",
                "content": "send success"
            }
        ],
        "max_tokens": 1024,
        "n": 1,
        "stream": false,
        "functions": [
            {
                "name": "send_email",
                "description": "send email with receiver, subject and content",
                "parameters": {
                    "type": "object",
                    "required": [
                        "topic"
                    ],
                    "properties": {
                        "receiver": {
                            "type": "string",
                            "description": "who can receive this email",
                            "enum": [
                                "mywife@163.com",
                                "myboss@163.com"
                            ]
                        },
                        "subject": {
                            "type": "string",
                            "description": "the subject of this email"
                        },
                        "content": {
                            "type": "string",
                            "description": "the content of this email"
                        }
                    }
                }
            }
        ]
    }
    
    # 获取最终结果
    {
        "id": "chatcmpl-8UBJuKSqwAbWZHWVPFHVw6mw830J9",
        "object": "chat.completion",
        "created": 1702204046,
        "model": "gpt-3.5-turbo-1106",
        "choices": [
            {
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": "邮件已成功发送给您的老板,内容是一封辞职通知。"
                },
                "finish_reason": "stop"
            }
        ],
        "usage": {
            "prompt_tokens": 244,
            "completion_tokens": 24,
            "total_tokens": 268
        },
        "system_fingerprint": "fp_eeff13170a"
    }

最后

新技术总是伴随着挑战与机遇。费益州还提到,在实施过程中存在诸如参数提取、函数定义清晰度等问题。但他同时表示信心:"随着技术不断进步和优化,我们有理由相信未来将有更多个性化、自定义化的工具出现。"

本文由mdnice多平台发布

相关推荐
我是陈泽1 小时前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
肖哥弹架构1 天前
Spring 全家桶使用教程
java·后端·程序员
IT杨秀才4 天前
自己动手写了一个协程池
后端·程序员·go
程序员麻辣烫6 天前
像AI一样思考
程序员
一颗苹果OMG7 天前
关于进游戏公司实习的第一周
前端·程序员
万少8 天前
你会了吗 HarmonyOS Next 项目级别的注释规范
前端·程序员·harmonyos
楽码8 天前
彻底理解时间?在编程中使用原子钟
后端·算法·程序员
江南一点雨9 天前
又一家培训机构即将倒闭!打工人讨薪无果,想报名的小伙伴擦亮眼睛~
java·程序员
用户86178277365189 天前
ELK 搭建 & 日志集成
java·后端·程序员
河北小田9 天前
局部变量成员变量、引用类型、this、static
java·后端·程序员