last works on AiApi:
- Default Ai Service and Controller; - Default Embeding and Controller; - ...
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OllamaSharp;
|
||||
using OllamaSharp.Models;
|
||||
using xAiApi.Configurations;
|
||||
using xAiApi.Constants;
|
||||
using xAiApi.Extensions;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiApi.Models;
|
||||
using xAiModels.Interfaces;
|
||||
using xAiService.Helpers;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xAiApi.Providers
|
||||
{
|
||||
/// <summary>
|
||||
/// Base Embedding Provider Service ...
|
||||
/// </summary>
|
||||
public abstract class XAiEmbeddingServiceBase : XAIServiceBase, IXAiEmbeddingServiceBase
|
||||
{
|
||||
private readonly XAiModelDescriptor descriptor;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Embedding Specified String ...
|
||||
/// </summary>
|
||||
/// <param name="content"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<float[]> Embeding(
|
||||
string content,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (content.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Ollama Client ...
|
||||
using var client = GetEmbeddingClient();
|
||||
|
||||
//
|
||||
// Create Embeding 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[]>> Embedings(
|
||||
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 Embeding 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace xAiApi.Providers
|
||||
{
|
||||
public XDefaultAiService(
|
||||
IXAiDataProvider dataProvider,
|
||||
ILogger<XAIServiceBase> logger,
|
||||
ILogger<XDefaultAiService> logger,
|
||||
XAiApiConfiguration configuration
|
||||
) : base(
|
||||
dataProvider,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xAiApi.Configurations;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiModels.Interfaces;
|
||||
|
||||
namespace xAiApi.Providers
|
||||
{
|
||||
public class XDefaultEmbeddingService : XAiEmbeddingServiceBase, IXDefaultEmbeddingService
|
||||
{
|
||||
public XDefaultEmbeddingService(
|
||||
IXAiDataProvider dataProvider,
|
||||
XAiApiConfiguration configuration,
|
||||
ILogger<XDefaultEmbeddingService> logger
|
||||
) : base(
|
||||
dataProvider,
|
||||
configuration,
|
||||
logger
|
||||
)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user