语义的归语义,语法的归语法。

基础定义

最基本的Native Function定义只需要在方法上添加 SKFunction 的特性即可。

using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Orchestration; namespace MySkillsDirectory; public class MyCSharpSkill
{
[SKFunction("Return the first row of a qwerty keyboard")]
public string Qwerty(string input)
{
return "qwertyuiop";
} [SKFunction("Return a string that's duplicated")]
public string DupDup(string text)
{
return text + text;
}
}

默认情况下只需要传递一个string 参数就行,如果需要多个参数的话,和Semantic Function一样,也是使用Context,不过这里传进去是 SKContext。在方法上使用 SKFunctionContextParameter声明一下参数,可以提供一定的说明,同时的有需要的话,可以设置参数的默认值。

using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Orchestration; namespace MySkillsDirectory; public class MyCSharpSkill
{
[SKFunction("Return a string that's duplicated")]
public string DupDup(string text)
{
return text + text;
} [SKFunction("Joins a first and last name together")]
[SKFunctionContextParameter(Name = "firstname", Description = "Informal name you use")]
[SKFunctionContextParameter(Name = "lastname", Description = "More formal name you use")]
public string FullNamer(SKContext context)
{
return context["firstname"] + " " + context["lastname"];
}
}

调用的时候,一样使用 ContextVariables.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration; using MySkillsDirectory; // ... instantiate a kernel as myKernel var myContext = new ContextVariables();
myContext.Set("firstname","Sam");
myContext.Set("lastname","Appdev"); var myCshSkill = myKernel.ImportSkill ( new MyCSharpSkill(), "MyCSharpSkill");
var myOutput = await myKernel.RunAsync(myContext,myCshSkill["FullNamer"]); Console.WriteLine(myOutput);

当然异步的方法也是支持的。这样的话,就可以处理一些像是网络请求,数据库访问、文件读写等操作了。

using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Orchestration; public class MyCSharpSkill
{
[SKFunction("Return the first row of a qwerty keyboard")]
public string Qwerty(string input)
{
return "qwertyuiop";
} [SKFunction("Return the second row of a qwerty keyboard")]
[SKFunctionName("Asdfg")]
public async Task<string> AsdfgAsync(string input)
{
await ...do something asynchronous... return "asdfghjkl";
}

这里针对 AsdfgAsync 添加了一个 SKFunctionName 的特性,主要是为了使Function name 好看一些,避免 MyCSharpSkill.AsdfgAsync 这样。

混合调用

和 Semantic Function中能够调用 Native Function一样,在 Native Function也可以调用Semantic Function,其中主要使用的还是 SKContext.

using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Orchestration; namespace MySkillsDirectory; public class MyCSharpSkill
{
[SKFunction("Tell me a joke in one line of text")]
[SKFunctionName("TellAJokeInOneLine")]
public async Task<string> TellAJokeInOneLineAsync(SKContext context)
{
// Fetch a semantic function previously loaded into the kernel
ISKFunction joker1 = context.Func("funSkill", "joker"); // OR Fetch a semantic function previously loaded into the kernel
ISKFunction joker2 = context.Skills.GetSemanticFunction("funSkill", "joker"); var joke = await joker1.InvokeAsync(); return joke.Result.ReplaceLineEndings(" ");
}
}

这里并没有限制是 Semantic Function 还是Native Function,所以甚至可以完全使用Native Function编排技能调用,除了参数的定义和提取有些费劲以外,其他的几乎没什么问题,毕竟返回值都是string,这也就贯彻了Text is the universal wire protocol,即便是代码也得将就一下。

一些核心技能

Semantic Kernel 中大部分的能力都是有技能提供的,例如Semantic Kernel的一个核心组件Planner,其实就是一个Semantic Skill,另外官方提供了一些Core SKill,基本是日常比较常用的。具体可以参考https://github.com/microsoft/semantic-kernel/tree/main/dotnet/src/SemanticKernel/CoreSkills

和自行定义的Native Function一样的,只需要使用ImportSkill就行了

using Microsoft.SemanticKernel.CoreSkills;

// ( You want to instantiate a kernel and configure it first )

myKernel.ImportSkill(new TimeSkill(), "time");

const string ThePromptTemplate = @"
Today is: {{time.Date}}
Current time is: {{time.Time}} Answer to the following questions using JSON syntax, including the data used.
Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)?
Is it weekend time (weekend/not weekend)?"; var myKindOfDay = myKernel.CreateSemanticFunction(ThePromptTemplate, maxTokens: 150); var myOutput = await myKindOfDay.InvokeAsync();
Console.WriteLine(myOutput);

至此,Semantic Kernel 的基础能力就学习得差不多了。


参考资料:

  1. https://learn.microsoft.com/en-us/semantic-kernel/howto/nativefunctions
  2. https://learn.microsoft.com/en-us/semantic-kernel/howto/coreskills
  3. https://github.com/microsoft/semantic-kernel/tree/main/dotnet/src/SemanticKernel/CoreSkills

