【AI大模型】大语言模型的应用探索—AI Agent初探!

前言

大语言模型的应用之一是与大语言模型进行聊天也就是一个ChatBot,这个应用已经很广泛了。

接下来的一个应用就是AI Agent。

AI Agent是人工智能代理(Artificial Intelligence Agent)的概念,它是一种能够感知环境、进行决策和执行动作的智能实体,通常基于机器学习和人工智能技术,具备自主性和自适应性,在特定任务或领域中能够自主地进行学习和改进。一个更完整的Agent,一定是与环境充分交互的,它包括两部分——一是Agent的部分,二是环境的部分。此刻的Agent就如同物理世界中的「人类」,物理世界就是人类的「外部环境」。

在这里插入图片描述

效果

今天就基于开源的大语言模型Qwen2-7B-Instruct与开源的LLM应用框架SenmanticKernel实现我们的第一个AI Agent!

入门先从一个简单的例子入手,比如叫大语言模型将字符串打印至控制台。

在ChatBox应用中,我们叫大语言模型将字符串打印至控制台,它的回答可能是这样子的:

在这里插入图片描述

而在简易的AI Agent应用中,大语言模型会帮我们完成这项简单的任务。

在这里插入图片描述
在这里插入图片描述

又比如,我们需要从数据库中检索信息,假设需要检索的信息如下所示:

 List<Order> Orders = new List<Order>()
 {
     new Order(){Id=1,Name="iPhone15",Address="武汉"},
     new Order(){Id=2,Name="iPad",Address="北京"},
     new Order(){Id=3,Name="MacBook",Address="上海"},
     new Order(){Id=4,Name = "HuaWei Mate60 ",Address = "深圳"},
     new Order(){Id = 5,Name = "小米14",Address = "广州"}
 };

在ChatBox应用中,我们如果问Id为1的订单信息是什么?大语言模型是不会知道我们想干什么的,回答可能如下所示:

在这里插入图片描述

而在简易的AI Agent应用中,AI回答如下:

在这里插入图片描述
在这里插入图片描述

实践

上一篇文章讲过,在SemanticKernel中OpenAI支持Function Call的模型与月之暗面支持Function Call的模型,只需进行简单的设置即可实现自动函数调用,但我尝试了其他开源的模型,发现做不到。

通过github了解到,其他的模型可以通过提示工程来实现本地函数调用。

什么是提示工程?

提示工程(Prompt Engineering)是一种自然语言处理(NLP)技术,主要应用于生成式AI模型,如GPT-3等。它通过精心设计输入提示(prompt),引导模型生成特定类型的输出。在提示工程中,用户可以控制模型的输出内容、风格和格式,以满足不同的应用场景需求。

提示工程的关键在于设计有效的提示,这通常需要对模型的能力和限制有深入的了解。通过调整提示的结构、语言和上下文,可以显著提高模型生成结果的质量和相关性。在实际应用中,提示工程可以用于文本生成、问答、翻译、摘要、对话系统等多个领域。

上面两个简单的AI Agent应用实现的原理是一样的,选择第二个获取订单的引用进行讲解。

实现的方法来自上一篇博客提到的项目:

Jenscaasen/UniversalLLMFunctionCaller: A planner that integrates into Semantic Kernel to enable function calling on all Chat based LLMs (Mistral, Bard, Claude, LLama etc) (github.com)

在kernel中导入插件:

public sealed class OrderPlugin
{
    List<Order> Orders = new List<Order>()
    {
        new Order(){Id=1,Name="iPhone15",Address="武汉"},
        new Order(){Id=2,Name="iPad",Address="北京"},
        new Order(){Id=3,Name="MacBook",Address="上海"},
        new Order(){Id=4,Name = "HuaWei Mate60 ",Address = "深圳"},
        new Order(){Id = 5,Name = "小米14",Address = "广州"}
    };

    [KernelFunction, Description("根据Id获取订单")]
    [return: Description("获取到的订单")]
    public string GetOrderById(
    [Description("订单的Id")] int id)
    {
        var order = Orders.Where(x => x.Id == id).FirstOrDefault();
        if(order != null)
        {
            return order.ToString();
        }
        else
        {
            return "找不到该Id的订单";
        }
    }
}
_kernel.ImportPluginFromType<OrderPlugin>("Order");
 UniversalLLMFunctionCaller planner = new(_kernel);
 string result = await planner.RunAsync(AskText);

