Commit 4a09cbd
.Net: Add FunctionChoiceBehavior support to Google Gemini connector (#13256)
- [x] Understand the current Google connector implementation and
identify what needs to be changed
- [x] Add FunctionChoiceBehavior support to
GeminiPromptExecutionSettings
- [x] Update GeminiChatCompletionClient to handle FunctionChoiceBehavior
- [x] Map FunctionChoiceBehavior to GeminiToolCallBehavior internally
- [x] Populate Items collection with FunctionCallContent and
FunctionResultContent
- [x] Build and test the changes (all 326 unit tests passing)
- [x] Address code review comments (optimize Kernel creation, improve
exception handling)
- [x] Replace integration tests with unit tests following existing
patterns
- [x] Update tests to use only IChatClient methods
- [x] Use static readonly field for shared Kernel instance
- [x] Remove unnecessary using directives
- [x] Request final code review
## Summary
This PR enables the standard `FunctionChoiceBehavior` API to work with
Google Gemini models, fixing the hallucination issue reported in the
original issue. Users can now use:
```csharp
var settings = new GeminiPromptExecutionSettings
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
```
Instead of the Gemini-specific:
```csharp
var settings = new GeminiPromptExecutionSettings
{
ToolCallBehavior = GeminiToolCallBehavior.AutoInvokeKernelFunctions
};
```
The connector automatically converts `FunctionChoiceBehavior` to the
appropriate `GeminiToolCallBehavior` internally, while maintaining full
backward compatibility with existing code.
## Tests
Added comprehensive unit tests in
`GeminiChatClientFunctionCallingTests.cs` that use **only IChatClient
methods** to verify:
- Conversion from IChatCompletionService to IChatClient using
AsChatClient()
- Receiving function calls in responses via IChatClient
- Streaming responses via IChatClient
- Basic message sending and receiving via IChatClient
All 326 unit tests passing (322 existing + 4 new IChatClient tests).
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>.Net: SK not working correctly with gemini</issue_title>
> <issue_description>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.
>
> ```csharp
> 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.
>
>
> </issue_description>
>
> <agent_instructions>There are multiple related issues here, including:
> #12702
> #12998
> #12998
>
> The gemini / google connector needs to be updated to support the
function calling abstraction, including ToolChoiceBehavior,
FunctionCallContent, FunctionResultContent, etc. </agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@rogerbarreto</author><body>
> Function calling is not yet fully supported (multiple function calls)
in the Google Connector.
>
> For best experience of function calling as of now as @futugyou rightly
mentioned is using their compatible OpenAI endpoints with our `OpenAI`
Connector.
>
> More info here:
>
> https://ai.google.dev/gemini-api/docs/openai
>
>
https://cloud.google.com/vertex-ai/generative-ai/docs/migrate/openai/overview</body></comment_new>
> </comments>
>
</details>
Fixes #12702
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ) to start
the survey.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>1 parent 0a9ee40 commit 4a09cbd
4 files changed
Lines changed: 392 additions & 20 deletions
File tree
- dotnet/src/Connectors
- Connectors.Google.UnitTests
- Core/Gemini/Clients
- Connectors.Google
- Models/Gemini
Lines changed: 176 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
Lines changed: 105 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
197 | 302 | | |
198 | 303 | | |
199 | 304 | | |
| |||
Lines changed: 66 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
| |||
357 | 360 | | |
358 | 361 | | |
359 | 362 | | |
360 | | - | |
361 | | - | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
362 | 370 | | |
363 | 371 | | |
364 | 372 | | |
365 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
366 | 429 | | |
367 | 430 | | |
0 commit comments