Skip to content

Commit

Permalink
feat(dotnet): Support for result schema (#16)
Browse files Browse the repository at this point in the history
* feat(dotnet): Support for result schema

* chore(dotnet): Bump project version
  • Loading branch information
johnjcsmith authored Oct 31, 2024
1 parent 7f26a1b commit 6d12e8f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion sdk-dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,18 @@ The following code will create an [Inferable run](https://docs.inferable.ai/page
var run = await inferable.CreateRun(new CreateRunInput
{
Message = "Say hello to John",
// Optional: Explicitly attach the `sayHello` function (All functions attached by default)
AttachedFunctions = new List<FunctionReference>
{
new FunctionReference {
Function = "SayHello",
Service = "default"
}
},
// Optional: Define a schema for the result to conform to
ResultSchema = JsonSchema.FromType<RunOutput>();
// Optional: Subscribe an Inferable function to receive notifications when the run status changes
//OnStatusChange = new CreateOnStatusChangeInput
//OnStatusChange = new CreateOnStatusChangeInput<RunOutput>
//{
// Function = OnStatusChangeFunction
//}
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions sdk-dotnet/src/API/Models.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using NJsonSchema;

namespace Inferable.API
{

public class JsonSchemaConverter : JsonConverter<JsonSchema>
{
public override JsonSchema Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, JsonSchema value, JsonSerializerOptions options)
{
writer.WriteRawValue(value.ToJson());
}
}

public struct CreateMachineInput
{
[JsonPropertyName("service")]
Expand Down Expand Up @@ -135,6 +149,13 @@ public struct CreateRunInput
JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)
]
public CreateOnStatusChangeInput? OnStatusChange { get; set; }

[
JsonPropertyName("resultSchema"),
JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull),
JsonConverter(typeof(JsonSchemaConverter))
]
public JsonSchema? ResultSchema { get; set; }
}

public struct CreateOnStatusChangeInput
Expand Down
3 changes: 2 additions & 1 deletion sdk-dotnet/src/Inferable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class Links
}

/// <summary>
/// Object type that will be returned to a Run's OnStatusChange Function
/// Object type that will be returned to a Run's OnStatusChange Function.
/// Generic type T is the type of the result the run's (resultSchema)
/// </summary>
public struct OnStatusChangeInput<T>
{
Expand Down
2 changes: 1 addition & 1 deletion sdk-dotnet/src/Inferable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>

<PackageId>Inferable</PackageId>
<Version>0.0.11</Version>
<Version>0.0.12</Version>
<Authors>Inferable</Authors>
<Company>Inferable</Company>
<Description>Client library for interacting with the Inferable API</Description>
Expand Down
13 changes: 10 additions & 3 deletions sdk-dotnet/tests/Inferable.Tests/InferableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ private struct TestInput
public required string testString { get; set; }
}

private struct RunOutput
{
public bool didSayHello { get; set; }
}


[Fact]
public void Inferable_Can_Instantiate()
{
Expand Down Expand Up @@ -242,10 +248,10 @@ async public void Inferable_Run_E2E()
}),
});

var OnStatusChangeFunction = client.Default.RegisterFunction(new FunctionRegistration<OnStatusChangeInput<object>>
var OnStatusChangeFunction = client.Default.RegisterFunction(new FunctionRegistration<OnStatusChangeInput<RunOutput>>
{
Name = "onStatusChangeFn",
Func = new Func<OnStatusChangeInput<object>, object?>((input) =>
Func = new Func<OnStatusChangeInput<RunOutput>, object?>((input) =>
{
didCallOnStatusChange = true;
return null;
Expand All @@ -266,7 +272,8 @@ async public void Inferable_Run_E2E()
OnStatusChange = new CreateOnStatusChangeInput
{
Function = OnStatusChangeFunction
}
},
ResultSchema = JsonSchema.FromType<RunOutput>(),
});

var result = await run.Poll(null);
Expand Down

0 comments on commit 6d12e8f

Please sign in to comment.