Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Bug: Exception when using Plugin and AzureSearchChatExtensionConfiguration #8488

Closed
AlexSchlecht opened this issue Sep 3, 2024 · 2 comments · Fixed by #9150
Closed
Assignees
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code

Comments

@AlexSchlecht
Copy link

Describe the bug
If you use a custom plugin while simultaneously using AzureSearchChatExtensions, an exception is thrown after the plugin method is executed. The plugin method itself is executed correctly.
Without plugin but with AzureSearchChatExtension, it works all fine.
Without AzureSearchChatExtension but with Plugin, it works all fine. (Of Course without my Data in Azure Blob-Storage)

To Reproduce
A prompt which will trigger the Plugin-Method will cause an Exception.
Here ist some sample-Code:

My Program.cs

using AzureSearch_Error;
await PluginAzureSearch_Error.Start();

My Code

using Azure.AI.OpenAI;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using SUMS3.Win.SUMS3System;

namespace AzureSearch_Error
{
    public class PluginAzureSearch_Error
    {
        public static IConfiguration configuration;
        public static void InitConfig()
        {
            var configBuilder = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddUserSecrets<Program>(); 

            configuration = configBuilder.Build();
        }

        public static async Task Start()
        {
            InitConfig();

            var builder = Kernel.CreateBuilder();
            builder.AddAzureOpenAIChatCompletion(
                configuration["deploymentName"]!,
                configuration["endpoint"]!,
                configuration["apiKey"]!
            );

            builder.Plugins.AddFromType<MyTestPlugin>("MyTestPlugin");


            Kernel kernel = builder.Build();

            var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
            OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
            {
                ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,  //for Plugin

#pragma warning disable SKEXP0010

                //Ursache für einen Fehler mit Plugins!
                AzureChatExtensionsOptions = GetAzureChatExtensionsOptions()

#pragma warning restore SKEXP0010
            };

            var history = new ChatHistory();
            var frage = "send me an email";   //This prompt triggers the Plugin

            Console.Write("Du: ");
            Console.WriteLine(frage);

            history.AddUserMessage(frage);

            // Get the response from the AI
            //Here a error occurs, after (!) successful execution of Plugin-Method   <------------------ Error
            //This error only occurs with a prompt that triggers the plugin.

            var result = await chatCompletionService.GetChatMessageContentAsync(
                history,
                executionSettings: openAIPromptExecutionSettings,
                kernel: kernel);

            Console.WriteLine("KI: " + result);
            history.AddAssistantMessage(result.ToString());

            Console.WriteLine();
        }

        public static AzureChatExtensionsOptions GetAzureChatExtensionsOptions()
        {
            var azureSearchExtensionConfiguration = new AzureSearchChatExtensionConfiguration
            {
                SearchEndpoint = new Uri(configuration["searchEndpoint"]!),
                Authentication = new OnYourDataApiKeyAuthenticationOptions(configuration["authentication"]!),
                IndexName = configuration["indexName"]!
            };

            return new AzureChatExtensionsOptions
            {
                Extensions = { azureSearchExtensionConfiguration }

            };
        }
    }
}

Here my Plugin-Code (simple sample)

using Microsoft.SemanticKernel;
using System.ComponentModel;

namespace SUMS3.Win.SUMS3System
{
    public class MyTestPlugin
    {
        [KernelFunction("sendmail_to_me")]
        [Description("Sendet an den User eine email.")]
        [return: Description("Text über das Ergebnis des Mailversandes")]
        public string SendEmailToMe()
        {
            try
            {
                
                return "erledigt, die Mail wurde gesendet.";
            }
            catch (Exception ex)
            {
                return $"Fehler, Mail konnte nicht gesendet werden {ex.Message}";
            }
        }
    }
}

Screenshots
grafik

Platform

  • OS: Windows
  • IDE: Microsoft Visual Studio Enterprise 2022 (64-Bit) - Current, Version 17.9.6
  • Language: C#
  • Model: gpt-4o
@AlexSchlecht AlexSchlecht added the bug Something isn't working label Sep 3, 2024
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Sep 3, 2024
@github-actions github-actions bot changed the title Bug: Exception when using Plugin and AzureSearchChatExtensionConfiguration .Net: Bug: Exception when using Plugin and AzureSearchChatExtensionConfiguration Sep 3, 2024
@AlexSchlecht
Copy link
Author

I forgot the detailed error message:

	Microsoft.SemanticKernel.HttpOperationException
	  HResult=0x80131500
	  Nachricht = Service request failed.
	Status: 400 (Bad Request)
	
	Content:
	{"error": {"requestid": "d784ca74-fca8-49e1-bdcc-1bdf80aeb25a", "code": 400, "message": "Invalid chat message detected: message content must be string"}}
	
	Headers:
	skip-error-remapping: REDACTED
	x-envoy-upstream-service-time: REDACTED
	apim-request-id: REDACTED
	x-ms-client-request-id: 5f6198ba-5064-48c3-9da1-f04ef4d1a229
	Strict-Transport-Security: REDACTED
	X-Content-Type-Options: REDACTED
	x-ms-region: REDACTED
	x-ratelimit-remaining-requests: REDACTED
	x-ratelimit-remaining-tokens: REDACTED
	Date: Tue, 03 Sep 2024 11:35:34 GMT
	Content-Length: 153
	Content-Type: application/json; charset=UTF-8
	
	  Quelle = Microsoft.SemanticKernel.Connectors.OpenAI
	  Stapelüberwachung:
	   bei Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.<RunRequestAsync>d__55`1.MoveNext()
	   bei Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.<GetChatMessageContentsAsync>d__35.MoveNext()
	   bei Microsoft.SemanticKernel.ChatCompletion.ChatCompletionServiceExtensions.<GetChatMessageContentAsync>d__2.MoveNext()
	   bei AzurOpenAI.LightPluginBeispiel.<Start>d__2.MoveNext() in C:\Users\a2002\source\GitHub\spielwieseVersuche\LightPluginBeispiel.cs: Zeile75
	   bei Program.<<Main>$>d__0.MoveNext() in C:\Users\a2002\source\GitHub\spielwieseVersuche\Program.cs: Zeile5
	
	  Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
	    [Externer Code]
	
	Innere Ausnahme 1:
	RequestFailedException: Service request failed.
	Status: 400 (Bad Request)
	
	Content:
	{"error": {"requestid": "d784ca74-fca8-49e1-bdcc-1bdf80aeb25a", "code": 400, "message": "Invalid chat message detected: message content must be string"}}
	
	Headers:
	skip-error-remapping: REDACTED
	x-envoy-upstream-service-time: REDACTED
	apim-request-id: REDACTED
	x-ms-client-request-id: 5f6198ba-5064-48c3-9da1-f04ef4d1a229
	Strict-Transport-Security: REDACTED
	X-Content-Type-Options: REDACTED
	x-ms-region: REDACTED
	x-ratelimit-remaining-requests: REDACTED
	x-ratelimit-remaining-tokens: REDACTED
	Date: Tue, 03 Sep 2024 11:35:34 GMT
	Content-Length: 153
	Content-Type: application/json; charset=UTF-8

@evchaki evchaki removed the triage label Sep 3, 2024
@evchaki
Copy link
Contributor

evchaki commented Sep 3, 2024

@dmytrostruk - can you make sure the error message are clear on this.

github-merge-queue bot pushed a commit that referenced this issue Oct 8, 2024
…n Azure/OpenAI connectors (#9150)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Resolves: #8072
Resolves: #8488

Azure Chat Completion with data service returns `400 (Bad Request)
Invalid chat message detected: message content must be string` response
when assistant message in chat history doesn't contain `content`
property or when this property is an array of content items. This case
is applicable for function calling, where AI model returns set of tools
to call instead of message content.

This PR contains a change to set `content` property as an empty string
only for cases when content doesn't exist (`null`).

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https:/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https:/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code
Projects
Status: Sprint: Done
4 participants