Closed10
ちょまどさんの書いたSemanticKernelの記事(.NET Conf Japan 2023「.NET + AI」補足記事)を攻略する
ピン留めされたアイテム

本セッションをGitHub Codespacesでやりました。

元ネタ
やってみたのでリポジトリを残しておく
READMEとか実行環境、動作した時に使ったパッケージはあとでかく。
ちなみに動作環境
- Macbook Air Apple Sillicon M2(Sonoma14.0)
- VSCode Version: 1.85.1
- dotNET 8.0.100
パッケージのバージョンは以下を参照

まずはdotNETとnugetをセットアップ

デスクトップに移動します。
cd Desktop
コンソールプロジェクトを作成します。
mkdir SemanticKernelTest
cd SemanticKernelTest
dotnet new console

パッケージを追加
dotnet add package Azure.Identity
dotnet add package Microsoft.SemanticKernel

Azure CLI入れてなくて泣いた。
かなしみのセットアップ
brew install azure-cli
brew install powershell/tap/powershell
pwsh
認証を通す。
az login
az account set --subscription {subscription_id}
このあとAzure Portalでロールを追加する。
以下の記事を参考
エラーが出るとこうなる
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
ヒントとなるドキュメント

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);

どうやら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()

出力結果

参考にしたURL
- https://learn.microsoft.com/en-us/answers/questions/589649/what-is-the-meaning-of-the-following-error-princip
- https://learn.microsoft.com/ja-jp/powershell/scripting/install/installing-powershell-on-macos?view=powershell-7.4
- https://learn.microsoft.com/ja-jp/cli/azure/authenticate-azure-cli
- https://learn.microsoft.com/ja-jp/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet
- https://github.com/microsoft/autogen/issues/822
- https://learn.microsoft.com/ja-jp/cli/azure/account/subscription?view=azure-cli-latest
このスクラップは2023/12/20にクローズされました