Compare commits
1
Commits
561542ea0a
...
a4bcde5994
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4bcde5994 |
@@ -0,0 +1,7 @@
|
|||||||
|
namespace xAiApi.Constants
|
||||||
|
{
|
||||||
|
public partial struct ConfigurationNodeNames
|
||||||
|
{
|
||||||
|
public const string AI_API_NODE = "AiAPiConfiguration";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ namespace xAiApi.Constants
|
|||||||
public const string XAiApiMigrationsAssemblyIdentifier = "xAiApi";
|
public const string XAiApiMigrationsAssemblyIdentifier = "xAiApi";
|
||||||
|
|
||||||
//
|
//
|
||||||
|
public const string XDILogTag = "XAiApi";
|
||||||
public const string XAiApiIntroductionPromptName = "Introduction";
|
public const string XAiApiIntroductionPromptName = "Introduction";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xAiApi.Interfaces;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using xCommons.Providers;
|
||||||
|
|
||||||
|
namespace xAiApi.Controllers
|
||||||
|
{
|
||||||
|
[Authorize]
|
||||||
|
[Route ("[controller]")]
|
||||||
|
public class DefaultAiController : XAiServiceControllerBase<IXDefaultAiService>
|
||||||
|
{
|
||||||
|
public DefaultAiController(
|
||||||
|
ILogger<DefaultAiController> logger,
|
||||||
|
IXDefaultAiService aiService,
|
||||||
|
XAppConfiguration appConfiguration,
|
||||||
|
XValidationProvider validationProvider
|
||||||
|
) : base(
|
||||||
|
logger,
|
||||||
|
aiService,
|
||||||
|
appConfiguration,
|
||||||
|
validationProvider
|
||||||
|
)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xAiApi.Interfaces;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using xCommons.Controllers;
|
||||||
|
using xCommons.Providers;
|
||||||
|
|
||||||
|
namespace xAiApi.Controllers
|
||||||
|
{
|
||||||
|
[Authorize]
|
||||||
|
[Route ("[controller]")]
|
||||||
|
public abstract class XAiServiceControllerBase<TAiService> : XBaseController, IXAiServiceControllerBase
|
||||||
|
where TAiService : IXAiServiceBase
|
||||||
|
{
|
||||||
|
private readonly TAiService aiService;
|
||||||
|
|
||||||
|
protected XAiServiceControllerBase(
|
||||||
|
ILogger logger,
|
||||||
|
TAiService aiService,
|
||||||
|
XAppConfiguration appConfiguration,
|
||||||
|
XValidationProvider validationProvider
|
||||||
|
) : base(
|
||||||
|
logger,
|
||||||
|
appConfiguration,
|
||||||
|
validationProvider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
this.aiService = aiService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="question"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpGet("Ask")]
|
||||||
|
public virtual async Task<ActionResult<string>> Ask(
|
||||||
|
[FromQuery] string question,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
ValidationProvider.NotEmpty(question);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await aiService.AskAsync(
|
||||||
|
prompt: question,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using xAiApi.Configurations;
|
||||||
|
using xAiApi.Constants;
|
||||||
|
using xAiApi.Extensions;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -186,7 +186,7 @@ namespace xAiApi.Extensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string GetPrompt(
|
public static string GetPrompt(
|
||||||
this XAiApiConfiguration source,
|
this XAiApiConfiguration source,
|
||||||
string name,
|
string name = null,
|
||||||
IDictionary<string, string> @params = null
|
IDictionary<string, string> @params = null
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace xAiApi.Interfaces
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Describe a Base Service Actions of any Ai Based Services ...
|
/// Describe a Base Service Actions of any Ai Based Services ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IXAIServiceBase : IDisposable
|
public interface IXAiServiceBase : IDisposable
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Base Method for Communicate with LLM ...
|
/// Base Method for Communicate with LLM ...
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace xAiApi.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXAiServiceControllerBase
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="question"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("Ask")]
|
||||||
|
Task<ActionResult<string>> Ask(
|
||||||
|
[FromQuery] string question,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace xAiApi.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXDefaultAiService : IXAiServiceBase
|
||||||
|
{ }
|
||||||
|
}
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace xAiApi
|
namespace xAiApi
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ using OllamaSharp;
|
|||||||
|
|
||||||
namespace xAiApi.Providers
|
namespace xAiApi.Providers
|
||||||
{
|
{
|
||||||
public abstract class XAIServiceBase : IXAIServiceBase
|
public abstract class XAIServiceBase : IXAiServiceBase
|
||||||
{
|
{
|
||||||
private readonly IXAiDataProvider dataProvider;
|
private readonly IXAiDataProvider dataProvider;
|
||||||
private readonly ILogger<XAIServiceBase> logger;
|
private readonly ILogger<XAIServiceBase> logger;
|
||||||
@@ -273,7 +273,7 @@ namespace xAiApi.Providers
|
|||||||
// Preparing Introduction Prompt ...
|
// Preparing Introduction Prompt ...
|
||||||
var introductionPrompt = new ChatMessage(
|
var introductionPrompt = new ChatMessage(
|
||||||
ChatRole.System,
|
ChatRole.System,
|
||||||
PromptHelper.GetIntroductionPrompt().Prompt
|
configuration.GetPrompt()
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -307,7 +307,7 @@ namespace xAiApi.Providers
|
|||||||
// Preparing Introduction Prompt ...
|
// Preparing Introduction Prompt ...
|
||||||
var introductionPrompt = new ChatMessage(
|
var introductionPrompt = new ChatMessage(
|
||||||
ChatRole.System,
|
ChatRole.System,
|
||||||
PromptHelper.GetIntroductionPrompt().Prompt
|
configuration.GetPrompt()
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xAiApi.Configurations;
|
||||||
|
using xAiApi.Interfaces;
|
||||||
|
using xAiModels.Interfaces;
|
||||||
|
|
||||||
|
namespace xAiApi.Providers
|
||||||
|
{
|
||||||
|
public class XDefaultAiService : XAIServiceBase, IXDefaultAiService
|
||||||
|
{
|
||||||
|
public XDefaultAiService(
|
||||||
|
IXAiDataProvider dataProvider,
|
||||||
|
ILogger<XAIServiceBase> logger,
|
||||||
|
XAiApiConfiguration configuration
|
||||||
|
) : base(
|
||||||
|
dataProvider,
|
||||||
|
logger,
|
||||||
|
configuration
|
||||||
|
)
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -26,6 +26,11 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using IdentityModel.AspNetCore.OAuth2Introspection;
|
using IdentityModel.AspNetCore.OAuth2Introspection;
|
||||||
using xPushService.Helpers;
|
using xPushService.Helpers;
|
||||||
|
using xAiApi.DI;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xAiApi.Interfaces;
|
||||||
|
using xAiApi.Providers;
|
||||||
|
using xAiModels.DI;
|
||||||
|
|
||||||
namespace xAiApi
|
namespace xAiApi
|
||||||
{
|
{
|
||||||
@@ -169,6 +174,21 @@ namespace xAiApi
|
|||||||
lifeTime: lifeTime,
|
lifeTime: lifeTime,
|
||||||
repositoryType: repositoryType
|
repositoryType: repositoryType
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register AiApi Data Service ...
|
||||||
|
services.AddXAiDataService<XAiApiDbContext>(
|
||||||
|
lifeTime: lifeTime,
|
||||||
|
repositoryType: repositoryType
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register XAiApi Configuration ...
|
||||||
|
services.AddXAiApiConfiguration(Configuration);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register Default Ai Service ...
|
||||||
|
services.AddScoped<IXDefaultAiService, XDefaultAiService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
|||||||
@@ -35,6 +35,25 @@
|
|||||||
"ThumbQuality": 72,
|
"ThumbQuality": 72,
|
||||||
"MaxFileSize": 41943040
|
"MaxFileSize": 41943040
|
||||||
},
|
},
|
||||||
|
"AiAPiConfiguration": {
|
||||||
|
"Models": [
|
||||||
|
{
|
||||||
|
"ApiKey": "",
|
||||||
|
"Name": "Gemma",
|
||||||
|
"LLM": "gemma3:1b",
|
||||||
|
"Provider": "Ollama",
|
||||||
|
"Url": "http://localhost:11434"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Prompts": [
|
||||||
|
{
|
||||||
|
"Name": "Introduction",
|
||||||
|
"Template": "your name is xSaherElmAi, you are a professional ai assistant which develped by SaherElm IT Center",
|
||||||
|
"Description": "",
|
||||||
|
"Params": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
|
|||||||
Reference in New Issue
Block a user