Files
xSaherElmAiApi/AI/Services/XAIService.cs
T
2026-04-09 17:02:13 +03:30

66 lines
1.7 KiB
C#

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
}
}