Semantic Kernel 入门系列:💾Native Function的更多相关文章

  1. 快速入门系列--WebAPI--03框架你值得拥有

    接下来进入的是俺在ASP.NET学习中最重要的WebAPI部分,在现在流行的互联网场景下,WebAPI可以和HTML5.单页应用程序SPA等技术和理念很好的结合在一起.所谓ASP.NET WebAPI ...

  2. linux入门系列12--磁盘管理之分区、格式化与挂载

    前面系列文章讲解了VI编辑器.常用命令.防火墙及网络服务管理,本篇将讲解磁盘管理相关知识. 本文将会介绍大量的Linux命令,其中有一部分在"linux入门系列5--新手必会的linux命令 ...

  3. 数据挖掘入门系列教程(九)之基于sklearn的SVM使用

    目录 介绍 基于SVM对MINIST数据集进行分类 使用SVM SVM分析垃圾邮件 加载数据集 分词 构建词云 构建数据集 进行训练 交叉验证 炼丹术 总结 参考 介绍 在上一篇博客:数据挖掘入门系列 ...

  4. es6 快速入门 系列 —— 变量声明:let和const

    其他章节请看: es6 快速入门 系列 变量声明:let和const 试图解决的问题 经典的 var 声明让人迷惑 function demo1(v){ if(v){ var color='red' ...

  5. webpack 快速入门 系列 —— 性能

    其他章节请看: webpack 快速入门 系列 性能 本篇主要介绍 webpack 中的一些常用性能,包括热模块替换.source map.oneOf.缓存.tree shaking.代码分割.懒加载 ...

  6. vue 快速入门 系列 —— Vue 实例的初始化过程

    其他章节请看: vue 快速入门 系列 Vue 实例的初始化过程 书接上文,每次调用 new Vue() 都会执行 Vue.prototype._init() 方法.倘若你看过 jQuery 的源码, ...

  7. 07. Web大前端时代之:HTML5+CSS3入门系列~H5 地理位置

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 源码:https://github.com/duniti ...

  8. 05. Web大前端时代之:HTML5+CSS3入门系列~H5 多媒体系

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 1.引入 概述 音频文件或视频文件都可以看做是一个容器文 ...

  9. 06. Web大前端时代之:HTML5+CSS3入门系列~HTML5 画布

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 我们先看看画布的魅力: 初始画布 canvas默认是宽3 ...

  10. 08. Web大前端时代之:HTML5+CSS3入门系列 ~ QQ空间时间轴

    Web大前端时代之:HTML5+CSS3入门系列:http://www.cnblogs.com/dunitian/p/5121725.html 大前端系列,主要就是使用CSS3.0来实现,注释我已经打 ...

随机推荐

  1. Component inside <Transition> renders non-element root node that cannot be animated

    原因是transition标签下存在多个根标签 原代码: 解决方法 Suspense还处于试验阶段,可能导致default内容与fallback内容同时存在,导致错误

  2. 字典方法 setdefault()、pprint;迭代、递归的区别

    计算一个字符串中每个字符出现的次数 import pprint message = 'It was a bright cold day in April, ' \ 'and the clocks we ...

  3. Hive 操作与应用 词频统计

    一.hive用本地文件进行词频统计 1.准备本地txt文件 2.启动hadoop,启动hive 3.创建数据库,创建文本表 4.映射本地文件的数据到文本表中 5.hql语句进行词频统计交将结果保存到结 ...

  4. WIn环境基于vs编辑器,Qt应用申请管理员权限的解决方法(转载)

    原文章地址:https://blog.csdn.net/weixin_38416696/article/details/103879377 基本按照那个文章的操作.基本可以了. 唯一问题就是那个vs的 ...

  5. 关与python面向对象的认识

    面向对象编程 类:从一堆对象中以抽象的方式把相同的特征归类得到. 抽象类 类 实列 子类抽象为父类,子类继承父类特征. 类实例化为实例,实例抽象为类. class Human(object): cen ...

  6. zookeeper异常

    1. KeeperErrorCode = Unimplemented for /service 在使用curator时,对zk有版本匹配关系. Curator 2.**    <--->  ...

  7. ArchKeeper (开篇):架构守护平台的问题与理念

    作者:京东科技 倪新明 在敏捷开发环境下,系统通过迭代增量的交付价值,系统架构也是如此.团队不可能在项目之初就建立完美的系统架构,系统架构应该随着系统迭代不断演进.架构演进和架构腐化是看待架构的不同视 ...

  8. spring aop切面说明

    execution:处理Join Point的类型,例如call.execution (* android.app.Activity.on**(..)):这个是最重要的表达式,第一个*表示返回值,*表 ...

  9. 论文解读(ToAlign)《ToAlign: Task-oriented Alignment for Unsupervised Domain Adaptation》

    论文信息 论文标题:ToAlign: Task-oriented Alignment for Unsupervised Domain Adaptation论文作者:Guoqiang Wei, Cuil ...

  10. 研发效能负责人/研发效能1号位 |DevOps负责人

    想要做好业务,老板们除了要梳理好公司级别的业务目标,公司的组织架构,还要搭个有产出的班子,也就是找负责人.建团队,让组织架构充实起来.搭班子最重要的就是把负责人找到,就是团队1号位的人.本文主要讲团队 ...