semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Kernel creation in Agent does not respect the Azure OpenAI endpoint

Open hanhsia opened this issue 2 years ago • 11 comments

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

  1. Create an Agent with .WithAzureOpenAIChatCompletion called.

  2. Add a plugin for this agent.

  3. make the agent call this plugin.

  4. The plugin use the kernel to make an chat completion request.

  5. 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.g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem) at System.Threading.Tasks.TaskCompletionSourceWithCancellation1.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.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) at Azure.Core.Pipeline.HttpClientTransport.ProcessAsync(HttpMessage message, Boolean async) --- End of inner exception stack trace --- at Azure.Core.Pipeline.HttpClientTransport.ProcessAsync(HttpMessage message, Boolean async) at Azure.Core.Pipeline.HttpPipelineTransportPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory1 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.

hanhsia avatar Mar 17 '24 16:03 hanhsia

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

crickman avatar Mar 18 '24 15:03 crickman

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

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

hanhsia avatar Mar 19 '24 01:03 hanhsia

My link was refering to this override (AgentBuilder.cs):

public AgentBuilder WithAzureOpenAIChatCompletion(string endpoint, string model, string apiKey, string? version = null)

crickman avatar Mar 19 '24 17:03 crickman

Have you been able to make progress?

crickman avatar Mar 20 '24 20:03 crickman

But I have called WithAzureOpenAIChatCompletion for agent which i have mentioned in my description.

hanhsia avatar Mar 21 '24 02:03 hanhsia

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.

hanhsia avatar Mar 21 '24 15:03 hanhsia

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. image

zimeo99 avatar Mar 22 '24 15:03 zimeo99

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) 

crckman avatar Mar 25 '24 15:03 crckman

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.

image

zimeo99 avatar Mar 25 '24 16:03 zimeo99

Its WithAzureOpenAIChatCompletion

crickman avatar Mar 25 '24 17:03 crickman

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

COSTAS99 avatar Mar 25 '24 17:03 COSTAS99