Skip to content

Commit

Permalink
Merge pull request #240 from Avanade/feature/structV8
Browse files Browse the repository at this point in the history
feat (Liquid.Core): AI functionalities OCR and ChatCompletions.
  • Loading branch information
lucianareginalino authored Jun 19, 2024
2 parents 7c0cb06 + 0f4a0c1 commit 87dda76
Show file tree
Hide file tree
Showing 20 changed files with 626 additions and 157 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/liquid-ci-cd-cache.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/liquid-ci-cd-domain-extensions-crud.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/liquid-ci-cd-domain.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/liquid-ci-cd-http-extensions-crud.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/liquid-ci-cd-messaging.yaml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/liquid-ci-cd-repository.yml

This file was deleted.

53 changes: 53 additions & 0 deletions src/Liquid.Core/AbstractMappers/OcrResultMapper.cs
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"/>.

Check warning on line 48 in src/Liquid.Core/AbstractMappers/OcrResultMapper.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

XML comment has cref attribute 'TFrom' that refers to a type parameter
/// </summary>
/// <param name="dataObject">data source object instance.</param>
protected abstract Task<OcrResult> MapImpl(TFrom dataObject);
}
}
24 changes: 24 additions & 0 deletions src/Liquid.Core/Entities/ChatCompletionResult.cs
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; }

}
}
35 changes: 35 additions & 0 deletions src/Liquid.Core/Entities/ChatMessages.cs
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; }
}

}
39 changes: 39 additions & 0 deletions src/Liquid.Core/Entities/ClientDictionary.cs
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;
}
}
}
72 changes: 72 additions & 0 deletions src/Liquid.Core/Entities/FunctionBody.cs
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"));
}
}
}
Loading

0 comments on commit 87dda76

Please sign in to comment.