重点在planner.RunAsync中。

导入为了实现目的内置的插件:

 // Initialize plugins
 var plugins = _kernel.Plugins;
 var internalPlugin = _kernel.Plugins.AddFromType<UniversalLLMFunctionCallerInternalFunctions>();

UniversalLLMFunctionCallerInternalFunctions插件如下:

    internal class UniversalLLMFunctionCallerInternalFunctions
    {
        //   [KernelFunction, Description("Call this when the workflow is done and there are no more functions to call")]
        //   public string Finished(
        //  [Description("Wrap up what was done and what the result is, be concise")] string finalmessage
        //)
        //   {
        //       return string.Empty;
        //       //no actual implementation, for internal routing only
        //   }
        [KernelFunction, Description("当工作流程完成,没有更多的函数需要调用时,调用这个函数")]
        public string Finished(
       [Description("总结已完成的工作和结果,尽量简洁明了。")] string finalmessage
     )
        {
            return string.Empty;
            //no actual implementation, for internal routing only
        }
        //[KernelFunction, Description("Gets the name of the spaceship of the user")]
        //public string GetMySpaceshipName()
        //{
        //    return "MSS3000";
        //}
        [KernelFunction, Description("获取用户飞船的名称")]
        public string GetMySpaceshipName()
        {
            return "嫦娥一号";
        }
     //   [KernelFunction, Description("Starts a Spaceship")]
     //   public void StartSpaceship(
     //  [Description("The name of the spaceship to start")] string ship_name
     //)
     //   {
     //       //no actual implementation, for internal routing only
     //   }

        [KernelFunction, Description("启动飞船")]
        public void StartSpaceship(
     [Description("启动的飞船的名字")] string ship_name
   )
        {
            //no actual implementation, for internal routing only
        }

    }
}

我将英文原版注释掉并增加了一个中文的版本。

将插件转化为文本:

// Convert plugins to text
string pluginsAsText = GetTemplatesAsTextPrompt3000(plugins);

在这里插入图片描述

获取到了插件中所有本地函数的信息。

nextFunctionCall = await GetNextFunctionCallAsync(chatHistory, pluginsAsText);

让大语言模型获取下一次需要调用的函数。

在对话示例中加入一个提示,这个提示是关键!
在这里插入图片描述

英文原版如下:

        private string GetLoopSystemMessage(string pluginsAsTextPrompt3000)
        {
            string systemPrompt = $@"You are a computer system. You can only speak TextPrompt3000 to make the user call functions, and the user will behave
        as a different computer system that answers those functions.
        Below, you are provided a goal that needs to be reached, as well as a list of functions that the user could use.
        You need to find out what the next step for the user is to reach the goal and recommend a TextPrompt3000 function call. 
        You are also provided a list of functions that are in TextPrompt3000 Schema Format.
        The TextPrompt3000 Format is defined like this:
        {GetTextPrompt300Explanation()}
        ##available functions##
        {pluginsAsTextPrompt3000}
        ##end functions##

        The following rules are very important:
        1) you can only recommend one function and the parameters, not multiple functions
        2) You can only recommend a function that is in the list of available functions
        3) You need to give all parameters for the function. Do NOT escape special characters in the name of functions or the names of parameters (dont do aaa_bbb, just stick to aaa_bbb)!
        4) Given the history, the function you recommend needs to be important to get closer towards the goal
        5) Do not wrap functions into each other. Stick to the list of functions, this is not a math problem. Do not use placeholders.
        We only need one function, the next one needed. For example, if function A() needs to be used as parameter in function B(), do NOT do B(A()). Instead,
        if A wasnt called allready, call A() first. The result will be used in B in a later iteration.
        6) Do not recommend a function that was recently called. Use the output instead. Do not use Placeholders or Functions as parameters for other functions
        7) Only write a Function Call, do not explain why, do not provide a reasoning. You are limited to writing a function call only!
        8) When all  necessary functions are called and the result was presented by the computer system, call the Finished function and present the result

        If you break any of those rules, a kitten dies. 
        ";
            return systemPrompt;
        }

