Semantic Kernel - 餐厅点餐助手

news/2025/3/22 23:21:00/文章来源:https://www.cnblogs.com/MrChuJiu/p/18785887

场景描述

一个餐厅智能点餐助手,该助手需要能够回答顾客关于菜单的问题,例如菜品的特色、价格等。我们将使用Semantic Kernel和Kernel Memory来实现这一功能,结合自定义插件和记忆管理来处理特定的业务逻辑。

安装必要的包

安装Semantic Kernel相关的NuGet包

dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.KernelMemory.SemanticKernelPlugin
dotnet add package Microsoft.KernelMemory.Service.AspNetCore

配置Kernel Memory

配置Kernel Memory以使用Azure OpenAI服务和PostgreSQL作为存储

using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Service.AspNetCore;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Memory;var builder = WebApplication.CreateBuilder(args);// 配置Semantic Kernel
builder.Services.AddKernel().AddAzureOpenAIChatCompletion();// 配置Semantic Memory
builder.Services.AddKernelMemory<MemoryServerless>(memoryBuilder =>
{memoryBuilder.WithPostgresMemoryDb(new PostgresConfig(){ConnectionString = builder.Configuration.GetConnectionString("PostgresConnectionString")!}).WithAzureOpenAITextGenerationService(deploymentName: "your-deployment-name",endpoint: "your-endpoint",apiKey: "your-api-key").WithAzureOpenAITextEmbeddingGenerationService(deploymentName: "your-embedding-deployment-name",endpoint: "your-endpoint",apiKey: "your-api-key");
});var app = builder.Build();// 映射RAG端点
app.AddKernelMemoryEndpoints(apiPrefix: "/rag");
app.Run();

创建菜单插件

创建一个菜单插件,用于提供菜单相关的功能。这个插件将包含两个方法:一个用于获取特色菜品,另一个用于获取特定菜品的价格。

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Functions;public class MenuPlugin
{[SKFunction("Provides a list of specials from the menu.")]public string GetSpecials(){return """Special Soup: Clam ChowderSpecial Salad: Cobb SaladSpecial Drink: Chai Tea""";}[SKFunction("Provides the price of the requested menu item.")]public string GetItemPrice(string menuItem){// 这里可以添加更复杂的逻辑来获取价格return "$9.99";}
}

使用Kernel Memory存储和检索信息

在点餐助手的逻辑中,使用Kernel Memory来存储和检索用户的历史交互信息

using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Memory;public class RestaurantChatAgent
{private readonly IKernel _kernel;private readonly MemoryPlugin _memoryPlugin;public RestaurantChatAgent(IKernel kernel, MemoryPlugin memoryPlugin){_kernel = kernel;_memoryPlugin = memoryPlugin;}public async Task RunAsync(){// 存储用户数据await _memoryPlugin.UpsertAsync("user_name", "Alice");await _memoryPlugin.UpsertAsync("user_location", "Paris");// 检索数据var userName = await _memoryPlugin.GetAsync("user_name");var userLocation = await _memoryPlugin.GetAsync("user_location");// 使用检索到的数据生成响应var response = $"Hello, {userName}! I heard you are from {userLocation}.";Console.WriteLine(response);}
}

运行程序

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;public class Program
{public static async Task Main(string[] args){var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) =>{services.AddKernel().AddAzureOpenAIChatCompletion();services.AddKernelMemory<MemoryServerless>(memoryBuilder =>{memoryBuilder.WithPostgresMemoryDb(new PostgresConfig(){ConnectionString = "your-postgres-connection-string"}).WithAzureOpenAITextGenerationService(deploymentName: "your-deployment-name",endpoint: "your-endpoint",apiKey: "your-api-key").WithAzureOpenAITextEmbeddingGenerationService(deploymentName: "your-embedding-deployment-name",endpoint: "your-endpoint",apiKey: "your-api-key");});services.AddSingleton<MenuPlugin>();services.AddSingleton<RestaurantChatAgent>();}).Build();var agent = host.Services.GetRequiredService<RestaurantChatAgent>();await agent.RunAsync();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/903679.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

昆明理工大学最新《现代材料测试技术》复试真题及答案

-材料测试 昆工材料物理与化学、材料学、材料表征与分析、材料工程、F001现代材料测试技术、864材料科学基础、昆明理工大学材料调剂

RTT 全志D1s跑tina linux

刚毕业那会抽奖抽了一块RTT的D1S开发板,看了一下打印log就放下吃灰了。跑RT-thread的感觉折腾起来太麻烦了就让他吃灰了。最近手头有一块屏幕和一个2欧的小喇叭打算驱动一下。 正好这块板子也出来好多年了。玩一玩。首先我找到了百问网的哪吒开发板他也是D1S的,直接把他的SDK…

COMSOL 基础学习笔记

设置网格 软件通过计算网格顶点的数值,推算其他位置的数值。 行函数:移动的波。 二次行函数(除网格顶点外还计算两定点连线中心的值):网格最大长度 \(≤\frac{λ}{6}\) 一次行函数:网格最大长度 \(≤\frac{λ}{12}\) 修改方式:

NSSCTF ROUND#28 Ciallo~(∠・ω )⌒☆ WriteUp

WriteUp 题目信息 来源:NSSCTF 名称:ROUND#28 Ciallo~(∠・ω )⌒☆ 分类:Reverse 描述:无题目链接: https://www.nssctf.cn/contest/732/解题思路 首先使用DIE对文件进行查壳,发现这是一个无壳的64位exe文件。于是使用64位IDA对文件进行反汇编,得到伪代码如下:先一步步…

day35 nfs共享服务器的学习

day35 nfs共享服务器的学习 1.企业集群为什么要共享服务器 没有共享服务器先看一下没有共享服务器的问题 A用户上传啦图片到web01的服务器,然后B用户访问但是负载均衡服务器把请求分发到了web02的服务器上,导致B用户查看不了图片。配置啦共享服务器无论是用户把图片发送给web…

Nature Communications | 全基因组沉默子图谱揭示人类细胞基因调控新机制

摘要总结 这篇文章是2025年1月发表在《Nature Communications》杂志上的一篇研究,标题为“Uncovering the whole genome silencers of human cells via Ss-STARR-seq”。这篇文章通过开发一种名为Ss-STARR-seq的高通量筛选技术,首次在全基因组范围内系统性鉴定了人类细胞中的…

数据结构3

基本数据处理技术概率论与数理统计1-基本概念 概率论与数理统计2-基本数据结构 概率论与数理统计3-基本数据处理技术 基本的数据处理技术 查找 查找的基本概念 在哪里找:查找表是由同一类型的数据元素(或记录)构成的集合,集合中的数据元素之间关系松散。 按什么查找:根据给…

NSSCTF ROUND#28 动态调试 WriteUp

WriteUp 题目信息 来源:NSSCTF 名称:ROUND#28 动态调试 分类:Reverse 描述:无题目链接: https://www.nssctf.cn/contest/732/解题思路 首先使用DIE对文件进行查壳,发现这是一个无壳的32位ELF文件。于是使用32位IDA对文件进行反汇编,得到伪代码如下:为方便阅读伪代码,修…

Video Analysis Assignment

This scene is what the heroine saw from a begin sycarmore at the first time . It was this landscape that awake the heroine of her father’s word: “ A painting is more than the sum of its parts”. This scene is shot from the big sycarmore and it is a estab…

AI一键生成流程图架构图甘特图饼图等可视化图形 原创

AI脑图除了使用文字、语音、图片、文件、网页和视频等一键生成思维导图外,现在也可以支持一键生成流程图、架构图、甘特图等可视化图形了,使用非常简单,告诉AI脑图你想要生成什么图,大概不到两分钟就会制作好并以图片回复给你啦。 支持的可视化图形有: 流程图 例如向AI脑图…

Atcoder ABC398.F - ABCBA 题解 KMP的next函数

题目链接:https://atcoder.jp/contests/abc398/tasks/abc398_f 题目大意: 给你一个字符串 \(s\),要求在字符串 \(s\) 的末尾添加尽可能少的字符使其变成一个回文串。 解题思路: 首先,设输入的字符串为 \(s = s_1 s_2 \ldots s_n\),设字符串 \(s\) 翻转后的字符串为 \(s\)…

方法的定义和调用

//方法的应用 package Base; public class Demon16 { public static void main(String[] args) {// TODO Auto-generated method stubint max=max(10,10);System.out.println(max); } //比大小 public static int max(int num1,int num2) {int result=0;if(num1==num2) {System…