Add Google PaLM 2.

This commit is contained in:
Haiping Chen 2023-10-08 15:46:42 -05:00
parent b7d19ee789
commit 0cd0b32e9a
17 changed files with 200 additions and 9 deletions

View file

@ -61,6 +61,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DataStorages", "DataStorage
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MongoStorage", "src\Plugins\BotSharp.Plugin.MongoStorage\BotSharp.Plugin.MongoStorage.csproj", "{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.GoogleAI", "src\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj", "{8BC29F8A-78D6-422C-B522-10687ADC38ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -213,6 +215,14 @@ Global
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C}.Release|Any CPU.Build.0 = Release|Any CPU
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C}.Release|x64.ActiveCfg = Release|Any CPU
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C}.Release|x64.Build.0 = Release|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Debug|x64.ActiveCfg = Debug|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Debug|x64.Build.0 = Debug|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|Any CPU.Build.0 = Release|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.ActiveCfg = Release|Any CPU
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -243,6 +253,7 @@ Global
{298AC787-A104-414C-B114-82BE764FBD9C} = {4F346DCE-087F-4368-AF88-EE9C720D0E69}
{5CD330E1-9E5A-4112-8346-6E31CA98EF78} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78}
{8BC29F8A-78D6-422C-B522-10687ADC38ED} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

View file

@ -2,5 +2,16 @@ namespace BotSharp.Abstraction.MLTasks;
public interface ITextCompletion
{
/// <summary>
/// The LLM provider like Microsoft Azure, OpenAI, ClaudAI
/// </summary>
string Provider { get; }
/// <summary>
/// Set model name, one provider can consume different model or version(s)
/// </summary>
/// <param name="model"></param>
void SetModelName(string model);
Task<string> GetCompletion(string text);
}

View file

