Compare commits
8
Commits
a4dee3b5a4
...
26df2c7ebc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26df2c7ebc | ||
|
|
755eafa7d2 | ||
|
|
47b2cbc0d2 | ||
|
|
f48b9257a3 | ||
|
|
5d3601ba44 | ||
|
|
33922537d5 | ||
|
|
8e23a09f7b | ||
|
|
2aa0607e85 |
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xAiModels.Models;
|
||||
using xModels.Extensions;
|
||||
using xCommons.Extensions;
|
||||
using xAiModels.Constants;
|
||||
using xAiModels.Models.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using Microsoft.Extensions.AI;
|
||||
using xAiModels.Models.Entities;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -26,7 +28,6 @@ namespace xAiModels.Extensions
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.OwnerId.IsNullOrEmpty() &&
|
||||
!source.Title.IsNullOrEmpty() &&
|
||||
!source.Prompt.IsNullOrEmpty();
|
||||
|
||||
//
|
||||
@@ -38,7 +39,6 @@ namespace xAiModels.Extensions
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.OwnerId.IsNullOrEmpty() &&
|
||||
!source.Title.IsNullOrEmpty() &&
|
||||
!source.Prompt.IsNullOrEmpty();
|
||||
|
||||
//
|
||||
@@ -56,7 +56,6 @@ namespace xAiModels.Extensions
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.OwnerId.IsNullOrEmpty() &&
|
||||
!source.Title.IsNullOrEmpty() &&
|
||||
!source.ProjectId.IsNull() &&
|
||||
!source.ProjectId.IsDefaultGuid();
|
||||
|
||||
@@ -69,7 +68,6 @@ namespace xAiModels.Extensions
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.OwnerId.IsNullOrEmpty() &&
|
||||
!source.Title.IsNullOrEmpty() &&
|
||||
!source.ProjectId.IsNull() &&
|
||||
!source.ProjectId.IsDefaultGuid();
|
||||
|
||||
@@ -177,6 +175,83 @@ namespace xAiModels.Extensions
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate a Request for Retrieve Response in Controllers ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this XAiResponseRequest source)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.Prompt.IsNullOrEmpty();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate a File Extraction Result ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(this XFileExtractionResult source)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
!source.IsNullOrDefault() &&
|
||||
(
|
||||
source.Images.HasChild() ||
|
||||
!source.Text.IsNullOrEmpty() ||
|
||||
!source.AudioTranscript.IsNullOrEmpty()
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a File Extraction Result to AI Contents ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IList<AIContent> ToAIContents(this XFileExtractionResult source)
|
||||
{
|
||||
//
|
||||
var result = new List<AIContent>();
|
||||
|
||||
//
|
||||
if (source.IsValid())
|
||||
{
|
||||
//
|
||||
// Main Text ...
|
||||
if (!source.Text.IsNullOrEmpty())
|
||||
{
|
||||
result.Add(new TextContent(source.Text));
|
||||
}
|
||||
|
||||
//
|
||||
// Images ...
|
||||
if (source.Images.HasChild())
|
||||
{
|
||||
//
|
||||
foreach (var img in source.Images)
|
||||
{
|
||||
result.Add(new DataContent(img.Bytes, img.MimeType));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Transcripted Audio ...
|
||||
if (!source.AudioTranscript.IsNullOrEmpty())
|
||||
{
|
||||
result.Add(new TextContent($"[Audio Transcript]\n{source.AudioTranscript}"));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check a Collection Contains Default Project for Specified Owner or not ...
|
||||
/// </summary>
|
||||
@@ -204,14 +279,14 @@ namespace xAiModels.Extensions
|
||||
.Contains(ownerId.ToNormalString()) &&
|
||||
x.Title
|
||||
.ToNormalString()
|
||||
.Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID)
|
||||
.Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID.ToNormalString())
|
||||
);
|
||||
|
||||
//
|
||||
var result = !item.IsNullOrDefault();
|
||||
|
||||
//
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -253,7 +328,7 @@ namespace xAiModels.Extensions
|
||||
var result = !item.IsNullOrDefault();
|
||||
|
||||
//
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -269,7 +344,7 @@ namespace xAiModels.Extensions
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (source.HasChild() ||
|
||||
if (!source.HasChild() ||
|
||||
ownerId.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
@@ -283,7 +358,7 @@ namespace xAiModels.Extensions
|
||||
.Contains(ownerId.ToNormalString()) &&
|
||||
x.Title
|
||||
.ToNormalString()
|
||||
.Contains(XAiModelsConstants.DefaultXAiProjectDescriptionResourceID)
|
||||
.Contains(XAiModelsConstants.XAiProjectTitleIdentifier.ToNormalString())
|
||||
);
|
||||
if (item.IsNullOrDefault())
|
||||
{
|
||||
@@ -293,7 +368,7 @@ namespace xAiModels.Extensions
|
||||
//
|
||||
return item.Id;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get Default Conversation Id from a Collection Contains ...
|
||||
/// </summary>
|
||||
@@ -309,7 +384,8 @@ namespace xAiModels.Extensions
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (source.HasChild() ||
|
||||
if (!source.HasChild() ||
|
||||
ownerId.IsNullOrEmpty() ||
|
||||
projectId.IsDefaultGuid())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
@@ -323,17 +399,68 @@ namespace xAiModels.Extensions
|
||||
.Contains(ownerId.ToNormalString()) &&
|
||||
x.Title
|
||||
.ToNormalString()
|
||||
.Contains(projectId.ToString().ToNormalString()) &&
|
||||
x.Title
|
||||
.ToNormalString()
|
||||
.Contains(XAiModelsConstants.DefaultXAiConversationTitleResourceID)
|
||||
.Contains(XAiModelsConstants.XAiConversationTitleIdentifier.ToNormalString())
|
||||
);
|
||||
if (item.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = item.Id;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Message Dto to ChatMessage ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="additionalContents"></param>
|
||||
/// <returns></returns>
|
||||
public static ChatMessage ToChatMessages(
|
||||
this XAiMessageDto source,
|
||||
IList<AIContent> additionalContents = null
|
||||
)
|
||||
{
|
||||
//
|
||||
ChatMessage result = null;
|
||||
|
||||
//
|
||||
if (!source.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
var contents = new List<AIContent>
|
||||
{
|
||||
//
|
||||
// Add Source Content ...
|
||||
new TextContent(source.Content)
|
||||
};
|
||||
|
||||
//
|
||||
// Add Additional Content ...
|
||||
if (additionalContents.HasChild())
|
||||
{
|
||||
contents.AddRange(additionalContents);
|
||||
}
|
||||
|
||||
//
|
||||
result = new ChatMessage
|
||||
{
|
||||
Contents = contents,
|
||||
AuthorName =
|
||||
source.Role == XAiChatRole.User &&
|
||||
!source.Owner.IsNullOrDefault()
|
||||
? source.Owner.GetFullname()
|
||||
: string.Empty,
|
||||
Role = source.Role.ToChatRole(),
|
||||
MessageId = source.Id.ToString(),
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xAiModels.Models.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
@@ -11,18 +12,51 @@ namespace xAiModels.Interfaces
|
||||
/// </summary>
|
||||
public interface IXAiDataProvider
|
||||
{
|
||||
//
|
||||
#region Getters ...
|
||||
/// <summary>
|
||||
/// Retrieve Specified Project ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="fillConversations"></param>
|
||||
/// <param name="fillConversationMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiProjectDto> GetProject(
|
||||
Guid id,
|
||||
bool fillConversations = false,
|
||||
bool fillConversationMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Conversation ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiConversationDto> GetConversation(
|
||||
Guid id,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Loaders ...
|
||||
/// <summary>
|
||||
/// Load Projects List of Specified Owner ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="fillConversations"></param>
|
||||
/// <param name="fillConversationMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiProjectDto>> LoadProjects(
|
||||
string ownerId,
|
||||
string connectionId = null,
|
||||
bool fillConversations = false,
|
||||
bool fillConversationMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -33,11 +67,13 @@ namespace xAiModels.Interfaces
|
||||
/// </summary>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiConversationDto>> LoadConversations(
|
||||
Guid projectId,
|
||||
bool fillMessages = false,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -54,7 +90,117 @@ namespace xAiModels.Interfaces
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Default Identifiers ...
|
||||
#region Enrichers ...
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fillConversations"></param>
|
||||
/// <param name="fillConversationMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiProjectDto> Enrich(
|
||||
XAiProjectDto source,
|
||||
bool fillConversations = false,
|
||||
bool fillConversationMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="fillConversations"></param>
|
||||
/// <param name="fillConversationMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiProjectDto>> EnrichRange(
|
||||
IEnumerable<XAiProjectDto> sources,
|
||||
bool fillConversations = false,
|
||||
bool fillConversationMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiConversationDto> Enrich(
|
||||
XAiConversationDto source,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="fillMessages"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiConversationDto>> EnrichRange(
|
||||
IEnumerable<XAiConversationDto> sources,
|
||||
bool fillMessages = false,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiMessageDto> Enrich(
|
||||
XAiMessageDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XAiMessageDto>> EnrichRange(
|
||||
IEnumerable<XAiMessageDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Defaults ...
|
||||
/// <summary>
|
||||
/// Retrieve Default Project of Specified Owner ...
|
||||
/// Create it if not Handled ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiProjectDto> GetDefaultProject(
|
||||
string ownerId,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Default Conversation of Specified Owner ...
|
||||
/// Create it if not Handled ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiConversationDto> GetDefaultConversation(
|
||||
string ownerId,
|
||||
Guid projectId,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
@@ -77,12 +223,14 @@ namespace xAiModels.Interfaces
|
||||
/// <summary>
|
||||
/// Create Conversation ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiConversationDto> CreateConversation(
|
||||
string ownerId,
|
||||
Guid projectId,
|
||||
XAiConversationDto item = null,
|
||||
string connectionId = null,
|
||||
@@ -104,5 +252,51 @@ namespace xAiModels.Interfaces
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Query ...
|
||||
/// <summary>
|
||||
/// Query Projects ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XQueryResult<XAiProjectDto>> QueryProjects(
|
||||
string ownerId,
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Query Conversations ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XQueryResult<XAiConversationDto>> QueryConversations(
|
||||
string ownerId,
|
||||
Guid projectId,
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Query Messages ...
|
||||
/// </summary>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XQueryResult<XAiMessageDto>> QueryMessages(
|
||||
string ownerId,
|
||||
Guid conversationId,
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using xModels.Base;
|
||||
using xAiModels.Constants;
|
||||
|
||||
namespace xAiModels.Models
|
||||
{
|
||||
public class XAiResonseMessage : XBaseDto
|
||||
{
|
||||
public long Sequence { get; set; }
|
||||
public Guid RespondTo { get; set; }
|
||||
public string OwnerId { get; set; }
|
||||
public string Content { get; set; }
|
||||
public XAiChatRole Role { get; set; }
|
||||
public DateTime RespondedOn { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
|
||||
namespace xAiModels.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// /Request for a Response in Controllers ...
|
||||
/// </summary>
|
||||
public class XAiResponseRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Prompt Text ...
|
||||
/// </summary>
|
||||
public string Prompt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specified Project Id ...
|
||||
/// </summary>
|
||||
public Guid ProjectId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specified Conversation Id ...
|
||||
/// </summary>
|
||||
public Guid ConversationId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xAiModels.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds File Extraction Result ...
|
||||
/// </summary>
|
||||
public class XFileExtractionResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracted String Content ...
|
||||
/// </summary>
|
||||
public string Text { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Extracted Images for Vision Model ...
|
||||
/// </summary>
|
||||
public IList<XExtractedImage> Images { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Extracted Content from Audio File ...
|
||||
/// </summary>
|
||||
public string AudioTranscript { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Main File Name ...
|
||||
/// </summary>
|
||||
public string FileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Main File MIME Type ...
|
||||
/// </summary>
|
||||
public string MimeType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Specified Fallback done using Images or not ...
|
||||
/// </summary>
|
||||
public bool UsedImageFallback { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Error Message ...
|
||||
/// </summary>
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracted Image ...
|
||||
/// </summary>
|
||||
public class XExtractedImage
|
||||
{
|
||||
/// <summary>
|
||||
/// Content as Byte ...
|
||||
/// </summary>
|
||||
public byte[] Bytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// MIME Type of Image ...
|
||||
/// </summary>
|
||||
public string MimeType { get; set; } = "image/png";
|
||||
|
||||
/// <summary>
|
||||
/// for PDF Files ...
|
||||
/// </summary>
|
||||
public int? PageNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Describe Image ...
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -134,7 +134,9 @@ namespace xAiModels.Providers.Enrichers
|
||||
result.Owner = owner;
|
||||
result.TitleResource = titleResource;
|
||||
result.ProjectTitleResource = projectTitleResource;
|
||||
result.ProjectTitle = projectTitleResource.Resource;
|
||||
result.ProjectDescriptionResource = projectDescriptionResource;
|
||||
result.ProjectDescription = projectDescriptionResource.Resource;
|
||||
|
||||
//
|
||||
return result;
|
||||
|
||||
+954
-194
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user