Skip to content

Commit

Permalink
com.openai.unity 8.0.3 (#254)
Browse files Browse the repository at this point in the history
- Fixed Thread.MessageResponse and Thread.RunStepResponse Delta objects not being properly populated
- Added Thread.MessageDelta.PrintContent()
- Added additional unit tests for delta objects
  • Loading branch information
StephenHodgson authored Jun 16, 2024
1 parent c081574 commit 7ee1c10
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 10 deletions.
13 changes: 12 additions & 1 deletion Runtime/Threads/MessageDelta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Scripting;

namespace OpenAI.Threads
Expand All @@ -21,10 +22,20 @@ internal MessageDelta(

[Preserve]
[JsonProperty("role")]
public Role Role { get; private set; }
public Role Role { get; internal set; }

[Preserve]
[JsonProperty("content")]
public IReadOnlyList<Content> Content { get; }

/// <summary>
/// Formats all of the <see cref="Content"/> items into a single string,
/// putting each item on a new line.
/// </summary>
/// <returns><see cref="string"/> of all <see cref="Content"/>.</returns>
public string PrintContent()
=> Content == null
? string.Empty
: string.Join("\n", Content.Select(c => c?.ToString()));
}
}
25 changes: 23 additions & 2 deletions Runtime/Threads/MessageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,45 @@ internal void AppendFrom(MessageResponse other)
{
if (other == null) { return; }

if (!string.IsNullOrWhiteSpace(Id))
{
if (Id != other.Id)
{
throw new InvalidOperationException("Attempting to append a different object than the original!");
}
}
else
{
Id = other.Id;
}

Object = other.Object;

if (other.Delta != null)
{
if (Role == 0 &&
other.Delta.Role > 0)
if (Role == 0 && other.Delta.Role > 0)
{
Role = other.Delta.Role;
}
else if (other.Delta.Role == 0 && Role > 0)
{
other.Delta.Role = Role;
}

if (other.Delta.Content != null)
{
content ??= new List<Content>();
content.AppendFrom(other.Delta.Content);
}

Delta = other.Delta;

// bail early since we only care about the delta content
return;
}

Delta = null;

if (Role == 0 &&
other.Role > 0)
{
Expand Down
20 changes: 17 additions & 3 deletions Runtime/Threads/RunResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ internal RunResponse(
/// </summary>
[Preserve]
[JsonProperty("id")]
public string Id { get; }
public string Id { get; private set; }

/// <summary>
/// The object type, which is always run.
/// </summary>
[Preserve]
[JsonProperty("object")]
public string Object { get; }
public string Object { get; private set; }

/// <summary>
/// The Unix timestamp (in seconds) for when the thread was created.
Expand Down Expand Up @@ -326,7 +326,7 @@ public DateTime? CompletedAt
/// which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
/// </remarks>
[Preserve]
[JsonProperty("response_format")]
[JsonProperty("response_format", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonConverter(typeof(ResponseFormatConverter))]
public ChatResponseFormat ResponseFormat { get; private set; }

Expand All @@ -340,6 +340,20 @@ internal void AppendFrom(RunResponse other)
{
if (other is null) { return; }

if (!string.IsNullOrWhiteSpace(Id))
{
if (Id != other.Id)
{
throw new InvalidOperationException("Attempting to append a different object than the original!");
}
}
else
{
Id = other.Id;
}

Object = other.Object;

if (other.Status > 0)
{
Status = other.Status;
Expand Down
24 changes: 21 additions & 3 deletions Runtime/Threads/RunStepResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ internal RunStepResponse(
/// </summary>
[Preserve]
[JsonProperty("id")]
public string Id { get; }
public string Id { get; private set; }

[Preserve]
[JsonProperty("object")]
public string Object { get; }
public string Object { get; private set; }

[Preserve]
[JsonProperty("delta", DefaultValueHandling = DefaultValueHandling.Ignore)]
public RunStepDelta Delta { get; }
public RunStepDelta Delta { get; private set; }

/// <summary>
/// The Unix timestamp (in seconds) for when the run step was created.
Expand Down Expand Up @@ -225,6 +225,20 @@ internal void AppendFrom(RunStepResponse other)
{
if (other == null) { return; }

if (!string.IsNullOrWhiteSpace(Id))
{
if (Id != other.Id)
{
throw new InvalidOperationException("Attempting to append a different object than the original!");
}
}
else
{
Id = other.Id;
}

Object = other.Object;

if (other.Delta != null)
{
if (other.Delta.StepDetails != null)
Expand All @@ -239,10 +253,14 @@ internal void AppendFrom(RunStepResponse other)
}
}

Delta = other.Delta;

// don't update other fields if we are just appending Delta
return;
}

Delta = null;

if (other.CreatedAtUnixTimeSeconds.HasValue)
{
CreatedAtUnixTimeSeconds = other.CreatedAtUnixTimeSeconds;
Expand Down
43 changes: 43 additions & 0 deletions Tests/TestFixture_03_Threads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,49 @@ public async Task Test_03_03_01_CreateRun_Streaming()
var run = await thread.CreateRunAsync(assistant, streamEvent =>
{
Debug.Log(streamEvent.ToJsonString());

switch (streamEvent)
{
case RunResponse runEvent:
Assert.NotNull(runEvent);
break;
case RunStepResponse runStepEvent:
Assert.NotNull(runStepEvent);
switch (runStepEvent.Object)
{
case "thread.run.step.delta":
Assert.NotNull(runStepEvent.Delta);
break;
default:
Assert.IsNull(runStepEvent.Delta);
break;
}
break;
case ThreadResponse threadEvent:
Assert.NotNull(threadEvent);
break;
case MessageResponse messageEvent:
Assert.NotNull(messageEvent);
switch (messageEvent.Object)
{
case "thread.message.delta":
Assert.NotNull(messageEvent.Delta);
Debug.Log($"{messageEvent.Object}: \"{messageEvent.Delta.PrintContent()}\"");
break;
default:
Debug.Log($"{messageEvent.Object}: \"{messageEvent.PrintContent()}\"");
Assert.IsNull(messageEvent.Delta);
break;
}
break;
case Error errorEvent:
Assert.NotNull(errorEvent);
break;
//default:
// handle event not already processed by library
// var @event = JsonSerializer.Deserialize<T>(streamEvent.ToJsonString());
//break;
}
});

Assert.IsNotNull(run);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "OpenAI",
"description": "A OpenAI package for the Unity Game Engine to use GPT-4, GPT-3.5, GPT-3 and Dall-E though their RESTful API (currently in beta).\n\nIndependently developed, this is not an official library and I am not affiliated with OpenAI.\n\nAn OpenAI API account is required.",
"keywords": [],
"version": "8.0.2",
"version": "8.0.3",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.openai.unity#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.openai.unity/releases",
Expand Down

0 comments on commit 7ee1c10

Please sign in to comment.