277 lines
7.7 KiB
C#
277 lines
7.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using OllamaSharp;
|
|
using System.Threading;
|
|
using xAiApi.Constants;
|
|
using xAiModels.Models;
|
|
using xAiApi.Extensions;
|
|
using xAiApi.Interfaces;
|
|
using OllamaSharp.Models;
|
|
using xAiService.Helpers;
|
|
using xCommons.Extensions;
|
|
using xAiModels.Extensions;
|
|
using xAiModels.Interfaces;
|
|
using xAiApi.Configurations;
|
|
using xExceptions.Constants;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.AI;
|
|
using xAiModels.Constants;
|
|
using OpenAI;
|
|
using System.ClientModel;
|
|
using System.ClientModel.Primitives;
|
|
|
|
namespace xAiApi.Providers
|
|
{
|
|
/// <summary>
|
|
/// Base Embedding Provider Service ...
|
|
/// </summary>
|
|
public abstract class XAiEmbeddingServiceBase : XAIServiceBase, IXAiEmbeddingServiceBase
|
|
{
|
|
private readonly XAiModelDescriptor descriptor;
|
|
private readonly ILogger<XAiEmbeddingServiceBase> logger;
|
|
|
|
protected XAiEmbeddingServiceBase(
|
|
IXAiDataProvider dataProvider,
|
|
XAiApiConfiguration configuration,
|
|
ILogger<XAiEmbeddingServiceBase> logger,
|
|
string embeddingModelName = XAiApiConstants.XAiEmbeddingModelName
|
|
) : base(
|
|
dataProvider,
|
|
logger,
|
|
configuration
|
|
)
|
|
{
|
|
//
|
|
// Prepare Embedding Model Descriptor ...
|
|
descriptor = configuration.GetModel(embeddingModelName);
|
|
if (!descriptor.IsValid())
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
this.logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Embedding Specified String ...
|
|
/// </summary>
|
|
/// <param name="content"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<float[]> Embedding(
|
|
string content,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (content.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
using var generator = GetEmbeddingGenerator();
|
|
GeneratedEmbeddings<Embedding<float>> response = null;
|
|
try
|
|
{
|
|
response = await generator.GenerateAsync([content]);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex.Message);
|
|
}
|
|
|
|
//
|
|
if (response.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Generae Result ...
|
|
var result = response
|
|
.First()?
|
|
.Vector
|
|
.ToArray()
|
|
?? [];
|
|
if (result.Length == 0)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Ollama Client ...
|
|
// using var client = GetEmbeddingClient();
|
|
|
|
// //
|
|
// // Create Embedding Request ....
|
|
// var request = new EmbedRequest
|
|
// {
|
|
// Input = [content],
|
|
// Model = descriptor.LLM,
|
|
// };
|
|
|
|
// //
|
|
// // Make Request and Recieve Response ...
|
|
// var response = await client.EmbedAsync(
|
|
// request: request,
|
|
// cancellationToken: cancellationToken
|
|
// );
|
|
// if (response.IsNullOrDefault())
|
|
// {
|
|
// XException.ActionFailed.Throw();
|
|
// }
|
|
|
|
// //
|
|
// // Generae Result ...
|
|
// var result = response?
|
|
// .Embeddings?
|
|
// .FirstOrDefault()
|
|
// ?? [];
|
|
// if (result.Length == 0)
|
|
// {
|
|
// XException.ActionFailed.Throw();
|
|
// }
|
|
|
|
//
|
|
// Normalize ...
|
|
VectorHelper.Normalize(result);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Embedding Specified Batch ...
|
|
/// </summary>
|
|
/// <param name="batch"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<IReadOnlyList<float[]>> Embeddings(
|
|
string[] batch,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (!batch.HasChild() ||
|
|
!batch.All(x => !x.IsNullOrEmpty())
|
|
)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Ollama Client ...
|
|
using var client = GetEmbeddingClient();
|
|
|
|
//
|
|
// Create Embedding Request ....
|
|
var request = new EmbedRequest
|
|
{
|
|
Input = [.. batch],
|
|
Model = descriptor.LLM,
|
|
};
|
|
|
|
//
|
|
// Make Request and Recieve Response ...
|
|
var response = await client.EmbedAsync(
|
|
request: request,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (response.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Generae Result ...
|
|
var result = response?
|
|
.Embeddings
|
|
.ToArray()
|
|
?? [];
|
|
|
|
//
|
|
// Validate Result ...
|
|
if (result.Length == 0)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Normalize ...
|
|
foreach (var vector in result)
|
|
{
|
|
VectorHelper.Normalize(vector);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Access Embedding Client ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public OllamaApiClient GetEmbeddingClient()
|
|
{
|
|
//
|
|
var result = new OllamaApiClient(new Uri(descriptor.Url));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public IEmbeddingGenerator<string, Embedding<float>> GetEmbeddingGenerator()
|
|
{
|
|
//
|
|
IEmbeddingGenerator<string, Embedding<float>> result = null;
|
|
|
|
//
|
|
if (!descriptor.IsValid())
|
|
{
|
|
XException.InvalidData.Throw("Invalid Embedding Model ...");
|
|
}
|
|
|
|
//
|
|
switch (descriptor.Provider)
|
|
{
|
|
//
|
|
case XAiModelProviderType.OpenAI:
|
|
//
|
|
var openAiClient = new OpenAIClient(
|
|
new ApiKeyCredential(descriptor.ApiKey.IsNullOrEmpty() ? XAiApiConstants.XOpenAINoKey : descriptor.ApiKey),
|
|
new OpenAIClientOptions
|
|
{
|
|
Endpoint = new Uri(descriptor.Url),
|
|
Transport = new HttpClientPipelineTransport(GetHttpClient(descriptor.Url))
|
|
}
|
|
);
|
|
result = openAiClient
|
|
.GetEmbeddingClient(descriptor.LLM)
|
|
.AsIEmbeddingGenerator();
|
|
break;
|
|
|
|
//
|
|
case XAiModelProviderType.Ollama:
|
|
//
|
|
var ollamaClient = new OllamaApiClient(new Uri(descriptor.Url));
|
|
result = ollamaClient;
|
|
break;
|
|
}
|
|
|
|
//
|
|
if (result.IsNull())
|
|
{
|
|
XException.InvalidData.Throw("Invalid Embedding Model ...");
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |