some code refactoring
This commit is contained in:
parent
f46737e118
commit
6a03b67e1c
|
|
@ -201,8 +201,6 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
|
|
||||||
public async Task<RealtimeSession> CreateSession(Agent agent, List<RoleDialogModel> conversations)
|
public async Task<RealtimeSession> CreateSession(Agent agent, List<RoleDialogModel> conversations)
|
||||||
{
|
{
|
||||||
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
|
|
||||||
|
|
||||||
var client = ProviderHelper.GetGeminiClient(Provider, _model, _services);
|
var client = ProviderHelper.GetGeminiClient(Provider, _model, _services);
|
||||||
var chatClient = client.CreateGenerativeModel(_model);
|
var chatClient = client.CreateGenerativeModel(_model);
|
||||||
var (prompt, request) = PrepareOptions(chatClient, agent, conversations);
|
var (prompt, request) = PrepareOptions(chatClient, agent, conversations);
|
||||||
|
|
@ -261,22 +259,25 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
|
|
||||||
var config = request.GenerationConfig;
|
var config = request.GenerationConfig;
|
||||||
//Output Modality can either be text or audio
|
//Output Modality can either be text or audio
|
||||||
config.ResponseModalities = new List<Modality>([Modality.AUDIO]);
|
if (config != null)
|
||||||
|
{
|
||||||
|
config.ResponseModalities = new List<Modality>([Modality.AUDIO]);
|
||||||
|
|
||||||
var words = new List<string>();
|
var words = new List<string>();
|
||||||
HookEmitter.Emit<IRealtimeHook>(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)));
|
HookEmitter.Emit<IRealtimeHook>(_services, hook => words.AddRange(hook.OnModelTranscriptPrompt(agent)));
|
||||||
|
|
||||||
var realtimeModelSettings = _services.GetRequiredService<RealtimeModelSettings>();
|
var realtimeModelSettings = _services.GetRequiredService<RealtimeModelSettings>();
|
||||||
|
|
||||||
config.Temperature = Math.Max(realtimeModelSettings.Temperature, 0.6f);
|
config.Temperature = Math.Max(realtimeModelSettings.Temperature, 0.6f);
|
||||||
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
|
||||||
{
|
{
|
||||||
Name = x.Name ?? string.Empty,
|
Name = x.Name ?? string.Empty,
|
||||||
Description = x.Description
|
Description = x.Description?? string.Empty,
|
||||||
};
|
};
|
||||||
fn.Parameters = x.Parameters != null
|
fn.Parameters = x.Parameters != null
|
||||||
? JsonSerializer.Deserialize<FunctionParametersDef>(JsonSerializer.Serialize(x.Parameters))
|
? JsonSerializer.Deserialize<FunctionParametersDef>(JsonSerializer.Serialize(x.Parameters))
|
||||||
|
|
@ -287,7 +288,7 @@ 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); });
|
||||||
|
|
||||||
//ToDo: Not sure what's the purpose of UpdateSession, Google Realtime conversion works right after sending the message away!
|
//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()
|
||||||
// {
|
// {
|
||||||
|
|
@ -302,11 +303,13 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
|
|
||||||
public async Task InsertConversationItem(RoleDialogModel message)
|
public async Task InsertConversationItem(RoleDialogModel message)
|
||||||
{
|
{
|
||||||
|
if (_client == null)
|
||||||
|
throw new Exception("Client is not initialized");
|
||||||
if (message.Role == AgentRole.Function)
|
if (message.Role == AgentRole.Function)
|
||||||
{
|
{
|
||||||
var function = new FunctionResponse()
|
var function = new FunctionResponse()
|
||||||
{
|
{
|
||||||
Name = message.FunctionName,
|
Name = message.FunctionName?? string.Empty,
|
||||||
Response = JsonNode.Parse(message.Content ?? "{}")
|
Response = JsonNode.Parse(message.Content ?? "{}")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -323,7 +326,7 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
await _client.SendClientContentAsync(new BidiGenerateContentClientContent()
|
await _client.SendClientContentAsync(new BidiGenerateContentClientContent()
|
||||||
{
|
{
|
||||||
TurnComplete = true,
|
TurnComplete = true,
|
||||||
Turns = new[] { new Content(message.Content, AgentRole.User) }
|
Turns = [new Content(message.Content, AgentRole.User)]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -332,15 +335,15 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<RoleDialogModel>> OnResponsedDone(RealtimeHubConnection conn, string response)
|
public Task<List<RoleDialogModel>> OnResponsedDone(RealtimeHubConnection conn, string response)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException("");
|
throw new NotImplementedException("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<RoleDialogModel> OnConversationItemCreated(RealtimeHubConnection conn, string response)
|
public Task<RoleDialogModel> OnConversationItemCreated(RealtimeHubConnection conn, string response)
|
||||||
{
|
{
|
||||||
return new RoleDialogModel(AgentRole.User, response);
|
return Task.FromResult(new RoleDialogModel(AgentRole.User, response));
|
||||||
}
|
}
|
||||||
|
|
||||||
private (string, GenerateContentRequest) PrepareOptions(GenerativeModel aiModel, Agent agent,
|
private (string, GenerateContentRequest) PrepareOptions(GenerativeModel aiModel, Agent agent,
|
||||||
|
|
@ -425,7 +428,7 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
{
|
{
|
||||||
FunctionResponse = new FunctionResponse
|
FunctionResponse = new FunctionResponse
|
||||||
{
|
{
|
||||||
Name = message.FunctionName,
|
Name = message.FunctionName?? string.Empty,
|
||||||
Response = new JsonObject()
|
Response = new JsonObject()
|
||||||
{
|
{
|
||||||
["result"] = message.Content ?? string.Empty
|
["result"] = message.Content ?? string.Empty
|
||||||
|
|
@ -476,9 +479,7 @@ namespace BotSharp.Plugin.GoogleAi.Providers.Realtime
|
||||||
private string GetPrompt(IEnumerable<string> systemPrompts, IEnumerable<string> funcPrompts,
|
private string GetPrompt(IEnumerable<string> systemPrompts, IEnumerable<string> funcPrompts,
|
||||||
IEnumerable<string> convPrompts)
|
IEnumerable<string> convPrompts)
|
||||||
{
|
{
|
||||||
var prompt = string.Empty;
|
string prompt = string.Join("\r\n\r\n", systemPrompts);
|
||||||
|
|
||||||
prompt = string.Join("\r\n\r\n", systemPrompts);
|
|
||||||
|
|
||||||
if (!funcPrompts.IsNullOrEmpty())
|
if (!funcPrompts.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue