Skip to content

add gemini #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class AgentRole
public const string Assistant = "assistant";
public const string User = "user";
public const string Function = "function";
public const string Model = "model";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="LLMSharp.Google.Palm" Version="1.0.2" />
<PackageReference Include="Mscc.GenerativeAI" Version="2.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 7 additions & 5 deletions src/Plugins/BotSharp.Plugin.GoogleAI/GoogleAiPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using BotSharp.Abstraction.Plugins;
using BotSharp.Abstraction.Settings;
using BotSharp.Plugin.GoogleAI.Providers;
using BotSharp.Plugin.GoogleAI.Settings;
using BotSharp.Plugin.GoogleAi.Providers.Chat;
using BotSharp.Plugin.GoogleAi.Providers.Text;

namespace BotSharp.Plugin.GoogleAI;
namespace BotSharp.Plugin.GoogleAi;

public class GoogleAiPlugin : IBotSharpPlugin
{
Expand All @@ -19,7 +19,9 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
return settingService.Bind<GoogleAiSettings>("GoogleAi");
});

services.AddScoped<IChatCompletion, ChatCompletionProvider>();
services.AddScoped<ITextCompletion, TextCompletionProvider>();
services.AddScoped<ITextCompletion, PalmTextCompletionProvider>();
services.AddScoped<ITextCompletion, GeminiTextCompletionProvider>();
services.AddScoped<IChatCompletion, PalmChatCompletionProvider>();
services.AddScoped<IChatCompletion, GeminiChatCompletionProvider>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Loggers;
using Microsoft.Extensions.Logging;
using Mscc.GenerativeAI;

namespace BotSharp.Plugin.GoogleAi.Providers.Chat;

public class GeminiChatCompletionProvider : IChatCompletion
{
private readonly IServiceProvider _services;
private readonly ILogger<GeminiChatCompletionProvider> _logger;

private string _model;

public string Provider => "google-gemini";

public GeminiChatCompletionProvider(
IServiceProvider services,
ILogger<GeminiChatCompletionProvider> logger)
{
_services = services;
_logger = logger;
}

public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();

// Before chat completion hook
foreach (var hook in contentHooks)
{
await hook.BeforeGenerating(agent, conversations);
}

var client = ProviderHelper.GetGeminiClient(_services);
var aiModel = client.GenerativeModel(_model);
var (prompt, request) = PrepareOptions(aiModel, agent, conversations);

var response = await aiModel.GenerateContent(request);
var candidate = response.Candidates.First();
var part = candidate.Content?.Parts?.FirstOrDefault();
var text = part?.Text ?? string.Empty;

RoleDialogModel responseMessage;
if (part?.FunctionCall != null)
{
responseMessage = new RoleDialogModel(AgentRole.Function, text)
{
CurrentAgentId = agent.Id,
MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
ToolCallId = part.FunctionCall.Name,
FunctionName = part.FunctionCall.Name,
FunctionArgs = part.FunctionCall.Args?.ToString()
};
}
else
{
responseMessage = new RoleDialogModel(AgentRole.Assistant, text)
{
CurrentAgentId = agent.Id,
MessageId = conversations.LastOrDefault()?.MessageId ?? string.Empty,
};
}

// After chat completion hook
foreach (var hook in contentHooks)
{
await hook.AfterGenerated(responseMessage, new TokenStatsModel
{
Prompt = prompt,
Provider = Provider,
Model = _model
});
}

return responseMessage;
}

public Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onFunctionExecuting)
{
throw new NotImplementedException();
}

public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
{
throw new NotImplementedException();
}

public void SetModelName(string model)
{
_model = model;
}

private (string, GenerateContentRequest) PrepareOptions(GenerativeModel aiModel, Agent agent, List<RoleDialogModel> conversations)
{
var agentService = _services.GetRequiredService<IAgentService>();
var googleSettings = _services.GetRequiredService<GoogleAiSettings>();

// Add settings
aiModel.UseGoogleSearch = googleSettings.Gemini.UseGoogleSearch;
aiModel.UseGrounding = googleSettings.Gemini.UseGrounding;

// Assembly messages
var prompt = string.Empty;
var contents = new List<Content>();
var tools = new List<Tool>();
var funcDeclarations = new List<FunctionDeclaration>();

if (!string.IsNullOrEmpty(agent.Instruction))
{
var instruction = agentService.RenderedInstruction(agent);
contents.Add(new Content(instruction)
{
Role = AgentRole.User
});

prompt += $"{instruction}\r\n";
}

prompt += "\r\n[FUNCTIONS]\r\n";
foreach (var function in agent.Functions)
{
if (!agentService.RenderFunction(agent, function)) continue;

var def = agentService.RenderFunctionProperty(agent, function);

funcDeclarations.Add(new FunctionDeclaration
{
Name = function.Name,
Description = function.Description,
Parameters = new()
{
Type = ParameterType.Object,
Properties = def.Properties,
Required = def.Required
}
});

prompt += $"{function.Name}: {function.Description} {def}\r\n\r\n";
}

if (!funcDeclarations.IsNullOrEmpty())
{
tools.Add(new Tool { FunctionDeclarations = funcDeclarations });
}

prompt += "\r\n[CONVERSATIONS]\r\n";
foreach (var message in conversations)
{
if (message.Role == AgentRole.Function)
{
contents.Add(new Content(message.Content)
{
Role = AgentRole.Function,
Parts = new()
{
new FunctionCall
{
Name = message.FunctionName,
Args = JsonSerializer.Deserialize<object>(message.FunctionArgs ?? "{}")
}
}
});

prompt += $"{AgentRole.Assistant}: Call function {message.FunctionName}({message.FunctionArgs})\r\n";
}
else if (message.Role == AgentRole.User)
{
var text = !string.IsNullOrWhiteSpace(message.Payload) ? message.Payload : message.Content;
contents.Add(new Content(text)
{
Role = AgentRole.User
});
prompt += $"{AgentRole.User}: {text}\r\n";
}
else if (message.Role == AgentRole.Assistant)
{
contents.Add(new Content(message.Content)
{
Role = AgentRole.Model
});
prompt += $"{AgentRole.Assistant}: {message.Content}\r\n";
}
}

var request = new GenerateContentRequest
{
Contents = contents,
Tools = tools
};
return (prompt, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing;
using BotSharp.Plugin.GoogleAI.Settings;
using LLMSharp.Google.Palm;
using Microsoft.Extensions.Logging;
using LLMSharp.Google.Palm.DiscussService;
using Microsoft.Extensions.Logging;

namespace BotSharp.Plugin.GoogleAI.Providers;
namespace BotSharp.Plugin.GoogleAi.Providers.Chat;

public class ChatCompletionProvider : IChatCompletion
public class PalmChatCompletionProvider : IChatCompletion
{
public string Provider => "google-ai";
private readonly IServiceProvider _services;
private readonly GoogleAiSettings _settings;
private readonly ILogger _logger;
private readonly ILogger<PalmChatCompletionProvider> _logger;

private string _model;

public ChatCompletionProvider(IServiceProvider services,
GoogleAiSettings settings,
ILogger<ChatCompletionProvider> logger)
public string Provider => "google-ai";

public PalmChatCompletionProvider(
IServiceProvider services,
ILogger<PalmChatCompletionProvider> logger)
{
_services = services;
_settings = settings;
_logger = logger;
}

public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();

// Before chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.BeforeGenerating(agent, conversations)).ToArray());

var client = new GooglePalmClient(apiKey: _settings.PaLM.ApiKey);
foreach (var hook in contentHooks)
{
await hook.BeforeGenerating(agent, conversations);
}

var client = ProviderHelper.GetPalmClient(_services);
var (prompt, messages, hasFunctions) = PrepareOptions(agent, conversations);

RoleDialogModel msg;

if (hasFunctions)
{
// use text completion
Expand Down Expand Up @@ -80,12 +80,15 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
}

// After chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.AfterGenerated(msg, new TokenStatsModel
foreach (var hook in contentHooks)
{
await hook.AfterGenerated(msg, new TokenStatsModel
{
Prompt = prompt,
Provider = Provider,
Model = _model
})).ToArray());
});
}

return msg;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using LLMSharp.Google.Palm;
using Mscc.GenerativeAI;

namespace BotSharp.Plugin.GoogleAi.Providers;

public static class ProviderHelper
{
public static GoogleAI GetGeminiClient(IServiceProvider services)
{
var settings = services.GetRequiredService<GoogleAiSettings>();
var client = new GoogleAI(settings.Gemini.ApiKey);
return client;
}

public static GooglePalmClient GetPalmClient(IServiceProvider services)
{
var settings = services.GetRequiredService<GoogleAiSettings>();
var client = new GooglePalmClient(settings.PaLM.ApiKey);
return client;
}
}
Loading