last ...
This commit is contained in:
@@ -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)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user