Skip to content

.Net: SK not working correctly with gemini #12702

@ShreyasJejurkar

Description

@ShreyasJejurkar

I am trying SK for the first time and as Gemini is free, so I am trying gemini models.
Below is the code that I am trying.

using System.ComponentModel;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;

const string api_key = "__API_KEY__";
const string model = "gemini-2.5-pro";

var builder = Kernel.CreateBuilder()
    .AddGoogleAIGeminiChatCompletion(model, api_key);

var kernel = builder.Build();

kernel.Plugins.Add(KernelPluginFactory.CreateFromType<LightsController>());

ChatCompletionAgent agent = new()
{
    Kernel = kernel,
    Name = "Lights Agent",
    Description = "An agent that can provide information about available lights.",
    Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()})
};

await foreach (var response in agent.InvokeAsync("Get a list of available lights"))
{
    Console.WriteLine(response.Message);
}

public class LightsController
{
    private readonly Dictionary<int,string> _lights = new()
    {
        { 1, "Table Lamp" },
        { 2, "Floor Lamp" },
        { 3, "Ceiling Light" }
    };
    
    [KernelFunction]
    [Description("Get a list of available lights")]
    public string GetLights()
    {
        return string.Join(", ", _lights.Select(kv => $"{kv.Key}: {kv.Value}"));
    }
}

And I am getting the output below.
Of course. Here is a list of your available lights and their current status:

### **Living Room**
*   **Living Room Lamp**
    *   **Status:** On
    *   **Brightness:** 70%
*   **Ceiling Fan Light**
    *   **Status:** Off

### **Kitchen**
*   **Kitchen Downlights** (Group)
    *   **Status:** On
    *   **Brightness:** 100%
*   **Under Cabinet Lights**
    *   **Status:** On
    *   **Brightness:** 80%

### **Bedroom**
*   **Bedroom Mood Light**
    *   **Status:** On
    *   **Brightness:** 40%
    *   **Color:** Warm White
*   **Bedside Lamp**
    *   **Status:** Off

### **Office**
*   **Office Desk Lamp**
    *   **Status:** Off

You can control these by name, such as *"Turn off the Living Room Lamp"* or *"Set the Bedroom Mood Light to blue."*

If you see output, it's not matching with the lights that I have in my code. Looks like hallucination.

Metadata

Metadata

Labels

.NETIssue or Pull requests regarding .NET codestaleIssue is stale because it has been open for a while and has no activity

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions