-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from Avanade/feature/structV8
feat (Liquid.Core): AI functionalities OCR and ChatCompletions.
- Loading branch information
Showing
20 changed files
with
626 additions
and
157 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Liquid.Core.Entities; | ||
using Liquid.Core.Exceptions; | ||
using Liquid.Core.Interfaces; | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading.Tasks; | ||
|
||
namespace Liquid.Core.AbstractMappers | ||
{ | ||
/// <summary> | ||
/// Defines object that map data between two instance types. | ||
/// </summary> | ||
/// <typeparam name="TFrom">type of data source object.</typeparam> | ||
[ExcludeFromCodeCoverage] | ||
public abstract class OcrResultMapper<TFrom> : ILiquidMapper<TFrom, OcrResult> | ||
{ | ||
private readonly string _mapperName; | ||
|
||
/// <summary> | ||
/// Create a new instance of <see cref="OcrResultMapper{TFrom}"/> | ||
/// </summary> | ||
/// <param name="mapperName">Mapper implementation name.</param> | ||
public OcrResultMapper(string mapperName) | ||
{ | ||
_mapperName = mapperName; | ||
} | ||
///<inheritdoc/> | ||
public async Task<OcrResult> Map(TFrom dataObject) | ||
{ | ||
if (dataObject is null) | ||
{ | ||
throw new ArgumentNullException(nameof(dataObject)); | ||
} | ||
|
||
try | ||
{ | ||
return await MapImpl(dataObject); | ||
} | ||
catch (Exception e) | ||
{ | ||
var msg = $"{_mapperName} throw data mapping error: '{e.Message}'"; | ||
|
||
throw new DataMappingException(msg, e); | ||
} | ||
} | ||
/// <summary> | ||
/// Create a new instance of <see cref="OcrResult"/> | ||
/// with values obtained from <see cref="TFrom"/>. | ||
/// </summary> | ||
/// <param name="dataObject">data source object instance.</param> | ||
protected abstract Task<OcrResult> MapImpl(TFrom dataObject); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Liquid.Core.Entities | ||
{ | ||
/// <summary> | ||
/// Chat completions result set. | ||
/// </summary> | ||
public class ChatCompletionResult | ||
{ | ||
/// <summary> | ||
/// The content of the response message. | ||
/// </summary> | ||
public string Content { get; set; } | ||
|
||
/// <summary> | ||
/// The reason the model stopped generating tokens, together with any applicable details. | ||
/// </summary> | ||
public string FinishReason { get; set; } | ||
|
||
/// <summary> | ||
/// The total number of tokens processed for the completions request and response. | ||
/// </summary> | ||
public int Usage { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
| ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Liquid.Core.Entities | ||
{ | ||
/// <summary> | ||
/// The object of context messages associated with a chat completions request | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public class ChatMessages | ||
{ | ||
/// <summary> | ||
/// The collection of context messages associated with a chat completions request. | ||
/// </summary> | ||
public List<ChatMessage> Messages { get; set; } = new List<ChatMessage>(); | ||
} | ||
/// <summary> | ||
/// Context message associated with a chat completions request. | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public class ChatMessage | ||
{ | ||
/// <summary> | ||
/// The chat role associated with this message. | ||
/// </summary> | ||
public string Role { get; set; } | ||
|
||
/// <summary> | ||
/// The contents of the message. | ||
/// </summary> | ||
public string Content { get; set; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Liquid.Core.Entities | ||
{ | ||
/// <summary> | ||
/// Client dictionary to store client instances. | ||
/// </summary> | ||
/// <typeparam name="T">Type of client service.</typeparam> | ||
[ExcludeFromCodeCoverage] | ||
public class ClientDictionary<T> | ||
{ | ||
/// <summary> | ||
/// Number of executions for this client. | ||
/// </summary> | ||
public int Executions { get; set; } = 1; | ||
|
||
|
||
/// <summary> | ||
/// Client connection alias. | ||
/// </summary> | ||
public string ClientId { get; set; } | ||
|
||
/// <summary> | ||
/// Client instance. | ||
/// </summary> | ||
public T Client { get; set; } | ||
|
||
/// <summary> | ||
/// Initialize a new instance of <see cref="ClientDictionary{T}"/>. | ||
/// </summary> | ||
/// <param name="clientId">Client connection alias.</param> | ||
/// <param name="client">Client instance.</param> | ||
public ClientDictionary(string clientId, T client) | ||
{ | ||
ClientId = clientId; | ||
Client = client; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json; | ||
|
||
namespace Liquid.Core.Entities | ||
{ | ||
/// <summary> | ||
/// The body of a function to be called. | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public class FunctionBody | ||
{ | ||
|
||
/// <summary> The name of the function to be called. </summary> | ||
public string Name { get; set; } | ||
/// <summary> | ||
/// A description of what the function does. The model will use this description when selecting the function and | ||
/// interpreting its parameters. | ||
/// </summary> | ||
public string Description { get; set; } | ||
/// <summary> | ||
/// The parameters the function accepts, described as a JSON Schema object. | ||
/// <para> | ||
/// To assign an object to this property use <see cref="BinaryData.FromObjectAsJson{T}(T, JsonSerializerOptions)"/>. | ||
/// </para> | ||
/// <para> | ||
/// To assign an already formatted json string to this property use <see cref="BinaryData.FromString(string)"/>. | ||
/// </para> | ||
/// </summary> | ||
public BinaryData Parameters { get; set; } | ||
|
||
/// <summary> Initializes a new instance of <see cref="FunctionBody"/>. </summary> | ||
/// <param name="name"> The name of the function to be called. </param> | ||
/// <param name="description"> | ||
/// A description of what the function does. The model will use this description when selecting the function and | ||
/// interpreting its parameters. | ||
/// </param> | ||
/// <param name="parameters"> The parameters the function accepts, described as a JSON Schema object. </param> | ||
/// <exception cref="ArgumentException"></exception> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public FunctionBody(string name, string description, BinaryData parameters) | ||
{ | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
throw new ArgumentException($"'{nameof(name)}' cannot be null or empty.", nameof(name)); | ||
} | ||
|
||
if (string.IsNullOrEmpty(description)) | ||
{ | ||
throw new ArgumentException($"'{nameof(description)}' cannot be null or empty.", nameof(description)); | ||
} | ||
|
||
Name = name; | ||
Description = description; | ||
Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters)); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="FunctionBody"/>. | ||
/// </summary> | ||
/// <param name="functionBody">function definition JSON string. </param> | ||
public FunctionBody(string functionBody) | ||
{ | ||
var function = JsonSerializer.Deserialize<JsonElement>(functionBody); | ||
|
||
Name = function.GetProperty("name").ToString(); | ||
Description = function.GetProperty("description").ToString(); | ||
Parameters = BinaryData.FromObjectAsJson(function.GetProperty("parameters")); | ||
} | ||
} | ||
} |
Oops, something went wrong.