Skip to content

Commit 3859383

Browse files
author
Gunpal Jain
committed
fixed session initialization
1 parent 6a03b67 commit 3859383

File tree

4 files changed

+34
-53
lines changed

4 files changed

+34
-53
lines changed

Directory.Packages.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageVersion Include="EntityFramework" Version="6.4.4" />
9-
<PackageVersion Include="Google_GenerativeAI" Version="2.5.3" />
10-
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.3" />
9+
<PackageVersion Include="Google_GenerativeAI" Version="2.5.5" />
10+
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.5" />
1111
<PackageVersion Include="LLMSharp.Google.Palm" Version="1.0.2" />
1212
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(AspNetCoreVersion)" />
1313
<PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />
@@ -145,4 +145,4 @@
145145
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.25" />
146146
<PackageVersion Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.26" />
147147
</ItemGroup>
148-
</Project>
148+
</Project>

src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,28 @@ public async Task<RealtimeSession> CreateSession(Agent agent, List<RoleDialogMod
217217
systemInstruction: request.SystemInstruction?.Parts.FirstOrDefault()?.Text);
218218
_client.UseGoogleSearch = _settings.Gemini.UseGoogleSearch;
219219

220-
if (request.Tools != null && request.Tools.Count > 0)
220+
if (_settings.Gemini.UseGoogleSearch)
221221
{
222-
var lst = (request.Tools.Select(s => (IFunctionTool)new TemporaryFunctionTool(s)).ToList());
223-
224-
_client.AddFunctionTools(lst, new ToolConfig()
222+
if (request.Tools == null)
223+
request.Tools = new List<Tool>();
224+
request.Tools.Add(new Tool()
225225
{
226-
FunctionCallingConfig = new FunctionCallingConfig()
227-
{
228-
Mode = FunctionCallingMode.AUTO
229-
}
226+
GoogleSearch = new GoogleSearchTool()
230227
});
231228
}
232229

233230
await AttachEvents();
234231

235232
await _client.ConnectAsync();
236233

237-
_client.FunctionTools?.Clear();
238-
234+
await _client.SendSetupAsync(new BidiGenerateContentSetup()
235+
{
236+
GenerationConfig = config,
237+
Model = Model,
238+
SystemInstruction = request.SystemInstruction,
239+
Tools = request.Tools?.ToArray(),
240+
});
241+
239242
return new RealtimeSession()
240243
{
241244
Id = _client.ConnectionId.ToString(),
@@ -271,13 +274,14 @@ public async Task<string> UpdateSession(RealtimeHubConnection conn, bool interru
271274
config.Temperature = Math.Max(realtimeModelSettings.Temperature, 0.6f);
272275
config.MaxOutputTokens = realtimeModelSettings.MaxResponseOutputTokens;
273276
}
277+
274278

275279
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x =>
276280
{
277281
var fn = new FunctionDef
278282
{
279283
Name = x.Name ?? string.Empty,
280-
Description = x.Description?? string.Empty,
284+
Description = x.Description ?? string.Empty,
281285
};
282286
fn.Parameters = x.Parameters != null
283287
? JsonSerializer.Deserialize<FunctionParametersDef>(JsonSerializer.Serialize(x.Parameters))
@@ -287,6 +291,16 @@ public async Task<string> UpdateSession(RealtimeHubConnection conn, bool interru
287291

288292
await HookEmitter.Emit<IContentGeneratingHook>(_services,
289293
async hook => { await hook.OnSessionUpdated(agent, prompt, functions); });
294+
295+
if (_settings.Gemini.UseGoogleSearch)
296+
{
297+
if (request.Tools == null)
298+
request.Tools = new List<Tool>();
299+
request.Tools.Add(new Tool()
300+
{
301+
GoogleSearch = new GoogleSearchTool()
302+
});
303+
}
290304

291305
//ToDo: Not sure what's the purpose of UpdateSession, Google Realtime conversion works right away after sending the message!
292306

@@ -309,7 +323,7 @@ public async Task InsertConversationItem(RoleDialogModel message)
309323
{
310324
var function = new FunctionResponse()
311325
{
312-
Name = message.FunctionName?? string.Empty,
326+
Name = message.FunctionName ?? string.Empty,
313327
Response = JsonNode.Parse(message.Content ?? "{}")
314328
};
315329

@@ -323,11 +337,7 @@ await _client.SendToolResponseAsync(new BidiGenerateContentToolResponse()
323337
}
324338
else if (message.Role == AgentRole.User)
325339
{
326-
await _client.SendClientContentAsync(new BidiGenerateContentClientContent()
327-
{
328-
TurnComplete = true,
329-
Turns = [new Content(message.Content, AgentRole.User)]
330-
});
340+
await _client.SentTextAsync(message.Content);
331341
}
332342
else
333343
{
@@ -428,7 +438,7 @@ public Task<RoleDialogModel> OnConversationItemCreated(RealtimeHubConnection con
428438
{
429439
FunctionResponse = new FunctionResponse
430440
{
431-
Name = message.FunctionName?? string.Empty,
441+
Name = message.FunctionName ?? string.Empty,
432442
Response = new JsonObject()
433443
{
434444
["result"] = message.Content ?? string.Empty

src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/TemporaryFunctionTool.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/BotSharp.LLM.Tests/EmbeddingTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using BotSharp.Abstraction.Agents.Enums;
1+

2+
3+
using BotSharp.Abstraction.Agents.Enums;
24
using BotSharp.Abstraction.Agents.Models;
35
using BotSharp.Abstraction.Conversations.Models;
46
using BotSharp.Abstraction.MLTasks;
@@ -46,7 +48,6 @@ public static IEnumerable<object[]> CreateTestLLMProviders()
4648
(services, configuration, modelName) = LLMProvider.CreateOpenAI();
4749
yield return new object[] { services.BuildServiceProvider().GetService<ITextEmbedding>() ?? throw new Exception("Error while initializing"), agent, modelName };
4850
}
49-
5051
}
5152

5253

0 commit comments

Comments
 (0)