.Net: Kernel creation in Agent does not respect the Azure OpenAI endpoint
Describe the bug When I call a plugin inside an agent. It always try to connect openai endpoint instead of azure open ai endpoint
To Reproduce
-
Create an Agent with .WithAzureOpenAIChatCompletion called.
-
Add a plugin for this agent.
-
make the agent call this plugin.
-
The plugin use the kernel to make an chat completion request.
-
the request always goes to openai service The source code of Agent shows it always create OpenAIChatCompletion Agent.cs Internal Agent( AssistantModel assistantModel, PromptTemplateConfig? config, OpenAIRestContext restContext, IEnumerable
? plugins = null) { ... IKernelBuilder builder = Kernel.CreateBuilder(); this.Kernel = Kernel .CreateBuilder() .AddOpenAIChatCompletion(this._model.Model, this._restContext.ApiKey) <= .Build(); ...
Platform
- OS: Windows
- IDE: Visual Studio
- Language: C#
- Source: 1.6.2
Additional context
icrosoft.SemanticKernel.HttpOperationException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 (api.openai.com:443)
---> Azure.RequestFailedException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 (api.openai.com:443)
---> System.Net.Http.HttpRequestException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 (api.openai.com:443)
---> System.Net.Sockets.SocketException (10060): 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败 。
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.1.WaitWithCancellationAsync(CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.HttpConnectionWaiter1.WaitForConnectionWithTelemetryAsync(HttpRequestMessage request, HttpConnectionPool pool, Boolean async, CancellationToken requestCancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.1 pipeline)
at Azure.Core.Pipeline.RequestActivityPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 pipeline, Boolean async)
at Azure.Core.Pipeline.ResponseBodyPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 pipeline, Boolean async)
at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 pipeline, Boolean async)
at Azure.Core.Pipeline.RedirectPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 pipeline, Boolean async)
at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 pipeline, Boolean async)
at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 pipeline, Boolean async)
at Azure.Core.HttpPipelineExtensions.ProcessMessageAsync(HttpPipeline pipeline, HttpMessage message, RequestContext requestContext, CancellationToken cancellationToken)
at Azure.AI.OpenAI.OpenAIClient.GetChatCompletionsAsync(ChatCompletionsOptions chatCompletionsOptions, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func1 request)
--- End of inner exception stack trace ---
at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.RunRequestAsync[T](Func1 request)
at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetChatMessageContentsAsync(ChatHistory chat, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.ChatCompletion.ChatCompletionServiceExtensions.GetChatMessageContentAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, PromptExecutionSettings executionSettings, Kernel kernel, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.Planning.Handlebars.HandlebarsPlanner.CreatePlanCoreAsync(Kernel kernel, String goal, KernelArguments arguments, CancellationToken cancellationToken)
at Microsoft.SemanticKernel.Planning.PlannerInstrumentation.CreatePlanAsync[TPlanner,TPlan](Func6 createPlanAsync, TPlanner planner, Kernel kernel, String goal, KernelArguments arguments, ILogger logger, CancellationToken cancellationToken)
at Backend.Plugins.MathSolverPlugin.SolveAsync(Kernel kernel, String problem)
Expected behavior
The kernel inside the agent should respect WithAzureOpenAIChatCompletion settings.
For azure, please call the override of AddOpenAIChatCompletion that accepts the deployment endpoint url.
https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Experimental/Agents/AgentBuilder.cs#L108
For azure, please call the override of
AddOpenAIChatCompletionthat accepts the deployment endpoint url.https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Experimental/Agents/AgentBuilder.cs#L108
Hi crickman, I didn't find the override of AddOpenAIChatCompletion from your link. I have called WithAzureOpenAIChatCompletion for the agent builder. But when the agent calls my plugin. The kernel the plugin get is initialized by openai endpoint: https://github.com/microsoft/semantic-kernel/blob/88bdb1155e148c98a72b171400cdc0ad0687ddae/dotnet/src/Experimental/Agents/Internal/Agent.cs#L124
My link was refering to this override (AgentBuilder.cs):
public AgentBuilder WithAzureOpenAIChatCompletion(string endpoint, string model, string apiKey, string? version = null)
Have you been able to make progress?
But I have called WithAzureOpenAIChatCompletion for agent which i have mentioned in my description.
I believe this is a bug instead of a question because Agent Construction in Agent.cs always create kernel with AddOpenAIChatCompletion. So the kernel in agent always use
https://github.com/microsoft/semantic-kernel/blob/88bdb1155e148c98a72b171400cdc0ad0687ddae/dotnet/src/Experimental/Agents/Internal/Agent.cs#L124
this.Kernel =
Kernel
.CreateBuilder()
.AddOpenAIChatCompletion(this._model.Model, this._restContext.ApiKey)
.Build();
when the agent calls my plugin. And my plugin uses this kernel to call gpt. It will always conect to openai instead of azure openai whether WithAzureOpenAIChatCompletion has been called or not for the agent.
Hi thanks for the support. I have the same problem using Azure Openai, it seems that the Kernel created under the Agent object is always builded from "AddOpenAIChatCompletion". This does not allow to invoke the prompt inside the plugin throwing httpoperation exception for invalid key.
I am using the latest package version for agents 1.6.3-alpha.
For the agent builder, call the WithAzureOpenAIChatCompletion overload that accepts the endpoint paramter. AOAI uses and endpoint Open AI does not.
https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/Experimental/Agents/AgentBuilder.cs#L108
builder .AddOpenAIChatCompletion(<endpoint>, this._model.Model, this._restContext.ApiKey)
I am already using WithAzureOpenAIChatCompletion building the agent as you showed in the last comment. I'm using this block of code to build the agent, and it works fine.
new AgentBuilder()
.WithAzureOpenAIChatCompletion("AOAI_API_ENDPOINT", "AOAI_GPT_MODEL", "AOAI_API_KEY")
But the problem is on the Kernel object (under the Agent). It seems that the kernel object is always builded from "AddOpenAIChatCompletion". Here the screen from the internal class Agent.
Its WithAzureOpenAIChatCompletion
Its
WithAzureOpenAIChatCompletion
As i mentioned in my last comment, i m already using WithAzureOpenAIChatCompletion.
The problem is not the agent building, but the kernel under it is always built using AddOpenAIChatCompletion (see the screen attached in my last commenti).
When the KernelBuilder gets an Azure OpenAI key, but it' s from type AddOpenAiChatCompletion i surely get the exception (invalid api key).