last ...
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xCommons.Configurations;
|
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
@@ -15,11 +16,13 @@ namespace xAiApi.Controllers
|
|||||||
ILogger<DefaultAiController> logger,
|
ILogger<DefaultAiController> logger,
|
||||||
IXDefaultAiService aiService,
|
IXDefaultAiService aiService,
|
||||||
XAppConfiguration appConfiguration,
|
XAppConfiguration appConfiguration,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
XValidationProvider validationProvider
|
XValidationProvider validationProvider
|
||||||
) : base(
|
) : base(
|
||||||
logger,
|
logger,
|
||||||
aiService,
|
aiService,
|
||||||
appConfiguration,
|
appConfiguration,
|
||||||
|
identityProvider,
|
||||||
validationProvider
|
validationProvider
|
||||||
)
|
)
|
||||||
{ }
|
{ }
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xCommons.Configurations;
|
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
@@ -19,11 +16,13 @@ namespace xAiApi.Controllers
|
|||||||
ILogger<DefaultEmbeddingController> logger,
|
ILogger<DefaultEmbeddingController> logger,
|
||||||
IXDefaultEmbeddingService aiService,
|
IXDefaultEmbeddingService aiService,
|
||||||
XAppConfiguration appConfiguration,
|
XAppConfiguration appConfiguration,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
XValidationProvider validationProvider
|
XValidationProvider validationProvider
|
||||||
) : base(
|
) : base(
|
||||||
logger,
|
logger,
|
||||||
aiService,
|
aiService,
|
||||||
appConfiguration,
|
appConfiguration,
|
||||||
|
identityProvider,
|
||||||
validationProvider
|
validationProvider
|
||||||
)
|
)
|
||||||
{ }
|
{ }
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xCommons.Configurations;
|
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
@@ -11,8 +12,15 @@ namespace xAiApi.Controllers
|
|||||||
ILogger<DefaultThinkingAiController> logger,
|
ILogger<DefaultThinkingAiController> logger,
|
||||||
IXDefaultThinkingAiService aiService,
|
IXDefaultThinkingAiService aiService,
|
||||||
XAppConfiguration appConfiguration,
|
XAppConfiguration appConfiguration,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
XValidationProvider validationProvider
|
XValidationProvider validationProvider
|
||||||
) : base(logger, aiService, appConfiguration, validationProvider)
|
) : base(
|
||||||
|
logger,
|
||||||
|
aiService,
|
||||||
|
appConfiguration,
|
||||||
|
identityProvider,
|
||||||
|
validationProvider
|
||||||
|
)
|
||||||
{ }
|
{ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xCommons.Configurations;
|
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
@@ -22,11 +23,13 @@ namespace xAiApi.Controllers
|
|||||||
ILogger logger,
|
ILogger logger,
|
||||||
TAiService aiService,
|
TAiService aiService,
|
||||||
XAppConfiguration appConfiguration,
|
XAppConfiguration appConfiguration,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
XValidationProvider validationProvider
|
XValidationProvider validationProvider
|
||||||
) : base(
|
) : base(
|
||||||
logger,
|
logger,
|
||||||
aiService,
|
aiService,
|
||||||
appConfiguration,
|
appConfiguration,
|
||||||
|
identityProvider,
|
||||||
validationProvider
|
validationProvider
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,21 +1,28 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using xAiModels.Models;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xCommons.Configurations;
|
|
||||||
using xCommons.Controllers;
|
|
||||||
using xCommons.Providers;
|
using xCommons.Providers;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xAiModels.Extensions;
|
||||||
|
using xAiModels.Models.Dtos;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xCommons.Configurations;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xIdentityService.Controllers;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
namespace xAiApi.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public abstract class XAiServiceControllerBase<TAiService> : XBaseController, IXAiServiceControllerBase
|
public abstract class XAiServiceControllerBase<TAiService> : XBaseIdentityApiV1Controller, IXAiServiceControllerBase
|
||||||
where TAiService : IXAiServiceBase
|
where TAiService : IXAiServiceBase
|
||||||
{
|
{
|
||||||
private readonly TAiService aiService;
|
private readonly TAiService aiService;
|
||||||
@@ -24,16 +31,20 @@ namespace xAiApi.Controllers
|
|||||||
ILogger logger,
|
ILogger logger,
|
||||||
TAiService aiService,
|
TAiService aiService,
|
||||||
XAppConfiguration appConfiguration,
|
XAppConfiguration appConfiguration,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
XValidationProvider validationProvider
|
XValidationProvider validationProvider
|
||||||
) : base(
|
) : base(
|
||||||
logger,
|
logger,
|
||||||
appConfiguration,
|
appConfiguration,
|
||||||
|
identityProvider,
|
||||||
validationProvider
|
validationProvider
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
this.aiService = aiService;
|
this.aiService = aiService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Actions ...
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ask Question and Get Response ...
|
/// Ask Question and Get Response ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -150,5 +161,175 @@ namespace xAiApi.Controllers
|
|||||||
await Response.Body.FlushAsync();
|
await Response.Body.FlushAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Data Provided Actions ...
|
||||||
|
/// <summary>
|
||||||
|
/// Load AI Projects ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("LoadProjects")]
|
||||||
|
public async Task<ActionResult<IEnumerable<XAiProjectDto>>> LoadProjects(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await aiService.LoadProjects(
|
||||||
|
ownerId: userInfo.UserId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("Ask")]
|
||||||
|
public async Task<ActionResult<string>> Ask(
|
||||||
|
[FromBody] XAiResponseRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: Reading Fiels Form Collection and Attach it ...
|
||||||
|
var result = await aiService.AskAsync(
|
||||||
|
files: null,
|
||||||
|
prompt: request.Prompt,
|
||||||
|
ownerId: userInfo.UserId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
projectId: request.ProjectId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: request.ConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response Stream ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AskStream")]
|
||||||
|
public async Task AskStream(
|
||||||
|
[FromBody] XAiResponseRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Configure SSE Response Headers ...
|
||||||
|
Response.Headers["X-Accel-Buffering"] = "no";
|
||||||
|
Response.Headers["Connection"] = "keep-alive";
|
||||||
|
Response.Headers["Content-Type"] = "text/event-stream";
|
||||||
|
Response.Headers["Cache-Control"] = "no-cache, no-store";
|
||||||
|
|
||||||
|
//
|
||||||
|
// Disable response buffering ...
|
||||||
|
var responseBodyFeature = HttpContext.Features
|
||||||
|
.Get<Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature>();
|
||||||
|
responseBodyFeature?.DisableBuffering();
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: Reading Fiels Form Collection and Attach it ...
|
||||||
|
var enumerable = aiService.AskAsEnumerable(
|
||||||
|
files: null,
|
||||||
|
prompt: request.Prompt,
|
||||||
|
ownerId: userInfo.UserId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
projectId: request.ProjectId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: request.ConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
await foreach (var resp in enumerable)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Skip Empty Responses ...
|
||||||
|
if (resp.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var payload = resp.ToJSON(camelCase: true);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Write SSE Event ...
|
||||||
|
await Response.WriteAsync($"{payload}", cancellationToken);
|
||||||
|
await Response.Body.FlushAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.Body.FlushAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
await Response.Body.FlushAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,8 +2,10 @@ using System;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xAiModels.Models;
|
using xAiModels.Models;
|
||||||
|
using xAiModels.Models.Dtos;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.AI;
|
using Microsoft.Extensions.AI;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace xAiApi.Interfaces
|
namespace xAiApi.Interfaces
|
||||||
@@ -23,6 +25,8 @@ namespace xAiApi.Interfaces
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
XAiModelDescriptor Descriptor { get; }
|
XAiModelDescriptor Descriptor { get; }
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Actions ...
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Base Method for Communicate with LLM ...
|
/// Base Method for Communicate with LLM ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -79,5 +83,64 @@ namespace xAiApi.Interfaces
|
|||||||
ChatMessage message = null,
|
ChatMessage message = null,
|
||||||
bool forceMessage = true
|
bool forceMessage = true
|
||||||
);
|
);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Data Provider Based Actions ...
|
||||||
|
/// <summary>
|
||||||
|
/// Load AI Projects ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<XAiProjectDto>> LoadProjects(
|
||||||
|
string ownerId,
|
||||||
|
string connectionId = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question as Prompt and Retrieve Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="ownerId"></param>
|
||||||
|
/// <param name="projectId"></param>
|
||||||
|
/// <param name="conversationId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="files"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XAiMessageDto> AskAsync(
|
||||||
|
string prompt,
|
||||||
|
string ownerId,
|
||||||
|
Guid projectId,
|
||||||
|
Guid conversationId,
|
||||||
|
string connectionId = null,
|
||||||
|
IFormFileCollection files = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question as Prompt and Retrieve Response as Async Enumerable ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="ownerId"></param>
|
||||||
|
/// <param name="projectId"></param>
|
||||||
|
/// <param name="conversationId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="files"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
|
||||||
|
string prompt,
|
||||||
|
string ownerId,
|
||||||
|
Guid projectId,
|
||||||
|
Guid conversationId,
|
||||||
|
string connectionId = null,
|
||||||
|
IFormFileCollection files = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,16 @@
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using xAiModels.Models;
|
||||||
|
using xAiModels.Models.Dtos;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace xAiApi.Interfaces
|
namespace xAiApi.Interfaces
|
||||||
{
|
{
|
||||||
public interface IXAiServiceControllerBase
|
public interface IXAiServiceControllerBase
|
||||||
{
|
{
|
||||||
|
//
|
||||||
|
#region Actions ...
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ask Question and Get Response ...
|
/// Ask Question and Get Response ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -29,5 +34,43 @@ namespace xAiApi.Interfaces
|
|||||||
[FromQuery] string question,
|
[FromQuery] string question,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Data Provided Actions ...
|
||||||
|
/// <summary>
|
||||||
|
/// Load AI Projects ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("LoadProjects")]
|
||||||
|
Task<ActionResult<IEnumerable<XAiProjectDto>>> LoadProjects(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("Ask")]
|
||||||
|
Task<ActionResult<string>> Ask(
|
||||||
|
[FromBody] XAiResponseRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response Stream ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AskStream")]
|
||||||
|
Task AskStream(
|
||||||
|
[FromBody] XAiResponseRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+512
-4
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using OpenAI;
|
using OpenAI;
|
||||||
using OllamaSharp;
|
using OllamaSharp;
|
||||||
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using xAiApi.Constants;
|
using xAiApi.Constants;
|
||||||
@@ -8,20 +9,22 @@ using xAiModels.Models;
|
|||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
using xAiApi.Extensions;
|
using xAiApi.Extensions;
|
||||||
using System.ClientModel;
|
using System.ClientModel;
|
||||||
|
using xCommons.Providers;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xAiModels.Constants;
|
using xAiModels.Constants;
|
||||||
using xAiModels.Interfaces;
|
using xAiModels.Interfaces;
|
||||||
using xAiModels.Extensions;
|
using xAiModels.Extensions;
|
||||||
using xAiApi.Configurations;
|
using xAiApi.Configurations;
|
||||||
using xExceptions.Constants;
|
using xExceptions.Constants;
|
||||||
|
using xAiModels.Models.Dtos;
|
||||||
|
using xAiService.Extensions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.AI;
|
using Microsoft.Extensions.AI;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.ClientModel.Primitives;
|
using System.ClientModel.Primitives;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using xAiModels.Models.Dtos;
|
|
||||||
using xAiService.Extensions;
|
|
||||||
|
|
||||||
namespace xAiApi.Providers
|
namespace xAiApi.Providers
|
||||||
{
|
{
|
||||||
@@ -40,17 +43,21 @@ namespace xAiApi.Providers
|
|||||||
private readonly IXAiDataProvider dataProvider;
|
private readonly IXAiDataProvider dataProvider;
|
||||||
private readonly ILogger<XAIServiceBase> logger;
|
private readonly ILogger<XAIServiceBase> logger;
|
||||||
private readonly XAiApiConfiguration configuration;
|
private readonly XAiApiConfiguration configuration;
|
||||||
|
private readonly XValidationProvider validationProvider;
|
||||||
|
|
||||||
protected XAIServiceBase(
|
protected XAIServiceBase(
|
||||||
IXAiDataProvider dataProvider,
|
IXAiDataProvider dataProvider,
|
||||||
ILogger<XAIServiceBase> logger,
|
ILogger<XAIServiceBase> logger,
|
||||||
XAiApiConfiguration configuration,
|
XAiApiConfiguration configuration,
|
||||||
|
XValidationProvider validationProvider,
|
||||||
string model = null
|
string model = null
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.dataProvider = dataProvider;
|
this.dataProvider = dataProvider;
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
|
this.validationProvider = validationProvider;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Prepare Embedding Model Descriptor ...
|
// Prepare Embedding Model Descriptor ...
|
||||||
@@ -337,8 +344,16 @@ namespace xAiApi.Providers
|
|||||||
|
|
||||||
//
|
//
|
||||||
#region Data Provider Based Actions ...
|
#region Data Provider Based Actions ...
|
||||||
|
/// <summary>
|
||||||
|
/// Load AI Projects ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ownerId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public async Task<IEnumerable<XAiProjectDto>> LoadProjects(
|
public async Task<IEnumerable<XAiProjectDto>> LoadProjects(
|
||||||
string ownerId,
|
string ownerId,
|
||||||
|
string connectionId = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -352,28 +367,408 @@ namespace xAiApi.Providers
|
|||||||
//
|
//
|
||||||
var result = await dataProvider.LoadProjects(
|
var result = await dataProvider.LoadProjects(
|
||||||
ownerId: ownerId,
|
ownerId: ownerId,
|
||||||
|
fillConversations: false,
|
||||||
|
connectionId: connectionId,
|
||||||
|
fillConversationMessages: false,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Here we have to fill Conversations of Default Project ...
|
// Here we have to fill Conversations of Default Project ...
|
||||||
var defaultProjectId = result.GetDafualtProject(ownerId);
|
var defaultProjectId = result.GetDafualtProject(ownerId);
|
||||||
|
var defaultProject = result.FirstOrDefault(x => x.Id == defaultProjectId);
|
||||||
|
result = result.Where(x => x.Id != defaultProjectId);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Reading Conversations of Specified Default Project ...
|
// Reading Conversations of Specified Default Project ...
|
||||||
var conversations = await dataProvider.LoadConversations(
|
var defaultProjectConversations = await dataProvider.LoadConversations(
|
||||||
fillMessages: false,
|
fillMessages: false,
|
||||||
|
connectionId: connectionId,
|
||||||
projectId: defaultProjectId,
|
projectId: defaultProjectId,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
var defaultConversationId = conversations.GetDefaultConversation(
|
var defaultConversationId = defaultProjectConversations.GetDefaultConversation(
|
||||||
ownerId: ownerId,
|
ownerId: ownerId,
|
||||||
projectId: defaultProjectId
|
projectId: defaultProjectId
|
||||||
);
|
);
|
||||||
|
var defaultConversation = defaultProjectConversations.First(x => x.Id == defaultConversationId);
|
||||||
|
defaultProjectConversations = defaultProjectConversations.Where(x => x.Id != defaultConversationId);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Reading Default Conversations Messages ...
|
||||||
|
var defaultConversationMessages = await dataProvider.LoadMessages(
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: defaultConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
defaultConversation.Messages = [.. defaultConversationMessages];
|
||||||
|
defaultProjectConversations = [defaultConversation, .. defaultProjectConversations];
|
||||||
|
result = [defaultProject, .. result];
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question as Prompt and Retrieve Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="ownerId"></param>
|
||||||
|
/// <param name="projectId"></param>
|
||||||
|
/// <param name="conversationId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="files"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XAiMessageDto> AskAsync(
|
||||||
|
string prompt,
|
||||||
|
string ownerId,
|
||||||
|
Guid projectId,
|
||||||
|
Guid conversationId,
|
||||||
|
string connectionId = null,
|
||||||
|
IFormFileCollection files = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotEmpty(prompt)
|
||||||
|
.AddNotEmpty(ownerId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Project ...
|
||||||
|
XAiProjectDto project = null;
|
||||||
|
if (projectId.IsDefaultGuid())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
project = await dataProvider.GetDefaultProject(
|
||||||
|
ownerId: ownerId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
projectId = project.Id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
project = await dataProvider.GetProject(
|
||||||
|
id: projectId,
|
||||||
|
fillConversations: false,
|
||||||
|
fillConversationMessages: false,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (project.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Conversation ...
|
||||||
|
XAiConversationDto conversation = null;
|
||||||
|
if (conversationId.IsDefaultGuid())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
conversation = await dataProvider.GetDefaultConversation(
|
||||||
|
ownerId: ownerId,
|
||||||
|
projectId: projectId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
conversationId = conversation.Id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
conversation = await dataProvider.GetConversation(
|
||||||
|
id: conversationId,
|
||||||
|
fillMessages: false,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (conversation.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Reading Messages ...
|
||||||
|
var messages = await dataProvider.LoadMessages(
|
||||||
|
conversationId: conversationId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: Parse Files ...
|
||||||
|
// for Attached them into Prompt Message ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Memory ...
|
||||||
|
IList<ChatMessage> history = await PrepareMemory(
|
||||||
|
project: project,
|
||||||
|
messages: messages,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare and Add Prompt Message ...
|
||||||
|
var promptMessage = new XAiMessageDto
|
||||||
|
{
|
||||||
|
Content = prompt,
|
||||||
|
OwnerId = ownerId,
|
||||||
|
Role = XAiChatRole.User,
|
||||||
|
CreatedOn = DateTime.UtcNow,
|
||||||
|
ConversationId = conversationId,
|
||||||
|
};
|
||||||
|
promptMessage = await dataProvider.AddMessage(
|
||||||
|
item: promptMessage,
|
||||||
|
connectionId: connectionId,
|
||||||
|
conversationId: conversationId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
promptMessage = await dataProvider.Enrich(
|
||||||
|
source: promptMessage,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Attach Uploaded Files ...
|
||||||
|
var promptChatMessage = promptMessage
|
||||||
|
.ToChatMessages();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ask Questions From LLM ...
|
||||||
|
var answer = await AskLLMAsync(
|
||||||
|
history: history,
|
||||||
|
prompt: promptChatMessage,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Answer AI Message ...
|
||||||
|
var result = new XAiMessageDto
|
||||||
|
{
|
||||||
|
Content = answer,
|
||||||
|
OwnerId = ownerId,
|
||||||
|
Role = XAiChatRole.System,
|
||||||
|
CreatedOn = DateTime.UtcNow,
|
||||||
|
ConversationId = conversationId
|
||||||
|
};
|
||||||
|
result = await dataProvider.AddMessage(
|
||||||
|
item: result,
|
||||||
|
connectionId: connectionId,
|
||||||
|
conversationId: conversationId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
result = await dataProvider.Enrich(
|
||||||
|
source: result,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question as Prompt and Retrieve Response as Async Enumerable ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="ownerId"></param>
|
||||||
|
/// <param name="projectId"></param>
|
||||||
|
/// <param name="conversationId"></param>
|
||||||
|
/// <param name="connectionId"></param>
|
||||||
|
/// <param name="files"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
|
||||||
|
string prompt,
|
||||||
|
string ownerId,
|
||||||
|
Guid projectId,
|
||||||
|
Guid conversationId,
|
||||||
|
string connectionId = null,
|
||||||
|
IFormFileCollection files = null,
|
||||||
|
[EnumeratorCancellation]
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
validationProvider
|
||||||
|
.GroupValidationBuilder()
|
||||||
|
.AddNotEmpty(prompt)
|
||||||
|
.AddNotEmpty(ownerId)
|
||||||
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Project ...
|
||||||
|
XAiProjectDto project = null;
|
||||||
|
if (projectId.IsDefaultGuid())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
project = await dataProvider.GetDefaultProject(
|
||||||
|
ownerId: ownerId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
projectId = project.Id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
project = await dataProvider.GetProject(
|
||||||
|
id: projectId,
|
||||||
|
fillConversations: false,
|
||||||
|
fillConversationMessages: false,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (project.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Conversation ...
|
||||||
|
XAiConversationDto conversation = null;
|
||||||
|
if (conversationId.IsDefaultGuid())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
conversation = await dataProvider.GetDefaultConversation(
|
||||||
|
ownerId: ownerId,
|
||||||
|
projectId: projectId,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
conversationId = conversation.Id;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
conversation = await dataProvider.GetConversation(
|
||||||
|
id: conversationId,
|
||||||
|
fillMessages: false,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (conversation.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Reading Messages ...
|
||||||
|
var messages = await dataProvider.LoadMessages(
|
||||||
|
conversationId: conversationId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// TODO: Parse Files ...
|
||||||
|
// for Attached them into Prompt Message ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Handle Memory ...
|
||||||
|
IList<ChatMessage> history = await PrepareMemory(
|
||||||
|
project: project,
|
||||||
|
messages: messages,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare and Add Prompt Message ...
|
||||||
|
var promptMessage = new XAiMessageDto
|
||||||
|
{
|
||||||
|
Content = prompt,
|
||||||
|
OwnerId = ownerId,
|
||||||
|
Role = XAiChatRole.User,
|
||||||
|
CreatedOn = DateTime.UtcNow,
|
||||||
|
ConversationId = conversationId,
|
||||||
|
};
|
||||||
|
promptMessage = await dataProvider.AddMessage(
|
||||||
|
item: promptMessage,
|
||||||
|
connectionId: connectionId,
|
||||||
|
conversationId: conversationId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
promptMessage = await dataProvider.Enrich(
|
||||||
|
source: promptMessage,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Attach Uploaded Files ...
|
||||||
|
var promptChatMessage = promptMessage
|
||||||
|
.ToChatMessages();
|
||||||
|
history.Add(promptChatMessage);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ask Questions From LLM ...
|
||||||
|
using var client = GetClient();
|
||||||
|
var enumerable = client.GetStreamingResponseAsync(
|
||||||
|
options: null,
|
||||||
|
messages: history
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
long sequence = 1;
|
||||||
|
var answer = string.Empty;
|
||||||
|
await foreach (var res in enumerable)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Cancellation Token ...
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var resp = new XAiResonseMessage
|
||||||
|
{
|
||||||
|
OwnerId = ownerId,
|
||||||
|
Content = res.Text,
|
||||||
|
Sequence = sequence,
|
||||||
|
Role = XAiChatRole.System,
|
||||||
|
RespondTo = promptMessage.Id,
|
||||||
|
RespondedOn = DateTime.UtcNow,
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
sequence++;
|
||||||
|
answer += res.Text;
|
||||||
|
|
||||||
|
//
|
||||||
|
yield return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Answer AI Message ...
|
||||||
|
var result = new XAiMessageDto
|
||||||
|
{
|
||||||
|
Content = answer,
|
||||||
|
OwnerId = ownerId,
|
||||||
|
Role = XAiChatRole.System,
|
||||||
|
CreatedOn = DateTime.UtcNow,
|
||||||
|
ConversationId = conversationId
|
||||||
|
};
|
||||||
|
result = await dataProvider.AddMessage(
|
||||||
|
item: result,
|
||||||
|
connectionId: connectionId,
|
||||||
|
conversationId: conversationId,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -382,5 +777,118 @@ namespace xAiApi.Providers
|
|||||||
/// <exception cref="NotImplementedException"></exception>
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
public virtual void Dispose()
|
public virtual void Dispose()
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
/// <summary>
|
||||||
|
/// Implementing Memory Strategy here ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="project"></param>
|
||||||
|
/// <param name="messages"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task<IList<ChatMessage>> PrepareMemory(
|
||||||
|
XAiProjectDto project,
|
||||||
|
IEnumerable<XAiMessageDto> messages,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new List<ChatMessage>();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (messages.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// if Messages Has Child, this means
|
||||||
|
// Default Messages Handled before ...
|
||||||
|
result = [.. messages.Select(m => m.ToChatMessages())];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// If Message Has not any Childs, this means
|
||||||
|
// First Asking Questions ...
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve Introduction Prompt ...
|
||||||
|
var introductionPrompt = configuration.GetPrompt(XAiApiConstants.XAiApiIntroductionPromptName);
|
||||||
|
if (introductionPrompt.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidData.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var introductionMessage = new ChatMessage(
|
||||||
|
role: ChatRole.System,
|
||||||
|
content: introductionPrompt
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
result.Add(introductionMessage);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!project.IsNullOrDefault() &&
|
||||||
|
!project.Prompt.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// TODO: Handle Project Files Here ...
|
||||||
|
var projectMessage = new ChatMessage(
|
||||||
|
role: ChatRole.System,
|
||||||
|
contents: [new TextContent(project.Prompt)]
|
||||||
|
);
|
||||||
|
result.Add(projectMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Quetion from LLM ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="history"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task<string> AskLLMAsync(
|
||||||
|
ChatMessage prompt,
|
||||||
|
IList<ChatMessage> history,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (prompt.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
history.Add(prompt);
|
||||||
|
using var client = GetClient();
|
||||||
|
var response = await client.GetResponseAsync(
|
||||||
|
messages: history,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Response ...
|
||||||
|
if (!response.IsValid())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Dispose Client ...
|
||||||
|
client.Dispose();
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve Response Text ...
|
||||||
|
var result = response.Text;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using xAiApi.Constants;
|
using xAiApi.Constants;
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
|
using xCommons.Providers;
|
||||||
using xAiModels.Interfaces;
|
using xAiModels.Interfaces;
|
||||||
using xAiApi.Configurations;
|
using xAiApi.Configurations;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -11,11 +12,13 @@ namespace xAiApi.Providers
|
|||||||
public XDefaultAiService(
|
public XDefaultAiService(
|
||||||
IXAiDataProvider dataProvider,
|
IXAiDataProvider dataProvider,
|
||||||
ILogger<XDefaultAiService> logger,
|
ILogger<XDefaultAiService> logger,
|
||||||
XAiApiConfiguration configuration
|
XAiApiConfiguration configuration,
|
||||||
|
XValidationProvider validationProvider
|
||||||
) : base(
|
) : base(
|
||||||
dataProvider,
|
dataProvider,
|
||||||
logger,
|
logger,
|
||||||
configuration,
|
configuration,
|
||||||
|
validationProvider,
|
||||||
XAiApiConstants.XAiDefaultModelName
|
XAiApiConstants.XAiDefaultModelName
|
||||||
)
|
)
|
||||||
{ }
|
{ }
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
using xAiApi.Constants;
|
using xAiApi.Constants;
|
||||||
using xAiApi.Interfaces;
|
using xAiApi.Interfaces;
|
||||||
|
using xCommons.Providers;
|
||||||
using xAiModels.Interfaces;
|
using xAiModels.Interfaces;
|
||||||
using xAiApi.Configurations;
|
using xAiApi.Configurations;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.AI;
|
using Microsoft.Extensions.AI;
|
||||||
using xCommons.Extensions;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace xAiApi.Providers
|
namespace xAiApi.Providers
|
||||||
{
|
{
|
||||||
@@ -14,12 +14,14 @@ namespace xAiApi.Providers
|
|||||||
IXAiDataProvider dataProvider,
|
IXAiDataProvider dataProvider,
|
||||||
ILogger<XAIServiceBase> logger,
|
ILogger<XAIServiceBase> logger,
|
||||||
XAiApiConfiguration configuration,
|
XAiApiConfiguration configuration,
|
||||||
|
XValidationProvider validationProvider,
|
||||||
ReasoningEffort effort = ReasoningEffort.Medium,
|
ReasoningEffort effort = ReasoningEffort.Medium,
|
||||||
ReasoningOutput output = ReasoningOutput.Full
|
ReasoningOutput output = ReasoningOutput.Full
|
||||||
) : base(
|
) : base(
|
||||||
dataProvider,
|
dataProvider,
|
||||||
logger,
|
logger,
|
||||||
configuration,
|
configuration,
|
||||||
|
validationProvider,
|
||||||
XAiApiConstants.XAiDefaultThinkingModelName
|
XAiApiConstants.XAiDefaultThinkingModelName
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user