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: .Net Plugin return[Description] being totaly ignored? #10010

Open
christianarg opened this issue Dec 18, 2024 · 0 comments
Open

.Net: Bug: .Net Plugin return[Description] being totaly ignored? #10010

christianarg opened this issue Dec 18, 2024 · 0 comments
Assignees
Labels
bug Something isn't working .NET Issue or Pull requests regarding .NET code

Comments

@christianarg
Copy link

When I define un a plugin [return:Description("Some description")] as documented in the official documentation https://learn.microsoft.com/en-us/semantic-kernel/concepts/plugins/?pivots=programming-language-csharp#1-define-your-plugin the return description seems to be completely ignored.
After testing many times I've observed that the indications in [return:Description("...")] where completely ignored by the model. Since there is a large discussion about how to capture exact request that is made to azure open ai when using chat completion service here #1239 I made some code to inspect the exact request

public class DiagnosticListenerObserver : IObserver<DiagnosticListener>
{
    private readonly IObserver<KeyValuePair<string, object>> _innerObserver;

    public DiagnosticListenerObserver(IObserver<KeyValuePair<string, object>> innerObserver)
    {
        _innerObserver = innerObserver;
    }

    public void OnNext(DiagnosticListener diagnosticListener)
    {
        if (diagnosticListener.Name == "HttpHandlerDiagnosticListener")
        {
            diagnosticListener.Subscribe(_innerObserver);
        }
    }

    public void OnError(Exception error)
    {
        // Handle errors here, if needed
    }

    public void OnCompleted()
    {
        // Handle completion, if needed
    }
}

public class HttpClientLoggingListener : IObserver<KeyValuePair<string, object>>
{
    public void OnNext(KeyValuePair<string, object> value)
    {
        if (value.Key == "System.Net.Http.HttpRequestOut.Start")
        {
            var request = (HttpRequestMessage)value.Value.GetType().GetProperty("Request")?.GetValue(value.Value);

            //Console.WriteLine($"Request: {request?.Method} {request?.RequestUri}");
            if (request.Method == HttpMethod.Post && request.RequestUri.ToString().Contains("openai.azure.com"))
            {
                Console.WriteLine($"POST Request to {request.RequestUri}");

                if (request.Content != null)
                {
                    // Read the body as a string
                    var contentTask = request.Content.ReadAsStringAsync();
                    string requestBody = contentTask.Result;    // CUIDADO! sync-over-async

                    Console.WriteLine("Request Body:");
                    Console.WriteLine(requestBody);
                }
                else
                {
                    Console.WriteLine("Request Body: <No Content>");
                }
            }
        }
    }

    public void OnError(Exception error) { }

    public void OnCompleted() { }
}

Then having 2 simple plugins

public class M365InvoicePlugin
{
    [KernelFunction()]
    [Description("Obtener facturas de Microsoft 365")]
    public string GetM365Invoices()
    {
        return "Facturas de Microsoft 365. Septiembre 2024:80€";
    }
}


public class AzureInvoicePlugin
{
    [KernelFunction]
    
    [Description("Obtener facturas de Azure")]
    [return:Description("Facturas de Azure del último año")] 
    public string GetAzureInvoices()
    {
        return "Facturas de Azure. Octubre 2024:100€";
    }
}

When invoking with chatCompletions (many lines shortened for brevity, I assume you get the idea)

var listener = new HttpClientLoggingListener();
DiagnosticListener.AllListeners.Subscribe(new DiagnosticListenerObserver(listener));

var builder = Kernel.CreateBuilder();
builder.Plugins.AddFromType<M365InvoicePlugin>();
builder.Plugins.AddFromType<AzureInvoicePlugin>()
// configure azure openai etc
var kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
var chat = new ChatHistory();
chat.AddUserMessage("Hola");
var result = await chatCompletionService.GetChatMessageContentAsync(
     chat,
     executionSettings: openAIPromptExecutionSettings,
     kernel: kernel);

The captured post does NOT show the text in the [return:Description("")] in the tools parameter:

{
    "messages": [
        {
            "role": "user",
            "content": "hola"
        }
    ],
    "model": "CENSORED",
    "temperature": 0.1,
    "top_p": 1,
    "tools": [
        {
            "type": "function",
            "function": {
                "description": "Obtener facturas de Microsoft 365",
                "name": "M365InvoicePlugin-GetM365Invoices",
                "parameters": {
                    "type": "object",
                    "required": [],
                    "properties": {}
                }
            }
        },
        {
            "type": "function",
            "function": {
                "description": "Obtener facturas de Azure",
                "name": "AzureInvoicePlugin-GetAzureInvoices",
                "parameters": {
                    "type": "object",
                    "required": [],
                    "properties": {}
                }
            }
        }
    ],
    "tool_choice": "auto"
}
@christianarg christianarg added the bug Something isn't working label Dec 18, 2024
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Dec 18, 2024
@github-actions github-actions bot changed the title Bug: .Net Plugin return[Description] being totaly ignored? .Net: Bug: .Net Plugin return[Description] being totaly ignored? Dec 18, 2024
@markwallace-microsoft markwallace-microsoft moved this from Bug to Sprint: Planned in Semantic Kernel Dec 19, 2024
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: Planned
Development

No branches or pull requests

3 participants