Models. NL Serialization

This commit is contained in:
femsci 2023-11-17 21:33:38 +01:00
parent 9cb901873d
commit e9245294a4
Signed by: femsci
GPG key ID: 08F7911F0E650C67
9 changed files with 184 additions and 9 deletions

View file

@ -0,0 +1,7 @@
namespace Nyanlabs.Umogen.Core.Models;
public record ApiRequest
{
public string Model { get; set; } = "gpt-3.5-turbo";
public float Temperature { get; set; } = 0.7f;
}

View file

@ -0,0 +1,6 @@
namespace Nyanlabs.Umogen.Core.Models;
public interface INLSerializable
{
public string NLSerialize();
}

View file

@ -0,0 +1,15 @@
namespace Nyanlabs.Umogen.Core.Models;
public record Person(string Name, string Surname, string? Id = null, string? Pesel = null, DateTime? DoB = null) : INLSerializable
{
public string Name { get; set; } = Name;
public string Surname { get; set; } = Surname;
public string? IDCode { get; set; } = Id;
public string? Pesel { get; set; } = Pesel;
public DateTime? DoB { get; set; } = DoB;
public string NLSerialize()
{
return $"{Name} {Surname}; {(Pesel != null ? $"PESEL {Pesel}" : "unknown PESEL")}; {(IDCode != null ? $"ID series {IDCode}" : "unknown ID series")}; {(DoB != null ? $"born {DoB.Value:dd MMM yyyy}" : "unknown date of birth")}";
}
}

View file

@ -0,0 +1,50 @@
namespace Nyanlabs.Umogen.Core.Models;
public enum UmoDoctype
{
CONTR_EMPLOYMENT,
CONTR_SERVICE,
CONTR_ASS
}
public static class UmoDoctypeBindings
{
private static readonly Dictionary<UmoDoctype, (string, string)> s_umoass = new()
{
{ UmoDoctype.CONTR_EMPLOYMENT, ("Contract of Employment", "Umowa o Pracę")},
{ UmoDoctype.CONTR_SERVICE, ("Contract of Service", "Umowa Zlecenie") },
{ UmoDoctype.CONTR_ASS, ("Contract for Assignment", "Umowa o Dzieło") }
};
public static (string, string) Name(this UmoDoctype t)
{
return s_umoass[t];
}
public static UmoDoctype? GetFromName(string name)
{
KeyValuePair<UmoDoctype, (string, string)>? entry = s_umoass.FirstOrDefault((a) => a.Value.Item1.Equals(name.Trim(), StringComparison.OrdinalIgnoreCase) || a.Value.Item2.Equals(name.Trim(), StringComparison.OrdinalIgnoreCase));
return entry?.Key;
}
}
public class UmoDocument : INLSerializable
{
public UmoDocument(UmoDoctype type, Person employer, Person employee, ValidTime? validTime = null)
{
Doctype = type;
Employer = employer;
Employee = employee;
ValidTime = validTime;
}
public UmoDoctype Doctype { get; set; }
public Person Employer { get; set; }
public Person Employee { get; set; }
//Null is 'unspecified time'
public ValidTime? ValidTime { get; set; }
public string NLSerialize()
{
return $"{Doctype.Name().Item1} for {Employee.NLSerialize()} working for {Employer.NLSerialize()}; valid {ValidTime?.NLSerialize() ?? "for unspecified time"}.";
}
}

View file

@ -0,0 +1,22 @@
namespace Nyanlabs.Umogen.Core.Models;
public readonly struct ValidTime : INLSerializable
{
public ValidTime(DateTime start, DateTime end)
{
Start = start;
End = end;
}
public ValidTime(DateTime start, TimeSpan duration)
{
Start = start;
End = start + duration;
}
public readonly DateTime Start { get; }
public readonly DateTime End { get; }
public readonly TimeSpan Duration => End - Start;
public string NLSerialize() => $"from {Start:dd MMM yyyy} until {End:dd MMM yyyy}";
}

View file

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Nyanlabs.Umogen.Core; namespace Nyanlabs.Umogen.Core;

View file

@ -0,0 +1,16 @@
namespace Nyanlabs.Umogen.Core;
public class UmoProcess
{
public UmoProcess(UmoEngine eng)
{
_eng = eng;
}
private readonly UmoEngine _eng;
public async Task Ask()
{
}
}

View file

@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Nyanlabs.Umogen.Core; namespace Nyanlabs.Umogen.Core;
public class Umogen public class Umogen
{ {
public const string DEFAULT_API_KEY_FILE = "umogenkey.secret"; public const string DEFAULT_API_KEY_FILE = "umogenkey.secret";
public const string PROMPT = "You write Polish legal document in markdown format.";
} }

View file

@ -0,0 +1,67 @@
using Nyanlabs.Umogen.Core.Models;
namespace Nyanlabs.Umogen.CoreTests;
public class NLSerializationTests
{
private Person ModelEmployer => new("Kotek", "Miauczyński", "CBS4327563", "04281308999", new DateTime(2004, 08, 13));
private Person ModelEmployee => new("Miau", "Kotczyński", "CBS69696969", "76011694336", new DateTime(1976, 1, 16));
[Fact]
public void FullDocumentNLTest()
{
// Given
UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee);
// When
var query = doc.NLSerialize();
// Then
Assert.Equal($"Contract of Employment for {ModelEmployee.NLSerialize()} working for {ModelEmployer.NLSerialize()}; valid for unspecified time.", query);
}
[Fact]
public void FullDocumentNLTestSpecifiedTime()
{
// Given
UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee, new(new DateTime(2023, 11, 17), TimeSpan.FromDays(1)));
// When
var query = doc.NLSerialize();
// Then
Assert.Equal($"Contract of Employment for {ModelEmployee.NLSerialize()} working for {ModelEmployer.NLSerialize()}; valid from 17 Nov 2023 until 18 Nov 2023.", query);
}
[Fact]
public void PersonNLTest()
{
// Given
Person p = ModelEmployer;
// When
var query = p.NLSerialize();
// Then
Assert.Equal("Kotek Miauczyński; PESEL 04281308999; ID series CBS4327563; born 13 Aug 2004", query);
}
[Fact]
public void PersonNLTestNoData()
{
// Given
Person p = ModelEmployer with
{
IDCode = null,
Pesel = null,
DoB = null,
};
// When
var query = p.NLSerialize();
// Then
Assert.Equal("Kotek Miauczyński; unknown PESEL; unknown ID series; unknown date of birth", query);
}
}