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

Azure CLI入れてなくて泣いた。

かなしみのセットアップ

brew install azure-cli
brew install powershell/tap/powershell
pwsh

認証を通す。

az login
az account set --subscription {subscription_id}

このあとAzure Portalでロールを追加する。
以下の記事を参考

https://zenn.dev/chomado/articles/231207-semantic-kernel-rc3#managed-id-の設定

エラーが出るとこうなる

Unhandled exception. System.AggregateException: One or more errors occurred. (HTTP 401 (PermissionDenied)

Principal does not have access to API/Operation.)
 ---> Microsoft.SemanticKernel.HttpOperationException: HTTP 401 (PermissionDenied)

Principal does not have access to API/Operation.
 ---> System.ClientModel.ClientResultException: HTTP 401 (PermissionDenied)

Principal does not have access to API/Operation.
   at Azure.AI.OpenAI.ClientPipelineExtensions.ProcessMessageAsync(ClientPipeline pipeline, PipelineMessage message, RequestOptions options)
   at Azure.AI.OpenAI.Chat.AzureChatClient.CompleteChatAsync(BinaryContent content, RequestOptions options)
   at OpenAI.Chat.ChatClient.CompleteChatAsync(IEnumerable`1 messages, ChatCompletionOptions options, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func`1 request)
   --- End of inner exception stack trace ---
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func`1 request)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunctionFromPrompt.GetChatCompletionResultAsync(IChatCompletionService chatCompletion, Kernel kernel, PromptRenderingResult promptRenderingResult, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunctionFromPrompt.InvokeCoreAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunction.<>c__DisplayClass27_0.<<InvokeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.SemanticKernel.Kernel.InvokeFilterOrFunctionAsync(NonNullCollection`1 functionFilters, Func`2 functionCallback, FunctionInvocationContext context, Int32 index)
   at Microsoft.SemanticKernel.Kernel.OnFunctionInvocationAsync(KernelFunction function, KernelArguments arguments, FunctionResult functionResult, Boolean isStreaming, Func`2 functionCallback, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunction.InvokeAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at Program.<Main>$(String[] args) in /workspaces/SemanticKernelTesting/Program.cs:line 12
~ $ dotnet run 
Unhandled exception. System.AggregateException: One or more errors occurred. (HTTP 401 (PermissionDenied)

Principal does not have access to API/Operation.)
 ---> Microsoft.SemanticKernel.HttpOperationException: HTTP 401 (PermissionDenied)

Principal does not have access to API/Operation.
 ---> System.ClientModel.ClientResultException: HTTP 401 (PermissionDenied)

Principal does not have access to API/Operation.
   at Azure.AI.OpenAI.ClientPipelineExtensions.ProcessMessageAsync(ClientPipeline pipeline, PipelineMessage message, RequestOptions options)
   at Azure.AI.OpenAI.Chat.AzureChatClient.CompleteChatAsync(BinaryContent content, RequestOptions options)
   at OpenAI.Chat.ChatClient.CompleteChatAsync(IEnumerable`1 messages, ChatCompletionOptions options, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func`1 request)
   --- End of inner exception stack trace ---
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func`1 request)
   at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetChatMessageContentsAsync(String targetModel, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunctionFromPrompt.GetChatCompletionResultAsync(IChatCompletionService chatCompletion, Kernel kernel, PromptRenderingResult promptRenderingResult, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunctionFromPrompt.InvokeCoreAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunction.<>c__DisplayClass27_0.<<InvokeAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.SemanticKernel.Kernel.InvokeFilterOrFunctionAsync(NonNullCollection`1 functionFilters, Func`2 functionCallback, FunctionInvocationContext context, Int32 index)
   at Microsoft.SemanticKernel.Kernel.OnFunctionInvocationAsync(KernelFunction function, KernelArguments arguments, FunctionResult functionResult, Boolean isStreaming, Func`2 functionCallback, CancellationToken cancellationToken)
   at Microsoft.SemanticKernel.KernelFunction.InvokeAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at Program.<Main>$(String[] args) in /workspaces/SemanticKernelTesting/Program.cs:line 12

ヒントとなるドキュメント

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()
このスクラップは2023/12/20にクローズされました