fixed session initialization

This commit is contained in:
Gunpal Jain 2025-04-04 07:50:45 +05:30
parent 6a03b67e1c
commit 3859383f71
4 changed files with 34 additions and 53 deletions

View file

@ -6,8 +6,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageVersion Include="EntityFramework" Version="6.4.4" /> <PackageVersion Include="EntityFramework" Version="6.4.4" />
<PackageVersion Include="Google_GenerativeAI" Version="2.5.3" /> <PackageVersion Include="Google_GenerativeAI" Version="2.5.5" />
<PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.3" /> <PackageVersion Include="Google_GenerativeAI.Live" Version="2.5.5" />
<PackageVersion Include="LLMSharp.Google.Palm" Version="1.0.2" /> <PackageVersion Include="LLMSharp.Google.Palm" Version="1.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(AspNetCoreVersion)" /> <PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="$(AspNetCoreVersion)" />
<PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" /> <PackageVersion Include="Microsoft.AspNetCore.StaticFiles" Version="$(AspNetCoreVersion)" />

View file

@ -217,16 +217,13 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
systemInstruction: request.SystemInstruction?.Parts.FirstOrDefault()?.Text); systemInstruction: request.SystemInstruction?.Parts.FirstOrDefault()?.Text);
_client.UseGoogleSearch = _settings.Gemini.UseGoogleSearch; _client.UseGoogleSearch = _settings.Gemini.UseGoogleSearch;
if (request.Tools != null && request.Tools.Count > 0) if (_settings.Gemini.UseGoogleSearch)
{ {
var lst = (request.Tools.Select(s => (IFunctionTool)new TemporaryFunctionTool(s)).ToList()); if (request.Tools == null)
request.Tools = new List<Tool>();
_client.AddFunctionTools(lst, new ToolConfig() request.Tools.Add(new Tool()
{ {
FunctionCallingConfig = new FunctionCallingConfig() GoogleSearch = new GoogleSearchTool()
{
Mode = FunctionCallingMode.AUTO
}
}); });
} }
@ -234,7 +231,13 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
await _client.ConnectAsync(); await _client.ConnectAsync();
_client.FunctionTools?.Clear(); await _client.SendSetupAsync(new BidiGenerateContentSetup()
{
GenerationConfig = config,
Model = Model,
SystemInstruction = request.SystemInstruction,
Tools = request.Tools?.ToArray(),
});
return new RealtimeSession() return new RealtimeSession()
{ {
@ -272,6 +275,7 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
config.MaxOutputTokens = realtimeModelSettings.MaxResponseOutputTokens; config.MaxOutputTokens = realtimeModelSettings.MaxResponseOutputTokens;
} }
var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x => var functions = request.Tools?.SelectMany(s => s.FunctionDeclarations).Select(x =>
{ {
var fn = new FunctionDef var fn = new FunctionDef
@ -288,6 +292,16 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
await HookEmitter.Emit<IContentGeneratingHook>(_services, await HookEmitter.Emit<IContentGeneratingHook>(_services,
async hook => { await hook.OnSessionUpdated(agent, prompt, functions); }); async hook => { await hook.OnSessionUpdated(agent, prompt, functions); });
if (_settings.Gemini.UseGoogleSearch)
{
if (request.Tools == null)
request.Tools = new List<Tool>();
request.Tools.Add(new Tool()
{
GoogleSearch = new GoogleSearchTool()
});
}
//ToDo: Not sure what's the purpose of UpdateSession, Google Realtime conversion works right away after sending the message! //ToDo: Not sure what's the purpose of UpdateSession, Google Realtime conversion works right away after sending the message!
// await _client.SendSetupAsync(new BidiGenerateContentSetup() // await _client.SendSetupAsync(new BidiGenerateContentSetup()
@ -323,11 +337,7 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
} }
else if (message.Role == AgentRole.User) else if (message.Role == AgentRole.User)
{ {
await _client.SendClientContentAsync(new BidiGenerateContentClientContent() await _client.SentTextAsync(message.Content);
{
TurnComplete = true,
Turns = [new Content(message.Content, AgentRole.User)]
});
} }
else else
{ {

View file

@ -1,30 +0,0 @@
using System.Threading;
using GenerativeAI.Core;
using GenerativeAI.Types;
namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
{
public class TemporaryFunctionTool:IFunctionTool
{
public Tool Tool { get; set; }
public TemporaryFunctionTool(Tool tool)
{
this.Tool = tool;
}
public Tool AsTool()
{
return Tool;
}
public async Task<FunctionResponse?> CallAsync(FunctionCall functionCall, CancellationToken cancellationToken = new CancellationToken())
{
return null;
}
public bool IsContainFunction(string name)
{
return false;
}
}
}

View file

@ -1,4 +1,6 @@
using BotSharp.Abstraction.Agents.Enums; 
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.MLTasks;
@ -46,7 +48,6 @@ namespace BotSharp.Plugin.Google.Core
(services, configuration, modelName) = LLMProvider.CreateOpenAI(); (services, configuration, modelName) = LLMProvider.CreateOpenAI();
yield return new object[] { services.BuildServiceProvider().GetService<ITextEmbedding>() ?? throw new Exception("Error while initializing"), agent, modelName }; yield return new object[] { services.BuildServiceProvider().GetService<ITextEmbedding>() ?? throw new Exception("Error while initializing"), agent, modelName };
} }
} }