AI Agent系列-什么是AI智能体,使用Semantic Kernel开发一个AI Agent
<p>今年最热的技术除了LLM大语言模型外,AI Agent智能体成为下一个最热的技术发展热点。、</p><p>近期准备整理几篇AI智能体的博客,带着大家了解并学习AI 智能体的开发和应用。</p>
<p><strong><span style="font-size: 18px">一、什么是AI 智能体</span></strong></p>
<p><strong>AI智能体</strong>(AI Agent)是指一个由人工智能驱动的系统或程序,能够在一定的环境中自主感知、决策和执行任务,同时不断学习和优化自身行为。</p>
<p>智能体的目标是完成特定任务或解决问题,模拟或增强人类的智能活动。</p>
<h3>核心特性</h3>
<ol>
<li><strong>自主性</strong>:智能体可以独立完成任务,而不需要人类的实时干预。</li>
<li><strong>感知能力</strong>:通过传感器(物理或虚拟),获取环境中的信息。</li>
<li><strong>决策能力</strong>:根据感知到的信息,分析情况并制定行动计划。</li>
<li><strong>执行能力</strong>:实施决策,改变自身或环境状态。</li>
<li><strong>学习能力</strong>:通过与环境的交互不断优化决策和行为。</li>
</ol>
<h3>组成部分</h3>
<ol>
<li><strong>感知模块</strong>:采集数据,例如通过摄像头、麦克风、API接口等获取外界信息。</li>
<li><strong>决策模块</strong>:利用算法或规则制定行动策略。</li>
<li><strong>执行模块</strong>:实施决策,例如控制机器人动作或生成文本回复。</li>
<li><strong>学习模块</strong>:通过机器学习或深度学习模型改进未来的行为。</li>
</ol>
<h3>分类</h3>
<ul>
<li><strong>物理智能体</strong>:例如机器人、无人机、自动驾驶汽车,它们存在于现实世界中,与物理环境交互。</li>
<li><strong>虚拟智能体</strong>:例如聊天机器人、智能客服、游戏中的NPC(非玩家角色),它们存在于数字或虚拟环境中。</li>
</ul>
<h3>应用领域</h3>
<ul>
<li><strong>自动驾驶</strong>:自动驾驶汽车利用智能体感知道路状况,做出驾驶决策。</li>
<li><strong>智能客服</strong>:通过自然语言处理技术与用户交流,解决问题或提供服务。</li>
<li><strong>游戏开发</strong>:NPC智能体增强了游戏的互动性和挑战性。</li>
<li><strong>金融分析</strong>:智能体可以根据市场数据提供投资建议或进行风险评估。</li>
</ul>
<h3>通俗理解</h3>
<p>AI智能体就像是一个有“眼睛”“大脑”和“手脚”的虚拟助手。它能看(感知)、想(决策)、做(执行),并且能从错误中学习,让自己变得更聪明。无论是帮助你回答问题、完成任务,还是自动驾驶和导航,它都可以独立完成,而不需要你时时刻刻指导它。</p>
<p><span style="font-size: 18px"><strong>二、使用Microsoft Semantic Kernel开发AI Agent</strong></span></p>
<p>Semantic Kernel 是一个软件开发工具包(SDK),用于将大型语言模型(LLM),如 OpenAI、Azure OpenAI 和 Hugging Face,与传统编程语言(如 C#、Python 和 Java)集成。</p>
<p>它通过定义插件,并以几行代码将它们串联在一起,实现这一点。</p>
<p>Semantic Kernel 的独特之处在于其能够利用人工智能自动协调插件。通过 Semantic Kernel 的计划器,可以请求 LLM 生成一个实现用户特定目标的计划,然后由 Semantic Kernel 为用户执行该计划。</p>
<p>它提供了以下功能:</p>
<ul>
<li><strong>AI 服务的抽象</strong>(如聊天、文本转图像、音频转文本等)和内存存储。</li>
<li>针对 OpenAI、Azure OpenAI、Hugging Face、本地模型等服务的这些抽象的实现,以及针对众多向量数据库(如 Chroma、Qdrant、Milvus 和 Azure)的实现。</li>
<li>插件的通用表示形式,之后可由 AI 自动协调。</li>
<li>从多种来源创建此类插件的能力,包括 OpenAPI 规范、提示和用目标语言编写的任意代码。</li>
<li>对提示管理和渲染的可扩展支持,包括对 Handlebars 和 Liquid 等常见格式的内置处理。</li>
<li>基于这些抽象的丰富功能,例如负责任的 AI 过滤器、依赖注入集成等。</li>
</ul>
<p>由于其灵活性、模块化和可观察性,Semantic Kernel 被企业广泛使用。它具备增强安全性的功能,如遥测支持,以及挂钩和过滤器,能够大规模提供负责任的 AI 解决方案。</p>
<h3><span style="font-size: 18px"><strong>三、详细开发流程</strong></span></h3>
<h3><span style="font-size: 16px"><strong>1. 安装和配置 Semantic Kernel</strong></span></h3>
<ol>
<li>
<ul>
<li><strong>安装 SDK</strong>:通过 NuGet 或 pip 安装 Semantic Kernel 的库。</li>
<li><strong>配置 API</strong>:将 OpenAI、Azure OpenAI 或其他 LLM API 集成到项目中。</li>
</ul>
</li>
</ol>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> kernel =<span style="color: rgba(0, 0, 0, 1)"> Kernel.Builder
.WithOpenAIChatCompletionService(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gpt-4</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">YOUR_API_KEY</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://api.openai.com/v1/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
.Build();</span></pre>
</div>
<p><span style="font-size: 16px"><strong>2. 定义功能插件</strong></span></p>
<ul>
<li><strong>插件类型</strong>:插件可以是用提示(Prompts)、代码函数,或 OpenAPI 规范定义的功能模块。</li>
<li><strong>创建插件</strong>:插件可以封装不同的任务,例如处理文本、执行计算、或与外部服务交互。</li>
</ul>
<p><strong> 示例(简单文本生成插件)</strong>:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> textPlugin = kernel.CreateSemanticFunction(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">What's your goal? Summarize the following: {{input}}</span><span style="color: rgba(128, 0, 0, 1)">"</span>);</pre>
</div>
<p><span style="font-size: 16px"><strong>3. 编排和组合插件</strong></span></p>
<ul>
<li><strong>单一任务执行</strong>:直接调用一个插件完成任务。</li>
<li><strong>任务链式执行</strong>:通过 Semantic Kernel 的“计划器”自动将多个插件组合起来完成复杂任务。</li>
</ul>
<p><strong>示例(任务链式执行)</strong>:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> planner =<span style="color: rgba(0, 0, 0, 1)"> kernel.ImportPlannerPlugin();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> plan = <span style="color: rgba(0, 0, 255, 1)">await</span> planner.CreatePlanAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Summarize a long document and translate it into French.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result = <span style="color: rgba(0, 0, 255, 1)">await</span> kernel.RunAsync(plan, <span style="color: rgba(0, 0, 255, 1)">new</span> ContextVariables(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Input text here.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
Console.WriteLine(result.Result);</span></pre>
</div>
<p><span style="font-size: 16px"><strong>4. 集成内存功能</strong></span></p>
<ul>
<li><strong>记忆用户上下文</strong>:Semantic Kernel 提供内存存储支持,可以将数据保存到向量数据库中(如 Chroma、Milvus)。</li>
<li><strong>查询历史记录</strong>:通过语义查询,智能体可以动态访问存储的上下文信息。</li>
</ul>
<p><strong>示例(内存集成)</strong>:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">kernel.ImportMemoryPlugin();
</span><span style="color: rgba(0, 0, 255, 1)">await</span> kernel.Memory.SaveInformationAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">userData</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">User's preference for French summaries.</span><span style="color: rgba(128, 0, 0, 1)">"</span>);</pre>
</div>
<p><span style="font-size: 16px"><strong>5. 扩展和优化</strong></span></p>
<ul>
<li><strong>自定义 Prompt 模板</strong>:支持 Handlebars 和 Liquid 模板格式,轻松管理复杂的提示。</li>
<li><strong>安全性和责任感</strong>:启用遥测和过滤器,确保解决方案符合企业要求。</li>
<li><strong>多模型支持</strong>:切换到新的 LLM 模型(例如 GPT-4、Claude)时,只需替换 API,而无需改动代码。</li>
</ul>
<h3><span style="font-size: 18px"><strong>四、示例应用:多功能 AI Agent</strong></span></h3>
<p>目标:开发一个 AI Agent,具备以下能力:</p>
<ol>
<li>分析并总结输入文本。</li>
<li>将总结翻译成用户指定语言。</li>
<li>保存用户偏好到内存中。</li>
</ol>
<p><strong>完整代码示例(C#)</strong>:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span><span style="color: rgba(0, 0, 0, 1)"> Microsoft.SemanticKernel;
</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Program
{
</span><span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">async</span> Task Main(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">[] args)
{
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 初始化 Semantic Kernel</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> kernel =<span style="color: rgba(0, 0, 0, 1)"> Kernel.Builder
.WithOpenAIChatCompletionService(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">gpt-4</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">YOUR_API_KEY</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://api.openai.com/v1/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
.Build();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 定义插件</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> summarizer = kernel.CreateSemanticFunction(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Summarize the following: {{input}}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> translator = kernel.CreateSemanticFunction(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Translate to {{language}}: {{input}}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用户输入</span>
<span style="color: rgba(0, 0, 255, 1)">string</span> inputText = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Semantic Kernel is a framework for integrating LLMs into apps.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">string</span> targetLanguage = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">French</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行任务</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> summary = <span style="color: rgba(0, 0, 255, 1)">await</span> kernel.RunAsync(summarizer, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ContextVariables(inputText));
</span><span style="color: rgba(0, 0, 255, 1)">var</span> translatedText = <span style="color: rgba(0, 0, 255, 1)">await</span> kernel.RunAsync(translator, <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ContextVariables(summary.Result)
{
[</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">language</span><span style="color: rgba(128, 0, 0, 1)">"</span>] =<span style="color: rgba(0, 0, 0, 1)"> targetLanguage
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 保存用户偏好到内存</span>
<span style="color: rgba(0, 0, 0, 1)"> kernel.ImportMemoryPlugin();
</span><span style="color: rgba(0, 0, 255, 1)">await</span> kernel.Memory.SaveInformationAsync(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">userPreferences</span><span style="color: rgba(128, 0, 0, 1)">"</span>, $<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Preferred language: {targetLanguage}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出结果</span>
Console.WriteLine(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Summary: </span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> summary.Result);
Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Translation: </span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> translatedText.Result);
}
}</span></pre>
</div>
<ol>
<li>将代码部署到本地或云端。</li>
<li>提供输入(例如长文本),观察 AI Agent 自动执行文本总结、翻译和存储偏好。</li>
<li>修改模型、提示或插件实现更多定制功能。</li>
</ol>
<p>通过 Semantic Kernel,开发者可以高效创建多功能 AI Agent,将其应用于各种企业场景,如智能客服、知识管理、内容生成等。</p>
<p> </p>
<p>周国庆</p>
<p>2024/12/29</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/tianqing/p/18639649
頁:
[1]