Skip to content

Commit

Permalink
Merge pull request #1382 from solliancenet/kb-message-content-items-c…
Browse files Browse the repository at this point in the history
…herry-pick-080

Add message content items - cherry pick into 0.8.0
  • Loading branch information
ciprianjichici authored Aug 8, 2024
2 parents b74f098 + 5fc864c commit ee72dd8
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace FoundationaLLM.Common.Constants.Orchestration
{
/// <summary>
/// Contains constants for the types of message content items.
/// </summary>
public static class MessageContentItemTypes
{
/// <summary>
/// Text content.
/// </summary>
public const string Text = "text";

/// <summary>
/// Image file content.
/// </summary>
public const string ImageFile = "image_file";

/// <summary>
/// File path content.
/// </summary>
public const string FilePath = "file_path";
}
}
10 changes: 9 additions & 1 deletion src/dotnet/Common/Models/Chat/Message.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Search.Documents.Indexes;
using FoundationaLLM.Common.Models.Orchestration;
using System.Text.Json.Serialization;

namespace FoundationaLLM.Common.Models.Chat;

Expand Down Expand Up @@ -82,12 +83,18 @@ public record Message
/// </summary>
public Citation[]? Citations { get; set; }

/// <summary>
/// The content of the message.
/// </summary>
[JsonPropertyName("content")]
public List<MessageContent>? Content { get; set; }

/// <summary>
/// Constructor for Message.
/// </summary>
public Message(string sessionId, string sender, int? tokens, string text,
float[]? vector, bool? rating, string upn, string? senderDisplayName = null,
Citation[]? citations = null, string? expectedCompletion = null)
Citation[]? citations = null, string? expectedCompletion = null, List<MessageContent>? content = null)
{
Id = Guid.NewGuid().ToString();
Type = nameof(Message);
Expand All @@ -102,5 +109,6 @@ public Message(string sessionId, string sender, int? tokens, string text,
UPN = upn;
ExpectedCompletion = expectedCompletion;
Citations = citations;
Content = content;
}
}
22 changes: 22 additions & 0 deletions src/dotnet/Common/Models/Chat/MessageContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;

namespace FoundationaLLM.Common.Models.Chat
{
/// <summary>
/// Contains parts that compose the message content.
/// </summary>
public class MessageContent
{
/// <summary>
/// The type of the message content. Could be text, image, etc.
/// </summary>
[JsonPropertyName("type")]
public string? Type { get; set; }

/// <summary>
/// The value of the message content.
/// </summary>
[JsonPropertyName("value")]
public string? Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class CompletionResponseBase
[JsonPropertyName("completion")]
public string Completion { get; set; }

/// <summary>
/// Content returned from the Assistants API.
/// </summary>
[JsonPropertyName("content")]
public List<MessageContentItemBase>? Content { get; set; }

/// <summary>
/// The citations used in building the completion response.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/dotnet/Common/Models/Orchestration/MessageContentItemBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FoundationaLLM.Common.Constants.Orchestration;
using System.Text.Json.Serialization;

namespace FoundationaLLM.Common.Models.Orchestration
{
/// <summary>
/// Base message content item model.
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(OpenAITextMessageContentItem), MessageContentItemTypes.Text)]
[JsonDerivedType(typeof(OpenAIImageFileMessageContentItem), MessageContentItemTypes.ImageFile)]
[JsonDerivedType(typeof(OpenAIFilePathContentItem), MessageContentItemTypes.FilePath)]
public class MessageContentItemBase
{
/// <summary>
/// The type of the message content item.
/// </summary>
[JsonPropertyName("type")]
[JsonPropertyOrder(-100)]
public virtual string? Type { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using FoundationaLLM.Common.Constants.Orchestration;
using System.Text.Json.Serialization;

namespace FoundationaLLM.Common.Models.Orchestration
{
/// <summary>
/// File content item used to generate a message content item.
/// </summary>
public class OpenAIFilePathContentItem : MessageContentItemBase
{
/// <inheritdoc/>
[JsonIgnore]
public override string? Type { get; set; }

/// <summary>
/// The text of the annotation.
/// </summary>
[JsonPropertyName("text")]
public string? Text { get; set; }

/// <summary>
/// The starting index of the annotation.
/// </summary>
[JsonPropertyName("start_index")]
public int? StartIndex { get; set; }

/// <summary>
/// The ending index of the annotation.
/// </summary>
[JsonPropertyName("end_index")]
public int? EndIndex { get; set; }

/// <summary>
/// The ID of the file referenced by the annotation.
/// </summary>
[JsonPropertyName("file_id")]
public string? FileId { get; set; }

/// <summary>
/// Set default property values.
/// </summary>
public OpenAIFilePathContentItem() =>
Type = MessageContentItemTypes.FilePath;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using FoundationaLLM.Common.Constants.Orchestration;
using System.Text.Json.Serialization;

namespace FoundationaLLM.Common.Models.Orchestration
{
/// <summary>
/// An OpenAI image file message content item.
/// </summary>
public class OpenAIImageFileMessageContentItem : MessageContentItemBase
{
/// <inheritdoc/>
[JsonIgnore]
public override string? Type { get; set; }

/// <summary>
/// The ID of the image file.
/// </summary>
[JsonPropertyName("file_id")]
public string? FileId { get; set; }

/// <summary>
/// The URL of the image file.
/// </summary>
[JsonPropertyName("file_url")]
public string? FileUrl { get; set; }

/// <summary>
/// Set default property values.
/// </summary>
public OpenAIImageFileMessageContentItem() =>
Type = MessageContentItemTypes.ImageFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using FoundationaLLM.Common.Constants.Orchestration;
using System.Text.Json.Serialization;

namespace FoundationaLLM.Common.Models.Orchestration
{
/// <summary>
/// An OpenAI text message content item.
/// </summary>
public class OpenAITextMessageContentItem : MessageContentItemBase
{
/// <inheritdoc/>
[JsonIgnore]
public override string? Type { get; set; }

/// <summary>
/// A list of file annotations used to generate the message content item.
/// </summary>
[JsonPropertyName("annotations")]
public List<OpenAIFilePathContentItem> Annotations { get; set; } = [];

/// <summary>
/// The text value of the message content item.
/// </summary>
[JsonPropertyName("value")]
public string? Value { get; set; }

/// <summary>
/// Set default property values.
/// </summary>
public OpenAITextMessageContentItem() =>
Type = MessageContentItemTypes.Text;
}
}

0 comments on commit ee72dd8

Please sign in to comment.