feat: endpoint

This commit is contained in:
geffzhang 2024-03-28 10:56:37 +08:00
parent f6e0ff0cdc
commit 544e86fc7a
7 changed files with 39 additions and 30 deletions

View file

@ -2,7 +2,7 @@ using BotSharp.Plugin.MetaGLM.Modules;
namespace BotSharp.Plugin.MetaGLM
{
public class MetaGLMClientV4
public class MetaGLMClient
{
private MetaGLMSettings _settings;
@ -12,12 +12,12 @@ namespace BotSharp.Plugin.MetaGLM
public Embeddings Embeddings { get; private set; }
public MetaGLMClientV4(MetaGLMSettings settings)
public MetaGLMClient(MetaGLMSettings settings)
{
this._settings = settings;
this.Chat = new Chat(this._settings.ApiKey);
this.Images = new Images(this._settings.ApiKey);
this.Embeddings = new Embeddings(this._settings.ApiKey);
this.Chat = new Chat(this._settings.ApiKey, _settings.BaseAddress);
this.Images = new Images(this._settings.ApiKey, _settings.BaseAddress);
this.Embeddings = new Embeddings(this._settings.ApiKey, _settings.BaseAddress);
}
}
}

View file

@ -23,7 +23,7 @@ public class MetaGLMPlugin : IBotSharpPlugin
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<MetaGLMSettings>("MetaGLM");
});
services.AddScoped<MetaGLMClientV4>();
services.AddScoped<MetaGLMClient>();
services.AddScoped<IChatCompletion, ChatCompletionProvider>();
}
}

View file

@ -1,23 +1,24 @@
namespace BotSharp.Plugin.MetaGLM.Modules;
public enum ModelPortal
{
Regular,
Character,
}
//public enum ModelPortal
//{
// Regular,
// Character,
//}
public class Chat
{
private string _apiKey;
private string _baseAddress;
private static readonly int API_TOKEN_TTL_SECONDS = 60 * 5;
private static readonly HttpClient client = new();
private static readonly Dictionary<ModelPortal, string> PORTAL_URLS = new()
{
{ ModelPortal.Regular , "https://open.bigmodel.cn/api/paas/v4/chat/completions"},
};
//private static readonly Dictionary<ModelPortal, string> PORTAL_URLS = new()
//{
// { ModelPortal.Regular , "https://open.bigmodel.cn/api/paas/v4/chat/completions"},
//};
private static readonly JsonSerializerOptions JsonOptions = new ()
{
@ -29,9 +30,10 @@ public class Chat
WriteIndented = true,
};
public Chat(string apiKey)
public Chat(string apiKey, string basicAddress = "https://open.bigmodel.cn/api/paas/v4/")
{
this._apiKey = apiKey;
this._baseAddress = basicAddress;
}
private async IAsyncEnumerable<string> CompletionBase(TextRequestBase textRequestBody,string apiKey)
@ -43,7 +45,7 @@ public class Chat
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://open.bigmodel.cn/api/paas/v4/chat/completions"),
RequestUri = new Uri($"{_baseAddress}/chat/completions"),
Content = data,
Headers =
{

View file

@ -3,27 +3,30 @@ namespace BotSharp.Plugin.MetaGLM.Modules;
public class Embeddings
{
private string _apiKey;
private string _baseAddress;
private static readonly int API_TOKEN_TTL_SECONDS = 60 * 5;
static readonly HttpClient client = new();
public Embeddings(string apiKey)
public Embeddings(string apiKey, string basicAddress = "https://open.bigmodel.cn/api/paas/v4/")
{
this._apiKey = apiKey;
this._baseAddress = basicAddress;
}
private IEnumerable<string> ProcessBase(EmbeddingRequestBase requestBody, string apiKey)
private IEnumerable<string> ProcessBase(EmbeddingRequestBase requestBody)
{
var json = JsonSerializer.Serialize(requestBody);
// Console.WriteLine(JsonSerializer.Serialize(requestBody));
// Console.WriteLine("----1----");
// Console.WriteLine(json);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var api_key = AuthenticationUtils.GenerateToken(apiKey, API_TOKEN_TTL_SECONDS);
var api_key = AuthenticationUtils.GenerateToken(_apiKey, API_TOKEN_TTL_SECONDS);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://open.bigmodel.cn/api/paas/v4/embeddings"),
RequestUri = new Uri($"{_baseAddress}/embeddings"),
Content = data,
Headers =
{
@ -43,10 +46,10 @@ public class Embeddings
}
}
public EmbeddingResponseBase Process(EmbeddingRequestBase requestBody, string apiKey)
public EmbeddingResponseBase Process(EmbeddingRequestBase requestBody)
{
var sb = new StringBuilder();
foreach (var str in ProcessBase(requestBody,apiKey))
foreach (var str in ProcessBase(requestBody))
{
sb.Append(str);
}

View file

@ -3,24 +3,26 @@ namespace BotSharp.Plugin.MetaGLM.Modules;
public class Images
{
private string _apiKey;
private string _baseAddress;
private static readonly int API_TOKEN_TTL_SECONDS = 60 * 5;
static readonly HttpClient client = new HttpClient();
public Images(string apiKey)
public Images(string apiKey, string basicAddress = "https://open.bigmodel.cn/api/paas/v4/")
{
this._apiKey = apiKey;
this._baseAddress = basicAddress;
}
private IEnumerable<string> GenerateBase(ImageRequestBase requestBody, string apiKey)
private IEnumerable<string> GenerateBase(ImageRequestBase requestBody)
{
var json = JsonSerializer.Serialize(requestBody);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var api_key = AuthenticationUtils.GenerateToken(apiKey, API_TOKEN_TTL_SECONDS);
var api_key = AuthenticationUtils.GenerateToken(_apiKey, API_TOKEN_TTL_SECONDS);
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://open.bigmodel.cn/api/paas/v4/images/generations"),
RequestUri = new Uri($"{_baseAddress}images/generations"),
Content = data,
Headers =
{
@ -43,7 +45,7 @@ public class Images
public ImageResponseBase Generation(ImageRequestBase requestBody, string apiKey)
{
StringBuilder sb = new StringBuilder();
foreach (var str in GenerateBase(requestBody,apiKey))
foreach (var str in GenerateBase(requestBody))
{
sb.Append(str);
}

View file

@ -7,12 +7,12 @@ public class ChatCompletionProvider : IChatCompletion
private readonly MetaGLMSettings _settings;
private readonly IServiceProvider _services;
private readonly ILogger _logger;
private readonly MetaGLMClientV4 metaGLMClient;
private readonly MetaGLMClient metaGLMClient;
private string _model;
public ChatCompletionProvider(IServiceProvider services,
MetaGLMSettings settings,
MetaGLMClientV4 client,
MetaGLMClient client,
ILogger<ChatCompletionProvider> logger)
{
_services = services;

View file

@ -7,6 +7,8 @@ public class MetaGLMSettings
/// </summary>
public string ApiKey { get; set; }
public string BaseAddress { get; set; } = "https://open.bigmodel.cn/api/paas/v4/";
public string ModelId { get; set; }
public double Temperature { get; set; }