Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e33479f9b6 | ||
|
|
fed2b7d934 | ||
|
|
4245ba726f | ||
|
|
cece849cdf | ||
|
|
11881dcd9c | ||
|
|
0f8b8e8211 | ||
|
|
a78b2ae254 | ||
|
|
990fec2c38 | ||
|
|
9b84dde456 | ||
|
|
c72534614f |
@@ -1,100 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xAiService.Interfaces;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Providers;
|
||||
using xIdentityService.Controllers;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xAiService.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// a Controller base Class which implement based Actions to Provides AI Service Access ...
|
||||
/// </summary>
|
||||
public abstract class XAiServiceControllerBase : XIBaseProviderController, IXAiServiceControllerBase
|
||||
{
|
||||
private readonly IXAiService aiService;
|
||||
|
||||
protected XAiServiceControllerBase(
|
||||
ILogger logger,
|
||||
IXAiService aiService,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
this.aiService = aiService;
|
||||
}
|
||||
|
||||
//
|
||||
#region Text Actions ...
|
||||
[HttpGet("Ask")]
|
||||
public virtual async Task<ActionResult<string>> Ask(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var result = await aiService
|
||||
.GetTextResponseAsync(
|
||||
prompt: prompt,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("AskStream")]
|
||||
public async Task<ActionResult> AskAIStream(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var result = aiService
|
||||
.GetTextReponseStreamAsync(
|
||||
prompt: prompt,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using xAiService.Models.Dtos;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xAiService.Extensions
|
||||
{
|
||||
public static class PromptExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Get Template Params Filled Prompt ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="params"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetMessage(
|
||||
this XPromptDto source,
|
||||
IDictionary<string, string> @params = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
if (!source.IsNullOrDefault() &&
|
||||
!source.Prompt.IsNullOrEmpty()
|
||||
)
|
||||
{
|
||||
result = source.Prompt;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Params Conditions ...
|
||||
if (!source.Params.IsNullOrDefault() &&
|
||||
source.Params.HasChild() &&
|
||||
!@params.IsNullOrDefault() &&
|
||||
@params.HasChild()
|
||||
)
|
||||
{
|
||||
//
|
||||
// Select Params ...
|
||||
var existsParams = source.Params
|
||||
.Where(p => @params.Any(pr => pr.Key == p.Name))
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
//
|
||||
// Replace Selected Params ...
|
||||
existsParams
|
||||
.ForEach(pk =>
|
||||
{
|
||||
result = result.Replace(pk.Param, @params[pk.Name]);
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using xAiService.Models.Dtos;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xAiService.Helpers
|
||||
{
|
||||
public static class XPromptHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds Initialized Prompts ...
|
||||
/// </summary>
|
||||
private static XPromptsDto prompts;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize Prompt Helper ...
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
/// <exception cref="InvalidDataException"></exception>
|
||||
public static void Initialize(string path = "prompts.json")
|
||||
{
|
||||
//
|
||||
// Create full Path and Check File Exists ...
|
||||
var fullPath = Path.Combine(AppContext.BaseDirectory, path);
|
||||
if (!File.Exists(fullPath))
|
||||
{
|
||||
throw new Exception($"Prompts Resources not found: {fullPath}");
|
||||
}
|
||||
|
||||
//
|
||||
// Read File as Json Text ...
|
||||
var json = File.ReadAllText(fullPath);
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
AllowTrailingCommas = true,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter(
|
||||
namingPolicy: null,
|
||||
allowIntegerValues: false
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Serialized Root Object ...
|
||||
var root = JsonSerializer.Deserialize<XPromptsDto>(json, options);
|
||||
if (!root.IsNullOrDefault())
|
||||
{
|
||||
prompts = new XPromptsDto { Prompts = root.Prompts };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Allowed Prompts as Enumerable ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<XPromptDto> GetPrompts()
|
||||
{
|
||||
return prompts!.Prompts.AsEnumerable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Specified Prompt ...
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static XPromptDto GetPrompt(string name)
|
||||
{
|
||||
//
|
||||
return GetPrompts()
|
||||
.FirstOrDefault(p => p.Name == name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
@@ -51,7 +51,7 @@ namespace xAiService.Interfaces
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
IAsyncEnumerable<string> GetTextReponseStreamAsync(
|
||||
Task<Stream> GetTextReponseStreamAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace xAiService.Interfaces
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
Task<ActionResult> AskAIStream(
|
||||
Task<ActionResult> AskStream(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
namespace xAiService.Models.Dtos
|
||||
{
|
||||
/// <summary>
|
||||
/// Described Prompts in app ...
|
||||
/// </summary>
|
||||
public class XPromptsDto
|
||||
{
|
||||
public XPromptDto[] Prompts { get; set; } = [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// a Single Prompt Description ...
|
||||
/// </summary>
|
||||
public class XPromptDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Prompt Name ...
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prompt Template Message ...
|
||||
/// </summary>
|
||||
public string Prompt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Prompt Description ...
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Describe Prompt Params ...
|
||||
/// </summary>
|
||||
public virtual XPromptParamDto[] Params { get; set; } = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prompt Param used to Fill dynamic Datas to Specified Prompts ...
|
||||
/// </summary>
|
||||
public class XPromptParamDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Param Name ...
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Parma Identifier ...
|
||||
/// </summary>
|
||||
public string Param { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Param Description ...
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
@@ -104,7 +105,7 @@ namespace xAiService.Services
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public IAsyncEnumerable<string> GetTextReponseStreamAsync(
|
||||
public async Task<Stream> GetTextReponseStreamAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -123,7 +124,7 @@ namespace xAiService.Services
|
||||
|
||||
//
|
||||
var tokens = ToTokenResponse(userInfo);
|
||||
var result = StreamData<string>(
|
||||
var result = await StreamData(
|
||||
tokens: tokens,
|
||||
addXPoweredValue: true,
|
||||
endpoint: XAiApiEndpoint.Stream,
|
||||
|
||||
+1
-2
@@ -1,8 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="megan" value="https://hub.megan.ir/nuget/index.json" />
|
||||
<add key="nuget" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
<add key="baget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" />
|
||||
<add key="baget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" disableTLSCertificateValidation="true" />
|
||||
</packageSources>
|
||||
</configuration>
|
||||
+18
-6
@@ -1,12 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<!-- Runtime Definition -->
|
||||
<PropertyGroup>
|
||||
<Version>1.0.0</Version>
|
||||
<LangVersion>8.0</LangVersion>
|
||||
<LangVersion>12.0</LangVersion>
|
||||
<PackageId>xSaherelm.xAiService</PackageId>
|
||||
<Authors>Hadi Khazaee Asl</Authors>
|
||||
<Company>SaherElm IT Center</Company>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
|
||||
<Description>
|
||||
it is a Part of xSaherElm project which Provides Services to use xSaherElm AI Features ...
|
||||
</Description>
|
||||
@@ -21,18 +23,28 @@
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Local Projects -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\xIdentityHelper\xIdentityHelper.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\xAiModels\xAiModels.csproj" />
|
||||
<!-- <ProjectReference Include="..\xCommons\xCommons.csproj" />
|
||||
<ProjectReference Include="..\xIdentityHelper\xIdentityHelper.csproj" /> -->
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Local Modules -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xDashboard.xCommons" Version="1.0.0" />
|
||||
<!-- <PackageReference Include="xDashboard.xCommons" Version="1.0.0" /> -->
|
||||
<!-- <PackageReference Include="xDashboard.xIdentityService" Version="1.0.0" /> -->
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Dependencies -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.AI" Version="10.4.1" />
|
||||
<!-- <PackageReference Include="Microsoft.Extensions.AI" Version="10.4.1" /> -->
|
||||
</ItemGroup>
|
||||
|
||||
<!-- For XML Documentation Support -->
|
||||
<PropertyGroup>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user