This commit is contained in:
2026-04-09 17:02:13 +03:30
parent 843aff3879
commit 523ad16b3c
10 changed files with 706 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using xAiApi.AI.Interfaces;
using xAiApi.AI.Services;
namespace xAiApi.AI.DI
{
public static class AIExtensions
{
/// <summary>
/// Register AI Services ...
/// </summary>
/// <param name="services"></param>
public static void AddXAIServices(this IServiceCollection services)
{
//
services.AddSingleton<IXAIService, XAIService>();
}
/// <summary>
/// Use AI Service Middlewares ...
/// </summary>
/// <param name="builder"></param>
public static void UseXAIServices(this IApplicationBuilder builder)
{ }
}
}
+28
View File
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
namespace xAiApi.AI.Interfaces
{
public interface IXAIService
{
IChatClient TextGenreatorClient { get; }
IChatClient ImageGenreatorClient { get; }
//
#region Actions ...
/// <summary>
/// Generated Text ...
/// </summary>
/// <param name="prompt"></param>
/// <returns></returns>
Task<string> GetTextResponseAsync(string prompt);
/// <summary>
/// Generated Response ...
/// </summary>
/// <param name="prompt"></param>
/// <returns></returns>
Task<ChatResponse> GetResponseAsync(string prompt);
#endregion
}
}
+66
View File
@@ -0,0 +1,66 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using OllamaSharp;
using xAiApi.AI.Interfaces;
using xCommons.Extensions;
using xExceptions.Constants;
namespace xAiApi.AI.Services
{
public class XAIService : IXAIService
{
//
public IChatClient TextGenreatorClient { get; }
public IChatClient ImageGenreatorClient { get; }
public XAIService()
{
//
// here we are Initialize Text Generator Model ...
TextGenreatorClient = new OllamaApiClient(
new Uri("http://localhost:11434"),
"gemma3:1b"
);
}
//
#region Actions ...
/// <summary>
/// Generated Response ...
/// </summary>
/// <param name="prompt"></param>
/// <returns></returns>
public async Task<ChatResponse> GetResponseAsync(string prompt)
{
//
// Validate ...
var isValid = !prompt.IsNullOrEmpty();
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Generate Response ...
var result = await TextGenreatorClient
.GetResponseAsync(prompt);
//
return result;
}
/// <summary>
/// Generated Text ...
/// </summary>
/// <param name="prompt"></param>
/// <returns></returns>
public async Task<string> GetTextResponseAsync(string prompt)
{
//
var response = await GetResponseAsync(prompt);
return response.Text;
}
#endregion
}
}