Skip to content

.Net: IChatClient + Add FunctionChoiceBehavior to Google Connector #10223

Closed
@PraveenVerma17

Description

@PraveenVerma17

Describe the bug
kernel function call is not working properly with gemini-1.5-flash for below code

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Google;
using SementicKernelDemo.Plugins.LightsPlugin;



string? modelID = Environment.GetEnvironmentVariable("MODEL_ID");
string? apiKey = Environment.GetEnvironmentVariable("API_KEY");

if (string.IsNullOrEmpty(modelID) || string.IsNullOrEmpty(apiKey))
    throw new Exception("Please set the MODEL_ID and API_KEY environment variables to your Google AI Gemini model ID and API key, respectively.");

// create kernel
#pragma warning disable SKEXP0070 
var kernel = Kernel.CreateBuilder()
                    .AddGoogleAIGeminiChatCompletion(modelID, apiKey, GoogleAIVersion.V1)
                    .Build();



// Add plugins
kernel.Plugins.AddFromType<LightsPlugin>("Lights");


//Enable Prompt Execution Settings 
GeminiPromptExecutionSettings geminiPromptExecutionSettings = new() {
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
    ToolCallBehavior = GeminiToolCallBehavior.EnableKernelFunctions
};



var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();

string? userInput;
do
{
    Console.WriteLine("User >");
    userInput = Console.ReadLine();
    try
    {
        history.AddUserMessage(userInput);
        var result = await chatCompletionService.GetChatMessageContentAsync(
            history,
            executionSettings: geminiPromptExecutionSettings,
            kernel: kernel);

        Console.WriteLine("Assistant > " + result);
        history.AddMessage(result.Role, result.Content ?? string.Empty);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
} while (userInput is not null);


#pragma warning restore SKEXP0070

To Reproduce
Steps to reproduce the behavior:
Execute the above code its failing when ToolCallBehavior = GeminiToolCallBehavior.EnableKernelFunctions or ToolCallBehavior = GeminiToolCallBehavior.AutoInvokeKernelFunctions is set. if i don't set it does not recognize kernel functions.

Expected behavior
It should recognize kernel functions and return the result back from my plugin

Screenshots

Platform

  • OS: Windows
  • IDE: Visual Studio
  • Language: C#
  • Source: microsoft.sementic.kernel and connector.google

Additional context

Metadata

Metadata

Assignees

Labels

.NETIssue or Pull requests regarding .NET codeblocked externalThis item is blocked due to an external dependency that needs to be addressedbugSomething isn't working

Projects

Status

Sprint: Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions