Closed10

ちょまどさんの書いたSemanticKernelの記事(.NET Conf Japan 2023「.NET + AI」補足記事)を攻略する

Kento.YamadaKento.Yamada

元ネタ
https://zenn.dev/chomado/articles/231219-dot-net-conf

やってみたのでリポジトリを残しておく
READMEとか実行環境、動作した時に使ったパッケージはあとでかく。

https://github.com/ymd65536/SemanticKernelTest

ちなみに動作環境

  • Macbook Air Apple Sillicon M2(Sonoma14.0)
  • VSCode Version: 1.85.1
  • dotNET 8.0.100

パッケージのバージョンは以下を参照

https://github.com/ymd65536/SemanticKernelTest/blob/main/SemanticKernelTest.csproj

Kento.YamadaKento.Yamada

デスクトップに移動します。

cd Desktop

コンソールプロジェクトを作成します。

mkdir SemanticKernelTest
cd SemanticKernelTest
dotnet new console
Kento.YamadaKento.Yamada

パッケージを追加

dotnet add package Azure.Identity
dotnet add package Microsoft.SemanticKernel
Kento.YamadaKento.Yamada
using Azure.Identity;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;

// Kernel builder を作る
var builder = Kernel.CreateBuilder();

// kernel が使う AI モデル情報,認証情報などを登録
builder.AddAzureOpenAIChatCompletion(
        deploymentName: "gpt-35-turbo",
        modelId: "gpt-35-turbo",
        endpoint: "https://{endpoint}.openai.azure.com",
        credentials: new DefaultAzureCredential()
);

var kernel = builder.Build();


var skPrompt = @"{{$input}}
上の文章を、なるべく短く要約してください。";

var executionSettings = new OpenAIPromptExecutionSettings 
{
    MaxTokens = 2000,
    Temperature = 0.2,
    TopP = 0.5
};

var input = """
吾輩は猫である。名前はまだ無い。
どこで生れたかとんと見当がつかぬ。何でも薄暗いじめじめした所でニャーニャー泣いていた事だけは記憶している。
吾輩はここで始めて人間というものを見た。
しかもあとで聞くとそれは書生という人間中で一番獰悪な種族であったそうだ。
この書生というのは時々我々を捕えて煮て食うという話である。
しかしその当時は何という考もなかったから別段恐しいとも思わなかった。
ただ彼の掌に載せられてスーと持ち上げられた時何だかフワフワした感じがあったばかりである。
掌の上で少し落ちついて書生の顔を見たのがいわゆる人間というものの見始であろう。
""";

var summaryFunction = kernel.CreateFunctionFromPrompt(
    promptTemplate: skPrompt, 
    executionSettings: executionSettings
);

var summaryResult = await kernel.InvokeAsync(
    summaryFunction, 
    new KernelArguments() { ["input"] = input }
);

Console.WriteLine(summaryResult);

Kento.YamadaKento.Yamada

どうやらAzure AI Studioでモデルをデプロイしないといけないみたい。

モデルをデプロイしていない場合の実行時エラー

Exception has occurred: CLR/Microsoft.SemanticKernel.HttpOperationException
An exception of type 'Microsoft.SemanticKernel.HttpOperationException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.
Status: 404 (Not Found)
ErrorCode: DeploymentNotFound

Content:
{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}

Headers:
apim-request-id: REDACTED
x-ms-client-request-id: 4381f660-2936-405b-b7f7-18cf50497bdc
Strict-Transport-Security: REDACTED
X-Content-Type-Options: REDACTED
x-ms-region: REDACTED
Date: Tue, 19 Dec 2023 05:09:26 GMT
Content-Length: 198
Content-Type: application/json'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception 	 Azure.RequestFailedException : The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.
Status: 404 (Not Found)
ErrorCode: DeploymentNotFound

Content:
{"error":{"code":"DeploymentNotFound", "message":"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again."}}

Content-Type: application/json
   at Azure.Core.HttpPipelineExtensions.<ProcessMessageAsync>d__0.MoveNext()
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Azure.AI.OpenAI.OpenAIClient.<GetChatCompletionsAsync>d__14.MoveNext()
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.<RunRequestAsync>d__42`1.MoveNext()
このスクラップは5ヶ月前にクローズされました