首页 > 其他分享 >【Semantic Kernel】3、本机函数(Native Function)

【Semantic Kernel】3、本机函数(Native Function)

时间:2023-07-08 09:44:09浏览次数:35  
标签:Function Kernel Semantic string MyCSharpSkill public Native

基础定义

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

using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Orchestration;
 
namespace MySkillsDirectory;
 
public class MyCSharpSkill
{
    [SKFunction("Return a string that's duplicated")]
    [SKFunctionName("Dup")]//指定Function名称
    public string DupDup(string text)
    {
        return text + text;
    }
}

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

public class MyCSharpSkill
{
    [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.

// ... 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 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.

public class MyCSharpSkill
{
    [SKFunction("Tell me a joke in one line of text")]
    [SKFunctionName("InvokeSemanticFunc")]
    public async Task<string> InvokeSemanticFunc(SKContext context)
    {
        ISKFunction func = context.Func("Country", "Size");
        //ISKFunction func = _kernel.Skills.GetFunction("Country", "Size");

        var contry = await func.InvokeAsync("china");
        return contry.Result.ToUpper();
    }
}

这里并没有限制是 Semantic Function 还是Native Function,所以甚至可以完全使用Native Function。

官方提供的技能(core skill)

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

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

using Microsoft.SemanticKernel.CoreSkills;
 
myKernel.ImportSkill(new TimeSkill(), "time");
 
const string ThePromptTemplate = @"
Today is: {{time.Date}}";
 
var myKindOfDay = myKernel.CreateSemanticFunction(ThePromptTemplate, maxTokens: 150);
 
var myOutput = await myKindOfDay.InvokeAsync();
Console.WriteLine(myOutput);

标签:Function,Kernel,Semantic,string,MyCSharpSkill,public,Native
From: https://www.cnblogs.com/fanfan-90/p/17536062.html

相关文章

  • linux系统报错:系统自己弹出诸如 kernel:NMI watchdog: BUG: soft lockup - CPU#2 stuc
    1、https://blog.csdn.net/weixin_41752389/article/details/120777145 内核软死锁(softlockup)Softlockup:这个bug没有让系统彻底死机,但是若干个进程(或者kernelthread)被锁死在了某个状态(一般在内核区域),很多情况下这个是由于内核锁的使用的问题。出现死锁原因1、CPU高负载时......
  • Multi-Modal Attention Network Learning for Semantic Source Code Retrieval 解读
    Multi-ModalAttentionNetworkLearningfor SemanticSourceCodeRetrieva Multi-ModalAttentionNetworkLearningfor SemanticSourceCodeRetrieval,题目意思是用于语义源代码检索的多模态注意网络学习,2019年发表于ASE的##研究什么东西Background:研究代码检索技......
  • 2023-06-04-Generating-Function-Editor
    You'regrowingdesperatefromthefight.基本策略已知系数的幂级数首先是一些可以通过整体法得到封闭形式的幂级数,所谓整体法,即是通过将幂级数位移,用自己表示自己然后做差。\[\begin{aligned}\left\langle1,1,1,1,1,\dots\right\rangle&\rightarrow\frac{1}{1......
  • 2023-05-20-Probability-Generating-Function
    It'stimetorollthedice.\(\mathtt{Definition}\)令\(X\)为取值非负的随机变量,那么\(X\)的概率生成函数\(\mathtt{Probability\Generating\Function}\)为\[\begin{aligned}G_x(z)=\sum_{k\ge0}\mathrm{Pr}(X=k)z^k\end{aligned}\]根据上式可以得知......
  • 重写JSON.stringify与JSON.parse使其支持解析function类型
    constJSONStringify=(option)=>{returnJSON.stringify(option,(key,val)=>{//处理函数丢失问题if(typeofval==='function'){return`${val}`;}//处理undefined丢失问......
  • kernel pwn入门
    LinuxKernel介绍Linux内核是Linux操作系统的核心组件,它提供了操作系统的基本功能和服务。它是一个开源软件,由LinusTorvalds在1991年开始开发,并得到了全球广泛的贡献和支持。Linux内核的主要功能包括进程管理、内存管理、文件系统、网络通信、设备驱动程序等。它负责管理......
  • linux Kernel
    ......
  • How to use handleChange() function in react component?
    An onChange eventistriggeredwhenvaluesareenteredintheinput.Thisfiresafunction handleChange(),thatisusedtosetanewstatefortheinput.1.HandlingSingleInputFirst,wehavetosetuptheinputfieldasacontrolledcomponentsothatw......
  • 基于 Spring Cloud Function 的 Azure Function 开发
    Notice:本文章不包含AzureFunction环境配置等内容1.1前提Azure账户,且有可使用的订阅Azure支持的JDK(本教程适用于JDK1.8)IntelliJIDEA社区版或无限制版均可Maven3.5+最新的FunctionCoreTools1.2创建SpringCloudFunctionAzure工程在Github仓......
  • 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in
    项目场景:mysql创建function报错误1418-ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAin问题描述:执行创建函数的sql语句时,提示:ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAinitsdeclarationandbinaryloggingisenab......