Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2120. C# - Semantic Kernel의 Skill과 Function 사용 예제 [링크 복사], [링크+제목 복사],
조회: 12162
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 8개 있습니다.)
.NET Framework: 2117. C# - (OpenAI 기반의) Microsoft Semantic Kernel을 이용한 자연어 처리
; https://www.sysnet.pe.kr/2/0/13345

.NET Framework: 2118. C# - Semantic Kernel의 Prompt chaining 예제
; https://www.sysnet.pe.kr/2/0/13347

.NET Framework: 2119. C# - Semantic Kernel의 "Basic Loading of the Kernel" 예제
; https://www.sysnet.pe.kr/2/0/13348

.NET Framework: 2120. C# - Semantic Kernel의 Skill과 Function 사용 예제
; https://www.sysnet.pe.kr/2/0/13349

.NET Framework: 2121. C# - Semantic Kernel의 대화 문맥 유지
; https://www.sysnet.pe.kr/2/0/13352

.NET Framework: 2123. C# - Semantic Kernel의 ChatGPT 대화 구현
; https://www.sysnet.pe.kr/2/0/13355

.NET Framework: 2124. C# - Semantic Kernel의 Planner 사용 예제
; https://www.sysnet.pe.kr/2/0/13357

.NET Framework: 2125. C# - Semantic Kernel의 Semantic Memory 사용 예제
; https://www.sysnet.pe.kr/2/0/13358




C# - Semantic Kernel의 Skill과 Function 사용 예제

지난 글에서는,

C# - (OpenAI 기반의) Microsoft Semantic Kernel을 이용한 자연어 처리
; https://www.sysnet.pe.kr/2/0/13345

코드상에서 SemanticFunction을 만들어봤는데요, 이때 사용한 CreateSemanticFunction 함수의 원형을 보면,

public static ISKFunction CreateSemanticFunction(
    this IKernel kernel,
    string promptTemplate,
    string? functionName = null,
    string skillName = "",
    string? description = null,
    int maxTokens = 256,
    double temperature = 0,
    double topP = 0,
    double presencePenalty = 0,
    double frequencyPenalty = 0,
    IEnumerable<string>? stopSequences = null);

반환값이 ISKFunction으로 나오고, 인자로는 promptTemplate과 functionName, skillName 등을 볼 수 있습니다. 그러니까, SemanticFunction은 (Semantic Kernel의 구성요소 중 하나인) Skill과 그것에 속하는 Function을 동적으로 만드는 메서드였던 것입니다.

다시 그때의 코드를 곱씹어 볼까요?

var prompt = @"{{$input}}

One line TLDR with the fewest words.";

var summarize = kernel.CreateSemanticFunction(prompt);

prompt 문자열이 promptTemplate이었고, CreateSemanticFunction은 skillName과 functionName을 단순히 공백으로 설정한 Function을 만든 것입니다.

재미있는 것은, Function과 그것의 묶음인 Skill 구성은 디렉터리와 파일로 구성하는 것도 가능합니다. 이에 대한 예제를 다음의 글 3개에서 볼 수 있는데요,

samples/notebooks/dotnet/00-getting-started.ipynb
; https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb

How to run a semantic skills from file
; https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb

Running Semantic Functions Inline
; https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/03-semantic-function-inline.ipynb

예제에서 소개한 대로 우리도 직접 해볼까요? ^^

이를 위해 Console 프로젝트에 "FunSkill" 디렉터리를 추가하고, 그 하위에 다시 "Joke" 디렉터리를 만든 다음 skprompt.txt라는 파일명으로 다음의 내용을 채워줍니다.

WRITE EXACTLY ONE JOKE or HUMOROUS STORY ABOUT THE TOPIC BELOW

JOKE MUST BE:
- G RATED
- WORKPLACE/FAMILY SAFE
NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY

BE CREATIVE AND FUNNY. I WANT TO LAUGH.
{{$style}}
+++++

{{$input}}
+++++

이렇게 만들어준 (이름이 고정인) skprompt.txt 파일이 바로 CreateSemanticFunction 메서드의 인자로 넘긴 "promptTemplate"에 해당합니다. 그리고 예상할 수 있듯이 "{{$Input}}"에는 kernel.RunAsync로 전달되는 문장이 대체됩니다.

이 상태에서 "FunSkill" 디렉터리는 "Skill" 이름이 되고, 그 하위에 있는 "Joke" 디렉터리는 "Function" 이름이 됩니다. 여기까지 작업하면 솔루션 탐색기에서 다음과 같이 구성됐을 것입니다.

skill_directory_1.png

저 디렉터리/파일 구조가 빌드 후 출력이 함께 되도록 CopyToOutputDirectory도 설정해야 합니다.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.SemanticKernel" Version="0.13.277.1-preview" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="FunSkill\Joke\" />
  </ItemGroup>

  <ItemGroup>
    <None Update="FunSkill\Joke\skprompt.txt">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

자, 그럼 준비는 모두 끝났습니다. 이제 다음과 같이 코드를 작성하면 특정 문장에 대해 "G 등급", ... "창의적이고 재미있는..." 농담을 만들어 줍니다.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.KernelExtensions;

namespace ConsoleApp1;

internal class Program
{
    // Install-Package Microsoft.SemanticKernel -Pre
    private static async Task<int> Main(string[] args)
    {
        (string apiKey, _) = GetKeyInfo(@"d:\settings\openai_key.txt");

        KernelConfig kernelConfig = new KernelConfig();
        kernelConfig.AddOpenAITextCompletionService(
            "default",
            "text-davinci-003",               // OpenAI Model name
            apiKey       // OpenAI API Key
        );

        var kernel = Kernel.Builder
            .WithConfiguration(kernelConfig)
            .Build();

        var skill = kernel.ImportSemanticSkillFromDirectory(Directory.GetCurrentDirectory(), "FunSkill");

        var result = await kernel.RunAsync("time travel to dinosaur age", skill["Joke"]);
        Console.WriteLine(result);

        return 0;
    }

    private static (string apiKey, string orgName) GetKeyInfo(string filePath)
    {
        string[] keyInfo = File.ReadLines(filePath).ToArray();
        return (keyInfo[0], keyInfo.Length >= 1 ? keyInfo[1] : "");
    }
}

/* 출력 결과
A time traveler went back to the dinosaur age and was amazed by the size of the creatures. He asked one of the dinosaurs, "How do you manage to get around with such short legs?"

The dinosaur replied, "It's easy, I just take my time!"
*/

대충... 느낌이 오시죠? ^^ Semantic Kernel의 github repo에는 이러한 템플릿 Skill 예제를 몇 개 담고 있습니다.

CalendarSkill
    Function: AssistantShowCalendarEvents

ChatSkill
    Function: Chat, ChatFilter, ChatGPT, ChatUser, ChatV2

ChildrensBookSkill
    Function: BookIdeas, CreateBook

ClassificationSkill
    Function: Importance, Question

CodingSkill
    Function: Code, CodePython, CommandLinePython, DOSScript, EmailSearch, Entity

FunSkill
    Function: Execuse, Joke, Limerick

IntentDetectionSkill
    Function: AssistantIntent

MiscSkill
    Function: Continue, ElementAtIndex

QASkill
    Function: AssistantResults, ContextQuery, Form, GitHubMemoryQuery, QNA, Question

SummarizeSkill
    Function: MakeAbstractReadable, Notegen, Summarize, Topics

WriterSkill
    Function: Acronym, AcronymGenerator, AcronymReverse, Brainstorm, EmailGen, EmailTo, 
              EnglishImprover, NovelChapter, NovelChapterWithNotes, NovelOutline, Rewrite, 
              ShortPoem, StoryGen, TellMeMore, Translate, TwoSentenceSummary

위의 목록을 보고 있으면... 우리가 그동안 프로그래밍에서 만들었던 함수의 개념을, 그대로 "자연어 문장"에 대해 수행할 수 있는 함수로 표현한 듯한 인상을 받습니다.

요즘 ChatGPT에게 던지는 프롬프트의 중요성이 부각되고 있는데요, 결국 Skill/Function은 재사용 가능한 프롬프트 서식이라고 봐도 무방하겠습니다.




CreateSemanticFunction의 나머지 인자는 개별 Function에 해당하는 skprompt.txt 파일과 동일한 위치에 만들어 둔 config.json 파일을 추가해 설정할 수 있습니다. 아래는 FuncSkill/Joke에 있는 예제 json 파일입니다.

{
  "schema": 1,
  "description": "Generate a funny joke",
  "type": "completion",
  "completion": {
    "max_tokens": 1000,
    "temperature": 0.9,
    "top_p": 0.0,
    "presence_penalty": 0.0,
    "frequency_penalty": 0.0
  },
  "input": {
    "parameters": [
      {
        "name": "input",
        "description": "Joke subject",
        "defaultValue": ""
      }
    ]
  }
}

이 파일은 반드시 필요한 것은 아닌데, 내부의 옵션에 대해서는 OpenAI의 제어값에 해당하므로 필요한 경우라면 당연히 설정을 해야 합니다. (개별 의미는 차차 익숙해지는 대로 다뤄보겠습니다.)

마지막으로, "Running Semantic Functions Inline" 에제를 보면, 사실 CreateSemanticFunction은 PromptTemplateConfig, PromptTemplate, SemanticFunctionConfig, RegisterSemanticFunction 조합을 하나로 제공하는 도우미 메서드에 해당한다는 것을 알 수 있습니다.

이 정도면, 얼핏 Semantic Kernel의 기본 사용법이 제법 손에 잡히는 것 같죠?!!! ^^

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




이 글과 관련해서 다음의 문서도 읽어보시고,

Configuring templates
; https://learn.microsoft.com/en-us/semantic-kernel/howto/configuringfunctions

그러고 보니, 지난 글에서 Bing Chat이 내놓은 Semantic Kernel 소개에서,

... 이것은 대규모 언어 모델(Large Language Model, LLM) AI "프롬프트"와 템플릿, 체이닝 및 계획 기능을 제공...

문구 내에 "프롬프트와 템플릿"이 나옵니다. 아마도 저것은 Skill과 Function을 의미하는 듯합니다. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 5/16/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2025-03-28 01시59분
[Deep Research] Model Context Protocol(MCP) 개념 및 이해를 위한 학습 자료
; https://discuss.pytorch.kr/t/deep-research-model-context-protocol-mcp/6594/1

modelcontextprotocol/csharp-sdk
 - The official C# SDK for Model Context Protocol servers and clients, maintained by Microsoft
; https://github.com/modelcontextprotocol/csharp-sdk

Create an MCP Server with .NET and C#
; https://dev.to/mehrandvd/create-an-mcp-server-with-net-and-c-251p

MCP를 달리 생각해 보면, Skill과 Function을 외부 Plug-in 방식으로 분리한 유형으로 봐도 될 듯합니다.
정성태

... 61  62  [63]  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12364정성태10/11/202019438.NET Framework: 948. C# 9.0 - (2) localsinit 플래그 내보내기 무시(Suppress emitting localsinit flag)파일 다운로드1
12363정성태10/11/202021131.NET Framework: 947. C# 9.0 - (1) 대상으로 형식화된 new 식(Target-typed new expressions) [2]파일 다운로드1
12362정성태10/11/202017694VS.NET IDE: 151. Visual Studio 2019에 .NET 5 rc/preview 적용하는 방법
12361정성태10/11/202019704.NET Framework: 946. C# 9.0을 위한 개발 환경 구성
12360정성태10/8/202014937오류 유형: 666. The type or namespace name '...' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?)
12359정성태10/7/202017155오류 유형: 665. Windows - 재부팅 후 iSCSI 연결이 끊기는 문제
12358정성태10/7/202018031오류 유형: 664. Web Deploy 설치 시 "A newer version of Microsoft Web Deploy 3.6 was found on this machine." 오류 [3]
12357정성태10/7/202015668오류 유형: 663. 이벤트 로그 - The storage optimizer couldn't complete retrim on New Volume
12356정성태10/7/202031291오류 유형: 662. ASP.NET Core와 500.19, 500.21 오류 (0x8007000d)
12355정성태10/3/202014821오류 유형: 661. Hyper-V Linux VM의 Internal 유형의 가상 Switch에 대한 IP 연결이 되지 않는 경우
12354정성태10/2/202028835오류 유형: 660. Web Deploy (msdeploy.axd) 실행 시 오류 기록 [1]
12353정성태10/2/202018285개발 환경 구성: 518. 비주얼 스튜디오에서 IIS 웹 서버로 "Web Deploy"를 이용해 배포하는 방법
12352정성태10/2/202019544개발 환경 구성: 517. Hyper-V Internal 네트워크에 NAT을 이용한 인터넷 연결 제공
12351정성태10/2/202017995오류 유형: 659. Nox 실행이 안 되는 경우 - Unable to bind to the underlying transport for ...
12350정성태9/25/202022412Windows: 175. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 [2]파일 다운로드1
12349정성태9/25/202016581Linux: 32. Ubuntu 20.04 - docker를 위한 tcp 바인딩 추가
12348정성태9/25/202017601오류 유형: 658. 리눅스 docker - Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
12347정성태9/25/202033133Windows: 174. WSL 2의 네트워크 통신 방법 [4]
12346정성태9/25/202016616오류 유형: 657. IIS - http://localhost 방문 시 Service Unavailable 503 오류 발생
12345정성태9/25/202016149오류 유형: 656. iisreset 실행 시 "Restart attempt failed." 오류가 발생하지만 웹 서비스는 정상적인 경우파일 다운로드1
12344정성태9/25/202018065Windows: 173. 서비스 관리자에 "IIS Admin Service"가 등록되어 있지 않다면?
12343정성태9/24/202029123.NET Framework: 945. C# - 닷넷 응용 프로그램에서 메모리 누수가 발생할 수 있는 패턴 [5]
12342정성태9/24/202019139디버깅 기술: 171. windbg - 인스턴스가 살아 있어 메모리 누수가 발생하고 있는지 확인하는 방법
12341정성태9/23/202017171.NET Framework: 944. C# - 인스턴스가 살아 있어 메모리 누수가 발생하고 있는지 확인하는 방법파일 다운로드1
12340정성태9/23/202016829.NET Framework: 943. WPF - WindowsFormsHost를 담은 윈도우 생성 시 메모리 누수
12339정성태9/21/202017013오류 유형: 655. 코어 모드의 윈도우는 GUI 모드의 윈도우로 교체가 안 됩니다.
... 61  62  [63]  64  65  66  67  68  69  70  71  72  73  74  75  ...