Skip to content

Commit a6ffbc3

Browse files
authored
Merge pull request #738 from danijerez/feat/provider_vertexai
add plugin provider vertexai
2 parents 23aae24 + 5870bd4 commit a6ffbc3

File tree

5 files changed

+196
-1
lines changed

5 files changed

+196
-1
lines changed

BotSharp.sln

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Graph", "sr
117117
EndProject
118118
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.AudioHandler", "src\Plugins\BotSharp.Plugin.AudioHandler\BotSharp.Plugin.AudioHandler.csproj", "{F57F4862-F8D4-44A1-AC12-5C131B5C9785}"
119119
EndProject
120-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.SideCar", "src\Infrastructure\BotSharp.Core.SideCar\BotSharp.Core.SideCar.csproj", "{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE}"
120+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Core.SideCar", "src\Infrastructure\BotSharp.Core.SideCar\BotSharp.Core.SideCar.csproj", "{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE}"
121+
EndProject
122+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.VertexAI", "src\Plugins\BotSharp.Plugin.LangChain\BotSharp.Plugin.VertexAI.csproj", "{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}"
121123
EndProject
122124
Global
123125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -479,6 +481,14 @@ Global
479481
{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE}.Release|Any CPU.Build.0 = Release|Any CPU
480482
{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE}.Release|x64.ActiveCfg = Release|Any CPU
481483
{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE}.Release|x64.Build.0 = Release|Any CPU
484+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
485+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Debug|Any CPU.Build.0 = Debug|Any CPU
486+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Debug|x64.ActiveCfg = Debug|Any CPU
487+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Debug|x64.Build.0 = Debug|Any CPU
488+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|Any CPU.ActiveCfg = Release|Any CPU
489+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|Any CPU.Build.0 = Release|Any CPU
490+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|x64.ActiveCfg = Release|Any CPU
491+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|x64.Build.0 = Release|Any CPU
482492
EndGlobalSection
483493
GlobalSection(SolutionProperties) = preSolution
484494
HideSolutionNode = FALSE
@@ -536,6 +546,7 @@ Global
536546
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D} = {97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1}
537547
{F57F4862-F8D4-44A1-AC12-5C131B5C9785} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
538548
{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
549+
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
539550
EndGlobalSection
540551
GlobalSection(ExtensibilityGlobals) = postSolution
541552
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(TargetFramework)</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<LangVersion>$(LangVersion)</LangVersion>
7+
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
8+
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
10+
<OutputPath>$(SolutionDir)packages</OutputPath>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="LangChain.Providers.Google.VertexAI" Version="0.15.3-dev.58" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using BotSharp.Abstraction.Agents.Enums;
2+
using BotSharp.Abstraction.Agents.Models;
3+
using BotSharp.Abstraction.Conversations.Models;
4+
using BotSharp.Abstraction.Loggers;
5+
using BotSharp.Abstraction.MLTasks;
6+
using LangChain.Providers;
7+
using LangChain.Providers.Google.VertexAI;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
using System.Threading.Tasks;
14+
15+
namespace BotSharp.Plugin.VertexAI.Providers
16+
{
17+
public class ChatCompletionProvider(VertexAIConfiguration config,
18+
ChatSettings settings,
19+
ILogger<TextCompletionProvider> logger,
20+
IServiceProvider services) : IChatCompletion
21+
{
22+
public string Provider => "vertexai";
23+
private readonly VertexAIConfiguration _config = config;
24+
private readonly ChatSettings? _settings = settings;
25+
private readonly IServiceProvider _services = services;
26+
private readonly ILogger _logger = logger;
27+
public required string _model;
28+
29+
public void SetModelName(string model)
30+
{
31+
_model = model;
32+
}
33+
34+
public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
35+
{
36+
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
37+
Task.WaitAll(hooks.Select(hook =>
38+
hook.BeforeGenerating(agent, conversations)).ToArray());
39+
var client = new VertexAIProvider(_config);
40+
var model = new VertexAIChatModel(client, _model);
41+
var messages = conversations
42+
.Select(c => new Message(c.Content, c.Role == AgentRole.User ? MessageRole.Human : MessageRole.Ai)).ToList();
43+
44+
var response = await model.GenerateAsync(new ChatRequest { Messages = messages }, _settings);
45+
46+
var msg = new RoleDialogModel(MessageRole.Ai.ToString(), response.LastMessageContent)
47+
{
48+
CurrentAgentId = agent.Id
49+
};
50+
51+
Task.WaitAll(hooks.Select(hook =>
52+
hook.AfterGenerated(msg, new TokenStatsModel
53+
{
54+
Prompt = response.Messages[0].Content,
55+
Model = _model
56+
})).ToArray());
57+
58+
return msg;
59+
}
60+
61+
public Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onFunctionExecuting)
62+
{
63+
throw new NotImplementedException();
64+
}
65+
66+
public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
67+
{
68+
throw new NotImplementedException();
69+
}
70+
}
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using BotSharp.Abstraction.Agents.Enums;
2+
using BotSharp.Abstraction.Agents.Models;
3+
using BotSharp.Abstraction.Conversations.Models;
4+
using BotSharp.Abstraction.Loggers;
5+
using BotSharp.Abstraction.MLTasks;
6+
using LangChain.Providers;
7+
using LangChain.Providers.Google.VertexAI;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using System;
11+
using System.Linq;
12+
using System.Threading.Tasks;
13+
14+
namespace BotSharp.Plugin.VertexAI.Providers
15+
{
16+
public class TextCompletionProvider(VertexAIConfiguration config,
17+
ChatSettings settings,
18+
ILogger<TextCompletionProvider> logger,
19+
IServiceProvider services) : ITextCompletion
20+
{
21+
public string Provider => "vertexai";
22+
private readonly VertexAIConfiguration _config = config;
23+
private readonly ChatSettings? _settings = settings;
24+
private readonly IServiceProvider _services = services;
25+
private readonly ILogger _logger = logger;
26+
public required string _model;
27+
28+
public async Task<string> GetCompletion(string text, string agentId, string messageId)
29+
{
30+
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
31+
var agent = new Agent()
32+
{
33+
Id = agentId,
34+
};
35+
36+
var client = new VertexAIProvider(_config);
37+
var model = new VertexAIChatModel(client, _model);
38+
var response = await model.GenerateAsync(text, _settings);
39+
40+
var responseMessage = new RoleDialogModel(AgentRole.Assistant, response.LastMessageContent)
41+
{
42+
CurrentAgentId = agentId,
43+
MessageId = messageId
44+
};
45+
46+
Task.WaitAll(contentHooks.Select(hook =>
47+
hook.AfterGenerated(responseMessage, new TokenStatsModel
48+
{
49+
Prompt = text,
50+
Provider = Provider,
51+
Model = _model,
52+
PromptCount = response.Usage.TotalTokens,
53+
CompletionCount = response.Usage.OutputTokens
54+
})).ToArray());
55+
56+
return response.LastMessageContent;
57+
}
58+
59+
public void SetModelName(string model)
60+
{
61+
_model = model;
62+
}
63+
}
64+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using BotSharp.Abstraction.MLTasks;
2+
using BotSharp.Abstraction.Plugins;
3+
using BotSharp.Abstraction.Settings;
4+
using BotSharp.Plugin.VertexAI.Providers;
5+
using LangChain.Providers.Google.VertexAI;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace BotSharp.Plugin.VertexAI;
10+
11+
public class VertexAiPlugin : IBotSharpPlugin
12+
{
13+
public string Id => "962ff441-2b40-4db4-b530-49efb1688a75";
14+
public string Name => "VertexAI";
15+
public string Description => "VertexAI Service including text generation, text to image and other AI services.";
16+
public string IconUrl => "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Vertex_AI_Logo.svg/480px-Vertex_AI_Logo.svg.png";
17+
18+
public void RegisterDI(IServiceCollection services, IConfiguration config)
19+
{
20+
services.AddScoped(provider =>
21+
{
22+
var settingService = provider.GetRequiredService<ISettingService>();
23+
return settingService.Bind<VertexAIConfiguration>("VertexAI");
24+
});
25+
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
26+
services.AddScoped<ITextCompletion, TextCompletionProvider>();
27+
}
28+
}

0 commit comments

Comments
 (0)