192 lines
6.5 KiB
C#
192 lines
6.5 KiB
C#
using System;
|
|
using xAiApi.Constants;
|
|
using xAiApi.Providers;
|
|
using xAiApi.Extensions;
|
|
using xAiApi.Interfaces;
|
|
using xAiApi.Middlewares;
|
|
using xCommons.Extensions;
|
|
using xAiApi.Configurations;
|
|
using xExceptions.Constants;
|
|
using xAiApi.Providers.Extractors;
|
|
using xAiApi.Interfaces.Extractors;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace xAiApi.DI
|
|
{
|
|
public static partial class XDIHelperExtension
|
|
{
|
|
/// <summary>
|
|
/// Extract AI Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XAiApiConfiguration GetXAiApiConfiguration(this IConfiguration source)
|
|
{
|
|
//
|
|
XAiApiConfiguration result = null;
|
|
|
|
//
|
|
var section = source.GetSection(ConfigurationNodeNames.AI_API_NODE);
|
|
if (!section.IsNull())
|
|
{
|
|
result = section.Get<XAiApiConfiguration>();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register AiApi Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXAiApiConfiguration(
|
|
this IServiceCollection source,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
var apiConfig = configuration.GetXAiApiConfiguration();
|
|
source.AddXAiApiConfiguration(apiConfig);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register AiApi Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
public static void AddXAiApiConfiguration(
|
|
this IServiceCollection source,
|
|
XAiApiConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
if (!configuration.IsValid())
|
|
{
|
|
XException.InvalidConfiguration.Throw();
|
|
}
|
|
|
|
//
|
|
source.AddSingleton(configuration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Rag Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXRagService(
|
|
this IServiceCollection source
|
|
)
|
|
{
|
|
//
|
|
// Persistent RAG (Knowledge over XAiVector Table) ...
|
|
source.AddScoped<IXAiEmbeddingService, XAiEmbeddingService>();
|
|
source.AddScoped<IXAiRAGService, XAiRAGService>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Ai Services ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXAiServices(
|
|
this IServiceCollection source
|
|
)
|
|
{
|
|
//
|
|
source.AddScoped<IXAiService, XAiService>();
|
|
source.AddScoped<IXDefaultThinkingAiService, XDefaultThinkingAiService>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register AiApi File Content Extractor Services ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXAiFileContentExtractors(
|
|
this IServiceCollection source
|
|
)
|
|
{
|
|
//
|
|
// Register File Content Extractors ...
|
|
source.AddSingleton<IXFileContentExtractorBase, XPdfFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XRtfFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XEpubFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XDocxFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XHtmlFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XCodeFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XImageFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XAudioFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XExcelFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XVisionFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XPlainTextFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XPowerPointFileContentExtractor>();
|
|
source.AddSingleton<IXFileContentExtractorBase, XOpenDocumentFileContentExtractor>();
|
|
|
|
//
|
|
source.AddSingleton<IXFileContentExtractor, XFileContentExtractor>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register AiApi Text Chunker Service ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXAiTextChunker(
|
|
this IServiceCollection source
|
|
)
|
|
{
|
|
//
|
|
// Stateless -> Singleton ...
|
|
source.AddSingleton<IXTextChunker, XTextChunker>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register In-Memory Vector Store Service ...
|
|
/// </summary>
|
|
public static void AddXAiInMemoryVectorStore(this IServiceCollection source)
|
|
{
|
|
source.AddSingleton<IXInMemoryVectorStore, XInMemoryVectorStore>();
|
|
source.AddHostedService<XInMemoryVectorStoreCleanupService>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register all Services Required for Chat With Documents (In-Memory RAG):
|
|
/// Text Chunker + In-Memory Vector Store (+ Cleanup) ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXAiChatWithDocuments(this IServiceCollection source)
|
|
{
|
|
source.AddXAiInMemoryVectorStore();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use Model Proxy Provider ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static IApplicationBuilder UseXAiModelProxy(
|
|
this IApplicationBuilder source
|
|
)
|
|
{
|
|
return source.UseMiddleware<XAiModelProxyMiddleware>();
|
|
}
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// a LogTag for Service ...
|
|
/// </summary>
|
|
private static string XLogTag = XAiApiConstants.XDILogTag;
|
|
|
|
/// <summary>
|
|
/// print a log in Console ...
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
private static void Log(string message)
|
|
{
|
|
Console.WriteLine($"{XLogTag} => {message}");
|
|
}
|
|
#endregion
|
|
}
|
|
} |