Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2120. C# - Semantic Kernel의 Skill과 Function 사용 예제 [링크 복사], [링크+제목 복사],
조회: 12076
글쓴 사람
정성태 (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 방식으로 분리한 유형으로 봐도 될 듯합니다.
정성태

[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13917정성태4/30/202563VS.NET IDE: 199. Directory.Build.props에 정의한 속성에 대해 Condition 제약으로 값을 변경하는 방법
13916정성태4/23/2025465디버깅 기술: 221. WinDbg 분석 사례 - ASP.NET HttpCookieCollection을 다중 스레드에서 사용할 경우 무한 루프 현상 - 두 번째 이야기
13915정성태4/13/20251685닷넷: 2331. C# - 실행 시에 메서드 가로채기 (.NET 9)파일 다운로드1
13914정성태4/11/20251987디버깅 기술: 220. windbg 분석 사례 - x86 ASP.NET 웹 응용 프로그램의 CPU 100% 현상 (4)
13913정성태4/10/20251214오류 유형: 950. Process Explorer - 64비트 윈도우에서 32비트 프로세스의 덤프를 뜰 때 "Error writing dump file: Access is denied." 오류
13912정성태4/9/2025872닷넷: 2330. C# - 실행 시에 메서드 가로채기 (.NET 5 ~ .NET 8)파일 다운로드1
13911정성태4/8/20251115오류 유형: 949. WinDbg - .NET Core/5+ 응용 프로그램 디버깅 시 sos 확장을 자동으로 로드하지 못하는 문제
13910정성태4/8/20251266디버깅 기술: 219. WinDbg - 명령어 내에서 환경 변수 사용법
13909정성태4/7/20251771닷넷: 2329. C# - 실행 시에 메서드 가로채기 (.NET Framework 4.8)파일 다운로드1
13908정성태4/2/20252182닷넷: 2328. C# - MailKit: SMTP, POP3, IMAP 지원 라이브러리
13907정성태3/29/20251991VS.NET IDE: 198. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C# 프로젝트의 출력 경로 변경하기
13906정성태3/27/20252262닷넷: 2327. C# - 초기화되지 않은 메모리에 접근하는 버그?파일 다운로드1
13905정성태3/26/20252301Windows: 281. C++ - Windows / Critical Section의 안정화를 위해 도입된 "Keyed Event"파일 다운로드1
13904정성태3/25/20251916디버깅 기술: 218. Windbg로 살펴보는 Win32 Critical Section파일 다운로드1
13903정성태3/24/20251536VS.NET IDE: 197. (OneDrive, Dropbox 등의 공유 디렉터리에 있는) C++ 프로젝트의 출력 경로 변경하기
13902정성태3/24/20251748개발 환경 구성: 742. Oracle - 테스트용 hr 계정 및 데이터 생성파일 다운로드1
13901정성태3/9/20252138Windows: 280. Hyper-V의 3가지 Thread Scheduler (Classic, Core, Root)
13900정성태3/8/20252361스크립트: 72. 파이썬 - SQLAlchemy + oracledb 연동
13899정성태3/7/20251834스크립트: 71. 파이썬 - asyncio의 ContextVar 전달
13898정성태3/5/20252154오류 유형: 948. Visual Studio - Proxy Authentication Required: dotnetfeed.blob.core.windows.net
13897정성태3/5/20252384닷넷: 2326. C# - PowerShell과 연동하는 방법 (두 번째 이야기)파일 다운로드1
13896정성태3/5/20252204Windows: 279. Hyper-V Manager - VM 목록의 CPU Usage 항목이 항상 0%로 나오는 문제
13895정성태3/4/20252248Linux: 117. eBPF / bpf2go - Map에 추가된 요소의 개수를 확인하는 방법
13894정성태2/28/20252284Linux: 116. eBPF / bpf2go - BTF Style Maps 정의 구문과 데이터 정렬 문제
13893정성태2/27/20252225Linux: 115. eBPF (bpf2go) - ARRAY / HASH map 기본 사용법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...