Microsoft MVP성태의 닷넷 이야기
.NET Framework: 2120. C# - Semantic Kernel의 Skill과 Function 사용 예제 [링크 복사], [링크+제목 복사],
조회: 5332
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13697정성태7/26/2024386닷넷: 2283. C# - async 메서드에서의 lock/Monitor.Enter/Exit 잠금 처리파일 다운로드1
13696정성태7/26/2024297오류 유형: 920. dotnet publish - error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for '...'
13695정성태7/25/2024327닷넷: 2282. C# - Lock / Wait 상태에서도 STA COM 메서드 호출 처리파일 다운로드1
13694정성태7/25/2024306닷넷: 2281. C# - ASP.NET Core Web App의 Request 용량 상한값 (Kestrel, IIS)
13693정성태7/24/2024459개발 환경 구성: 717. Visual Studio - C# 프로젝트에서 레지스트리에 등록하지 않은 COM 개체 참조 및 사용 방법파일 다운로드1
13692정성태7/24/2024459디버깅 기술: 199. Windbg - 리눅스에서 뜬 닷넷 응용 프로그램 덤프 파일에 포함된 DLL의 Export Directory 탐색
13691정성태7/23/2024522디버깅 기술: 198. Windbg - 스레드의 Win32 Message Queue 정보 조회
13690정성태7/23/2024533오류 유형: 919. Visual C++ 리눅스 프로젝트 - error : ‘u8’ was not declared in this scope
13689정성태7/22/2024527디버깅 기술: 197. Windbg - PE 포맷의 Export Directory 탐색
13688정성태7/21/2024662닷넷: 2280. C# - Lock / Wait 상태에서도 일부 Win32 메시지 처리파일 다운로드1
13687정성태7/19/2024675닷넷: 2279. C# - PostThreadMessage로 보낸 메시지를 Windows Forms에서 수신하는 방법파일 다운로드1
13686정성태7/19/2024626오류 유형: 918. Visual Studio - ATL Simple Object 추가 시 error C2065: 'IDR_...': undeclared identifier
13685정성태7/19/2024695스크립트: 66. Windows 디렉터리 경로를 WSL의 /mnt 포맷으로 구하는 방법 - 두 번째 이야기
13684정성태7/19/2024865닷넷: 2278. C# - 문자열 보간식 사례
13683정성태7/18/2024877오류 유형: 917. ClrMD - Linux 환경의 .NET 5 덤프 분석 시 hang 현상
13682정성태7/18/2024946닷넷: 2277. WPF - 스레드에 종속되는 DependencyObject파일 다운로드1
13681정성태7/17/20241014닷넷: 2276. C# 13 - (2) 메서드 그룹의 자연 타입 개선 (메서드 추론 개선)파일 다운로드1
13680정성태7/16/20241030닷넷: 2275. C# - Method Group, Natural Type, function_type파일 다운로드1
13679정성태7/16/20241040Linux: 75. Linux - C++ (getaddrinfo 등을 담고 있는) libnss 정적 링크
13678정성태7/15/2024898VS.NET IDE: 191. Visual Studio 2022 - .NET 5 프로젝트를 Docker Support로 실행했을 때 오류
13677정성태7/15/20241032오류 유형: 916. MSBuild - CheckEolTargetFramework (warning NETSDK1138)
13676정성태7/14/2024996Linux: 75. gdb에서 glibc의 함수에 Breakpoint 걸기
13675정성태7/13/2024988C/C++: 166. C/C++ - DLL에서 template 함수를 export하는 방법파일 다운로드1
13674정성태7/13/20241043오류 유형: 915. Unhandled Exception: Microsoft.Diagnostics.NETCore.Client.ServerNotAvailableException: Unable to connect to Process
13673정성태7/11/20241065닷넷: 2274. C# 13 - (1) 신규 이스케이프 시퀀스 '\e'파일 다운로드1
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...