前言 大语言模型的应用之一是与大语言模型进行聊天也就是一个ChatBot,这个应用已经很广泛了。
接下来的一个应用就是AI Agent。
AI Agent是人工智能代理(Artificial Intelligence Agent)的概念,它是一种能够感知环境、进行决策和执行动作的智能实体,通常基于机器学习和人工智能技术,具备自主性和自适应性,在特定任务或领域中能够自主地进行学习和改进 。一个更完整的Agent,一定是与环境充分交互的,它包括两部分——一是Agent的部分,二是环境的部分 。此刻的Agent就如同物理世界中的「人类」,物理世界就是人类的「外部环境」。
效果 今天就基于开源的大语言模型Qwen2-7B-Instruct与开源的LLM应用框架SenmanticKernel实现我们的第一个AI Agent!
入门先从一个简单的例子入手,比如叫大语言模型将字符串打印至控制台。
在ChatBox应用中,我们叫大语言模型将字符串打印至控制台,它的回答可能是这样子的:
而在简易的AI Agent应用中,大语言模型会帮我们完成这项简单的任务。
又比如,我们需要从数据库中检索信息,假设需要检索的信息如下所示:
List<Order>Orders=newList<Order>() { newOrder(){Id=1,Name="iPhone15",Address="武汉"}, newOrder(){Id=2,Name="iPad",Address="北京"}, newOrder(){Id=3,Name="MacBook",Address="上海"}, newOrder(){Id=4,Name="HuaWeiMate60",Address="深圳"}, newOrder(){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中导入插件:
publicsealedclassOrderPlugin { List<Order>Orders=newList<Order>() { newOrder(){Id=1,Name="iPhone15",Address="武汉"}, newOrder(){Id=2,Name="iPad",Address="北京"}, newOrder(){Id=3,Name="MacBook",Address="上海"}, newOrder(){Id=4,Name="HuaWeiMate60",Address="深圳"}, newOrder(){Id=5,Name="小米14",Address="广州"} }; [KernelFunction,Description("根据Id获取订单")] [return escription("获取到的订单")] publicstringGetOrderById( [Description("订单的Id")]intid) { varorder=Orders.Where(x=>x.Id==id).FirstOrDefault(); if(order!=null) { returnorder.ToString(); } else { return"找不到该Id的订单"; } } }_kernel.ImportPluginFromType<OrderPlugin>("Order");UniversalLLMFunctionCallerplanner=new(_kernel); stringresult=awaitplanner.RunAsync(AskText);重点在planner.RunAsync中。
导入为了实现目的内置的插件:
//Initializeplugins varplugins=_kernel.Plugins; varinternalPlugin=_kernel.Plugins.AddFromType<UniversalLLMFunctionCallerInternalFunctions>();UniversalLLMFunctionCallerInternalFunctions插件如下:
internalclassUniversalLLMFunctionCallerInternalFunctions { //[KernelFunction,Description("Callthiswhentheworkflowisdoneandtherearenomorefunctionstocall")] //publicstringFinished( //[Description("Wrapupwhatwasdoneandwhattheresultis,beconcise")]stringfinalmessage //) //{ //returnstring.Empty; ////noactualimplementation,forinternalroutingonly //} [KernelFunction,Description("当工作流程完成,没有更多的函数需要调用时,调用这个函数")] publicstringFinished( [Description("总结已完成的工作和结果,尽量简洁明了。")]stringfinalmessage ) { returnstring.Empty; //noactualimplementation,forinternalroutingonly } //[KernelFunction,Description("Getsthenameofthespaceshipoftheuser")] //publicstringGetMySpaceshipName() //{ //return"MSS3000"; //} [KernelFunction,Description("获取用户飞船的名称")] publicstringGetMySpaceshipName() { return"嫦娥一号"; } //[KernelFunction,Description("StartsaSpaceship")] //publicvoidStartSpaceship( //[Description("Thenameofthespaceshiptostart")]stringship_name //) //{ ////noactualimplementation,forinternalroutingonly //} [KernelFunction,Description("启动飞船")] publicvoidStartSpaceship( [Description("启动的飞船的名字")]stringship_name ) { //noactualimplementation,forinternalroutingonly } } }我将英文原版注释掉并增加了一个中文的版本。
将插件转化为文本:
//Convertpluginstotext stringpluginsAsText=GetTemplatesAsTextPrompt3000(plugins);获取到了插件中所有本地函数的信息。
nextFunctionCall=awaitGetNextFunctionCallAsync(chatHistory,pluginsAsText);让大语言模型获取下一次需要调用的函数。
在对话示例中加入一个提示,这个提示是关键!
英文原版如下:
privatestringGetLoopSystemMessage(stringpluginsAsTextPrompt3000) { stringsystemPrompt=$@"Youareacomputersystem.YoucanonlyspeakTextPrompt3000tomaketheusercallfunctions,andtheuserwillbehave asadifferentcomputersystemthatanswersthosefunctions. Below,youareprovidedagoalthatneedstobereached,aswellasalistoffunctionsthattheusercoulduse. YouneedtofindoutwhatthenextstepfortheuseristoreachthegoalandrecommendaTextPrompt3000functioncall. YouarealsoprovidedalistoffunctionsthatareinTextPrompt3000SchemaFormat. TheTextPrompt3000Formatisdefinedlikethis: {GetTextPrompt300Explanation()} ##availablefunctions## {pluginsAsTextPrompt3000} ##endfunctions## Thefollowingrulesareveryimportant: 1)youcanonlyrecommendonefunctionandtheparameters,notmultiplefunctions 2)Youcanonlyrecommendafunctionthatisinthelistofavailablefunctions 3)Youneedtogiveallparametersforthefunction.DoNOTescapespecialcharactersinthenameoffunctionsorthenamesofparameters(dontdoaaa\_bbb,juststicktoaaa_bbb)! 4)Giventhehistory,thefunctionyourecommendneedstobeimportanttogetclosertowardsthegoal 5)Donotwrapfunctionsintoeachother.Sticktothelistoffunctions,thisisnotamathproblem.Donotuseplaceholders. Weonlyneedonefunction,thenextoneneeded.Forexample,iffunctionA()needstobeusedasparameterinfunctionB(),doNOTdoB(A()).Instead, ifAwasntcalledallready,callA()first.TheresultwillbeusedinBinalateriteration. 6)Donotrecommendafunctionthatwasrecentlycalled.Usetheoutputinstead.DonotusePlaceholdersorFunctionsasparametersforotherfunctions 7)OnlywriteaFunctionCall,donotexplainwhy,donotprovideareasoning.Youarelimitedtowritingafunctioncallonly! 8)Whenallnecessaryfunctionsarecalledandtheresultwaspresentedbythecomputersystem,calltheFinishedfunctionandpresenttheresult Ifyoubreakanyofthoserules,akittendies. "; returnsystemPrompt; }我翻译了一个中文版本如下:
privatestringGetLoopSystemMessage(stringpluginsAsTextPrompt3000) { stringsystemPrompt=$@"你是一个计算机系统。 你只能使用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)请使用中文回答。 如果你违反了任何这些规定,那么会有一只小猫死去。 "; returnsystemPrompt; }第一次直观感受到了提示工程的魔法。
根据这个模板与对话历史询问大语言模型下一步需要执行的函数名称与参数是什么:
大语言模型回答需要调用的函数名为GetOrderById,参数id为3,接下来验证是否可以转化为一个Function Call:
在plugins中查找是否有同名的函数,如果有KernelArguments,进行本地函数调用:
privateasyncTask<string>InvokePluginAsync(FunctionCallfunctionCall) { List<string>args=newList<string>(); foreach(varparaaminfunctionCall.Parameters) { args.Add($"{paraam.Name}:{paraam.Value}"); } Debug.WriteLine($">>invoking{functionCall.Name}withparameters{string.Join(",",args)}"); //Iterateovereachplugininthekernel foreach(varpluginin_kernel.Plugins) { //Checkifthepluginhasafunctionwiththesamenameasthefunctioncall varfunction=plugin.FirstOrDefault(f=>f.Name==functionCall.Name); if(function!=null) { //Createanewcontextforthefunctioncall KernelArgumentscontext=newKernelArguments(); //Addthefunctionparameterstothecontext foreach(varparameterinfunctionCall.Parameters) { context[parameter.Name]=parameter.Value; } //Invokethefunction varresult=awaitfunction.InvokeAsync(_kernel,context); Debug.WriteLine($">>Result:{result.ToString()}"); returnresult.ToString(); } }//Invokethefunction varresult=awaitfunction.InvokeAsync(_kernel,context);在本例中会执行:
[KernelFunction,Description("根据Id获取订单")] [return escription("获取到的订单")] publicstringGetOrderById( [Description("订单的Id")]intid) { varorder=Orders.Where(x=>x.Id==id).FirstOrDefault(); if(order!=null) { returnorder.ToString(); } else { return"找不到该Id的订单"; } }这个函数,得到如下结果:
大语言模型判断已经完成了任务,下一步执行
[KernelFunction,Description("当工作流程完成,没有更多的函数需要调用时,调用这个函数")] publicstringFinished( [Description("总结已完成的工作和结果,尽量简洁明了。")]stringfinalmessage ) { returnstring.Empty; //noactualimplementation,forinternalroutingonly }这个函数,如下所示:
下一个调用的函数是Finished的,会跳出循环:
返回最后的信息:
最终的效果如下所示:
以上就是本次分享的全部内容,尝试使用开源的大语言模型与SenmanticKernel框架结合,构建自己的简易的AI Agent,不过AI Agent的效果还不是很好,任务变复杂有可能会出错,具体学习可以看推荐的项目的源代码,作者写的还是比较清晰的。感谢硅基流动提供的平台,让我等没有硬件资源的人,也可以流畅的使用开源的大语言模型,进行大语言模型的应用探索。