@ -18,9 +18,12 @@ public partial class RoutingService
});
var content = $"{prompt} Response must be in JSON format {responseFormat}";
var state = _services.GetRequiredService<IConversationStateService>();
var provider = state.GetState("provider", _settings.Provider);
var model = state.GetState("model", _settings.Model);
var chatCompletion = CompletionProvider.GetChatCompletion(_services,
provider: _settings.Provider,
model: _settings.Model);
provider: provider,
model: model);
var response = chatCompletion.GetChatCompletions(_routerInstance.Router, new List<RoleDialogModel>
{

View file

@ -76,7 +76,7 @@ public partial class RoutingService : IRoutingService
{
loopCount++;
var prompt = _settings.EnableReasoning ? "Tell me the next step?" : "Which agent is suitable to handle user's request?";
var prompt = _settings.EnableReasoning ? "Tell me the next step?" : "Which agent is suitable to handle user's request based on the CONVERSATION?";
prompt += " Or you can handle without asking specific agent.";
var inst = await GetNextInstruction(prompt);
inst.Question = inst.Question ?? message;

View file

@ -5,7 +5,6 @@ using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Conversations.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Plugin.AzureOpenAI.Settings;
using Microsoft.Extensions.DependencyInjection;
@ -25,7 +24,7 @@ public class ChatCompletionProvider : IChatCompletion
private readonly ITokenStatistics _tokenStatistics;
private string _model;
public virtual string Provider => "azure-openai";
public string Provider => "azure-openai";
public ChatCompletionProvider(AzureOpenAiSettings settings,
ILogger<ChatCompletionProvider> logger,

View file

@ -13,6 +13,8 @@ public class TextCompletionProvider : ITextCompletion
private readonly AzureOpenAiSettings _settings;
private readonly ILogger _logger;
bool _useAzureOpenAI = true;
private string _model;
public string Provider => "azure-openai";
public TextCompletionProvider(AzureOpenAiSettings settings, ILogger<TextCompletionProvider> logger)
{
@ -49,6 +51,11 @@ public class TextCompletionProvider : ITextCompletion
return completion.Trim();
}
public void SetModelName(string model)
{
_model = model;
}
private OpenAIClient GetOpenAIClient()
{
OpenAIClient client = _useAzureOpenAI

View file

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LLMSharp.Google.Palm" Version="1.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,22 @@
using BotSharp.Abstraction.Plugins;
using BotSharp.Plugin.GoogleAI.Providers;
using BotSharp.Plugin.GoogleAI.Settings;
namespace BotSharp.Plugin.GoogleAI;
public class GoogleAiPlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
var settings = new GoogleAiSettings();
config.Bind("GoogleAi", settings);
services.AddSingleton(x =>
{
Console.WriteLine($"Loaded Google AI settings: {settings.PaLM.Endpoint} {settings.PaLM.ApiKey.SubstringMax(4)}");
return settings;
});
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
services.AddScoped<ITextCompletion, TextCompletionProvider>();
}
}

View file

@ -0,0 +1,63 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations;
using BotSharp.Plugin.GoogleAI.Settings;
using LLMSharp.Google.Palm;
using Microsoft.Extensions.Logging;
namespace BotSharp.Plugin.GoogleAI.Providers;
public class ChatCompletionProvider : IChatCompletion
{
public string Provider => "google-ai";
private readonly IServiceProvider _services;
private readonly GoogleAiSettings _settings;
private readonly ILogger _logger;
private readonly ITokenStatistics _tokenStatistics;
private string _model;
public ChatCompletionProvider(IServiceProvider services,
GoogleAiSettings settings,
ILogger<ChatCompletionProvider> logger,
ITokenStatistics tokenStatistics)
{
_services = services;
_settings = settings;
_logger = logger;
_tokenStatistics = tokenStatistics;
}
public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var client = new GooglePalmClient(apiKey: _settings.PaLM.ApiKey);
List<PalmChatMessage> messages = new()
{
new(conversations.Last().Content, "user"),
};
_tokenStatistics.StartTimer();
var response = client.ChatAsync(messages, agent.Instruction, null).Result;
_tokenStatistics.StopTimer();
var message = response.Candidates.First();
var msg = new RoleDialogModel(AgentRole.Assistant, message.Content)
{
CurrentAgentId = agent.Id
};
return msg;
}
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;
}
}

View file

@ -0,0 +1,17 @@
namespace BotSharp.Plugin.GoogleAI.Providers;
public class TextCompletionProvider : ITextCompletion
{
public string Provider => "google-ai";
private string _model;
public Task<string> GetCompletion(string text)
{
throw new NotImplementedException();
}
public void SetModelName(string model)
{
_model = model;
}
}

View file

@ -0,0 +1,6 @@
namespace BotSharp.Plugin.GoogleAI.Settings;
public class GoogleAiSettings
{
public PaLMSetting PaLM { get; set; }
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.GoogleAI.Settings;
public class PaLMSetting
{
public string Endpoint { get; set; } = string.Empty;
public string ApiKey { get; set; }
}

View file

@ -0,0 +1,13 @@
global using System;
global using System.Collections.Generic;
global using System.Text;
global using System.Threading.Tasks;
global using System.Linq;
global using System.Text.Json;
global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.MLTasks;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using System.Text.Json.Serialization;
global using BotSharp.Abstraction.Utilities;

View file

@ -85,6 +85,7 @@ public class MemVectorDatabase : IVectorDb
{
var simiMatix = CalCosineSimilarity(vec, records);
topK = Math.Min(topK, records.Count);
var topIndex = np.argsort(simiMatix)["::-1"][$":{topK}"];
var resIndex = new List<int>();

View file

@ -14,6 +14,8 @@ public class TextCompletionProvider : ITextCompletion
{
private readonly IServiceProvider _services;
private readonly LlamaSharpSettings _settings;
private string _model;
public string Provider => "llama-sharp";
public TextCompletionProvider(IServiceProvider services,
LlamaSharpSettings settings)
@ -42,4 +44,9 @@ public class TextCompletionProvider : ITextCompletion
return Task.FromResult(totalResponse);
}
public void SetModelName(string model)
{
_model = model;
}
}

View file

@ -29,17 +29,13 @@
<ItemGroup Condition="$(SolutionName)==PizzaBot">
<PackageReference Include="BotSharp.OpenAPI" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.AzureOpenAI" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.ChatbotUI" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.HuggingFace" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.KnowledgeBase" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.LLamaSharp" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.MetaAI" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.MetaMessenger" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.MongoStorage" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.PaddleSharp" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.Qdrant" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.RoutingSpeeder" Version="$(BotSharpVersion)" />
<PackageReference Include="BotSharp.Plugin.WeChat" Version="$(BotSharpVersion)" />
</ItemGroup>
<ItemGroup>
@ -52,6 +48,7 @@
<ItemGroup Condition="$(SolutionName)==BotSharp">
<ProjectReference Include="..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\Infrastructure\BotSharp.OpenAPI\BotSharp.OpenAPI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.MongoStorage\BotSharp.Plugin.MongoStorage.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.AzureOpenAI\BotSharp.Plugin.AzureOpenAI.csproj" />
<ProjectReference Include="..\Plugins\BotSharp.Plugin.ChatbotUI\BotSharp.Plugin.ChatbotUI.csproj" />

View file

@ -50,6 +50,13 @@
}
},
"GoogleAi": {
"PaLM": {
"Endpoint": "https://generativelanguage.googleapis.com",
"ApiKey": ""
}
},
"HuggingFace": {
"Endpoint": "https://api-inference.huggingface.co",
"Model": "tiiuae/falcon-180B-chat",
@ -113,6 +120,7 @@
"BotSharp.Core",
"BotSharp.Plugin.MongoStorage",
"BotSharp.Plugin.AzureOpenAI",
"BotSharp.Plugin.GoogleAI",
"BotSharp.Plugin.MetaAI",
"BotSharp.Plugin.HuggingFace",
"BotSharp.Plugin.LLamaSharp",