我翻译了一个中文版本并添加了使用中文回答如下:

        private string GetLoopSystemMessage(string pluginsAsTextPrompt3000)
        {
            string systemPrompt = $@"你是一个计算机系统。
你只能使用TextPrompt3000指令,让用户调用对应的函数,而用户将作为另一个回答这些函数的计算机系统。
以下是您所需实现的目标,以及用户可以使用的函数列表。
您需要找出用户到达目标的下一步,并推荐一个TextPrompt3000函数调用。 
您还会得到一个TextPrompt3000 Schema格式的函数列表。
TextPrompt3000格式的定义如下所示:
{GetTextPrompt300Explanation()}
##可用函数列表开始##
{pluginsAsTextPrompt3000}
##可用函数列表结束##

以下规则非常重要:
1) 你只能推荐一个函数及其参数,而不是多个函数
2) 你可以推荐的函数只存在于可用函数列表中
3) 你需要为该函数提供所有参数。不要在函数名或参数名中转义特殊字符,直接使用(如只写aaa_bbb,不要写成aaa_bbb)
4) 你推荐的历史记录与函数需要对更接近目标有重要作用
5) 不要将函数相互嵌套。 遵循列表中的函数,这不是一个数学问题。 不要使用占位符。
我们只需要一个函数,下一个所需的函数。举个例子, 如果 function A() 需要在 function B()中当参数使用, 不要使用 B(A())。 而是,
如果A还没有被调用, 先调用 A()。返回的结果将在下一次迭代中在B中使用。
6) 不要推荐一个最近已经调用过的函数。 使用输出代替。 不要将占位符或函数作为其他函数的参数使用。
7) 只写出一个函数调用,不解释原因,不提供理由。您只能写出一个函数调用!
8) 当所有必需的函数都被调用,且计算机系统呈现了结果,调用Finished函数并展示结果。
9) 请使用中文回答。

如果你违反了任何这些规定,那么会有一只小猫死去。
";
            return systemPrompt;
        }

第一次直观感受到了提示工程的魔法。

根据这个模板与对话历史询问大语言模型下一步需要执行的函数名称与参数是什么:

在这里插入图片描述

大语言模型回答需要调用的函数名为GetOrderById,参数id为3,接下来验证是否可以转化为一个Function Call:

在这里插入图片描述

在plugins中查找是否有同名的函数,如果有KernelArguments,进行本地函数调用:

private async Task<string> InvokePluginAsync(FunctionCall functionCall)
{
    List<string> args = new List<string>();
    foreach (var paraam in functionCall.Parameters)
    {
        args.Add($"{paraam.Name} : {paraam.Value}");
    }
    Debug.WriteLine($">>invoking {functionCall.Name} with parameters {string.Join(",", args)}");
    // Iterate over each plugin in the kernel
    foreach (var plugin in _kernel.Plugins)
    {
        // Check if the plugin has a function with the same name as the function call
        var function = plugin.FirstOrDefault(f => f.Name == functionCall.Name);
        if (function != null)
        {
            // Create a new context for the function call
            KernelArguments context = new KernelArguments();

            // Add the function parameters to the context
            foreach (var parameter in functionCall.Parameters)
            {
                context[parameter.Name] = parameter.Value;
            }

            // Invoke the function
            var result = await function.InvokeAsync(_kernel, context);

            Debug.WriteLine($">>Result: {result.ToString()}");
            return result.ToString();
        }
    }
 // Invoke the function
            var result = await function.InvokeAsync(_kernel, context);

在本例中会执行:

[KernelFunction, Description("根据Id获取订单")]
[return: Description("获取到的订单")]
public string GetOrderById(
[Description("订单的Id")] int id)
{
    var order = Orders.Where(x => x.Id == id).FirstOrDefault();
    if(order != null)
    {
        return order.ToString();
    }
    else
    {
        return "找不到该Id的订单";
    }
}

这个函数,得到如下结果:

在这里插入图片描述

大语言模型判断已经完成了任务,下一步执行

   [KernelFunction, Description("当工作流程完成,没有更多的函数需要调用时,调用这个函数")]
   public string Finished(
  [Description("总结已完成的工作和结果,尽量简洁明了。")] string finalmessage
)
   {
       return string.Empty;
       //no actual implementation, for internal routing only
   }

这个函数,如下所示:

在这里插入图片描述

下一个调用的函数是Finished的,会跳出循环:

在这里插入图片描述

返回最后的信息:

在这里插入图片描述

最终的效果如下所示:

在这里插入图片描述
以上就是本次分享的全部内容,尝试使用开源的大语言模型与SenmanticKernel框架结合,构建自己的简易的AI Agent,不过AI Agent的效果还不是很好,任务变复杂有可能会出错,具体学习可以看推荐的项目的源代码,作者写的还是比较清晰的。感谢硅基流动提供的平台,让我等没有硬件资源的人,也可以流畅的使用开源的大语言模型,进行大语言模型的应用探索。

如何系统的去学习AI大模型LLM ?

作为一名热心肠的互联网老兵,我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。

但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的 AI大模型资料 包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来

所有资料 ⚡️ ,朋友们如果有需要全套 《LLM大模型入门+进阶学习资源包》,扫码获取~

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

在这里插入图片描述

四、AI大模型商业化落地方案

img

阶段1:AI大模型时代的基础理解

  • 目标:了解AI大模型的基本概念、发展历程和核心原理。
  • 内容
    • L1.1 人工智能简述与大模型起源
    • L1.2 大模型与通用人工智能
    • L1.3 GPT模型的发展历程
    • L1.4 模型工程
      - L1.4.1 知识大模型
      - L1.4.2 生产大模型
      - L1.4.3 模型工程方法论
      - L1.4.4 模型工程实践
    • L1.5 GPT应用案例

阶段2:AI大模型API应用开发工程

  • 目标:掌握AI大模型API的使用和开发,以及相关的编程技能。
  • 内容
    • L2.1 API接口
      - L2.1.1 OpenAI API接口
      - L2.1.2 Python接口接入
      - L2.1.3 BOT工具类框架
      - L2.1.4 代码示例
    • L2.2 Prompt框架
      - L2.2.1 什么是Prompt
      - L2.2.2 Prompt框架应用现状
      - L2.2.3 基于GPTAS的Prompt框架
      - L2.2.4 Prompt框架与Thought
      - L2.2.5 Prompt框架与提示词
    • L2.3 流水线工程
      - L2.3.1 流水线工程的概念
      - L2.3.2 流水线工程的优点
      - L2.3.3 流水线工程的应用
    • L2.4 总结与展望

阶段3:AI大模型应用架构实践

  • 目标:深入理解AI大模型的应用架构,并能够进行私有化部署。
  • 内容
    • L3.1 Agent模型框架
      - L3.1.1 Agent模型框架的设计理念
      - L3.1.2 Agent模型框架的核心组件
      - L3.1.3 Agent模型框架的实现细节
    • L3.2 MetaGPT
      - L3.2.1 MetaGPT的基本概念
      - L3.2.2 MetaGPT的工作原理
      - L3.2.3 MetaGPT的应用场景
    • L3.3 ChatGLM
      - L3.3.1 ChatGLM的特点
      - L3.3.2 ChatGLM的开发环境
      - L3.3.3 ChatGLM的使用示例
    • L3.4 LLAMA
      - L3.4.1 LLAMA的特点
      - L3.4.2 LLAMA的开发环境
      - L3.4.3 LLAMA的使用示例
    • L3.5 其他大模型介绍

阶段4:AI大模型私有化部署

  • 目标:掌握多种AI大模型的私有化部署,包括多模态和特定领域模型。
  • 内容
    • L4.1 模型私有化部署概述
    • L4.2 模型私有化部署的关键技术
    • L4.3 模型私有化部署的实施步骤
    • L4.4 模型私有化部署的应用场景

学习计划:

  • 阶段1:1-2个月,建立AI大模型的基础知识体系。
  • 阶段2:2-3个月,专注于API应用开发能力的提升。
  • 阶段3:3-4个月,深入实践AI大模型的应用架构和私有化部署。
  • 阶段4:4-5个月,专注于高级模型的应用和部署。
这份完整版的所有 ⚡️ 大模型 LLM 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

全套 《LLM大模型入门+进阶学习资源包↓↓↓ 获取~

👉CSDN大礼包🎁:全网最全《LLM大模型入门+进阶学习资源包》免费分享(安全链接,放心点击)👈

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值