- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
大语言模型的应用之一是与大语言模型进行聊天也就是一个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的效果还不是很好,任务变复杂有可能会出错,具体学习可以看推荐的项目的源代码,作者写的还是比较清晰的。感谢硅基流动提供的平台,让我等没有硬件资源的人,也可以流畅的使用开源的大语言模型,进行大语言模型的应用探索.
最后此篇关于大语言模型的应用探索—AIAgent初探!的文章就讲到这里了,如果你想了解更多关于大语言模型的应用探索—AIAgent初探!的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
可不可以命名为MVVM模型?因为View通过查看模型数据。 View 是否应该只与 ViewModelData 交互?我确实在某处读到正确的 MVVM 模型应该在 ViewModel 而不是 Mode
我正在阅读有关设计模式的文章,虽然作者们都认为观察者模式很酷,但在设计方面,每个人都在谈论 MVC。 我有点困惑,MVC 图不是循环的,代码流具有闭合拓扑不是很自然吗?为什么没有人谈论这种模式: mo
我正在开发一个 Sticky Notes 项目并在 WPF 中做 UI,显然将 MVVM 作为我的架构设计选择。我正在重新考虑我的模型、 View 和 View 模型应该是什么。 我有一个名为 Not
不要混淆:How can I convert List to Hashtable in C#? 我有一个模型列表,我想将它们组织成一个哈希表,以枚举作为键,模型列表(具有枚举的值)作为值。 publi
我只是花了一些时间阅读这些术语(我不经常使用它们,因为我们没有任何 MVC 应用程序,我通常只说“模型”),但我觉得根据上下文,这些意味着不同的东西: 实体 这很简单,它是数据库中的一行: 2) In
我想知道你们中是否有人知道一些很好的教程来解释大型应用程序的 MVVM。我发现关于 MVVM 的每个教程都只是基础知识解释(如何实现模型、 View 模型和 View ),但我对在应用程序页面之间传递
我想realm.delete() 我的 Realm 中除了一个模型之外的所有模型。有什么办法可以不列出所有这些吗? 也许是一种遍历 Realm 中当前存在的所有类型的方法? 最佳答案 您可以从您的 R
我正在尝试使用 alias 指令模拟一个 Eloquent 模型,如下所示: $transporter = \Mockery::mock('alias:' . Transporter::class)
我正在使用 stargazer 创建我的 plm 汇总表。 library(plm) library(pglm) data("Unions", package = "pglm") anb1 <- pl
我读了几篇与 ASP.NET 分层架构相关的文章和问题,但是读得太多后我有点困惑。 UI 层是在 ASP.NET MVC 中开发的,对于数据访问,我在项目中使用 EF。 我想通过一个例子来描述我的问题
我收到此消息错误: Inceptionv3.mlmodel: unable to read document 我下载了最新版本的 xcode。 9.4 版测试版 (9Q1004a) 最佳答案 您没有
(同样,一个 MVC 验证问题。我知道,我知道......) 我想使用 AutoMapper ( http://automapper.codeplex.com/ ) 来验证我的创建 View 中不在我
需要澄清一件事,现在我正在处理一个流程,其中我有两个 View 模型,一个依赖于另一个 View 模型,为了处理这件事,我尝试在我的基本 Activity 中注入(inject)两个 View 模型,
如果 WPF MVVM 应该没有代码,为什么在使用 ICommand 时,是否需要在 Window.xaml.cs 代码中实例化 DataContext 属性?我已经并排观看并关注了 YouTube
当我第一次听说 ASP.NET MVC 时,我认为这意味着应用程序由三个部分组成:模型、 View 和 Controller 。 然后我读到 NerdDinner并学习了存储库和 View 模型的方法
Platform : ubuntu 16.04 Python version: 3.5.2 mmdnn version : 0.2.5 Source framework with version :
我正在学习本教程:https://www.raywenderlich.com/160728/object-oriented-programming-swift ...并尝试对代码进行一些个人调整,看看
我正试图围绕 AngularJS。我很喜欢它,但一个核心概念似乎在逃避我——模型在哪里? 例如,如果我有一个显示多个交易列表的应用程序。一个列表向服务器查询匹配某些条件的分页事务集,另一个列表使用不同
我在为某个应用程序找出最佳方法时遇到了麻烦。我不太习惯取代旧 TLA(三层架构)的新架构,所以这就是我的来源。 在为我的应用程序(POCO 类,对吧??)设计模型和 DAL 时,我有以下疑问: 我的模
我有两个模型:Person 和 Department。每个人可以在一个部门工作。部门可以由多人管理。我不确定如何在 Django 模型中构建这种关系。 这是我不成功的尝试之一 [models.py]:
我是一名优秀的程序员,十分优秀!