Skip to content

Commit

Permalink
PR feedback, image test reliability for owls
Browse files Browse the repository at this point in the history
  • Loading branch information
trrwilson committed Feb 6, 2025
1 parent a1ff661 commit 50ec586
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 116 deletions.
4 changes: 2 additions & 2 deletions .dotnet/api/OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,8 +1411,8 @@ public class ChatOutputAudioReference : IJsonModel<ChatOutputAudioReference>, IP
public override readonly string ToString();
}
public class ChatOutputPrediction : IJsonModel<ChatOutputPrediction>, IPersistableModel<ChatOutputPrediction> {
public static ChatOutputPrediction CreateStaticContentPrediction(IEnumerable<ChatMessageContentPart> contentParts);
public static ChatOutputPrediction CreateStaticContentPrediction(string content);
public static ChatOutputPrediction CreateStaticContentPrediction(IEnumerable<ChatMessageContentPart> staticContentParts);
public static ChatOutputPrediction CreateStaticContentPrediction(string staticContent);
public static explicit operator ChatOutputPrediction(ClientResult result);
public static implicit operator BinaryContent(ChatOutputPrediction chatOutputPrediction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public partial class ChatMessageContent
{
internal void WriteTo(Utf8JsonWriter writer, ModelReaderWriterOptions options = null)
{
options ??= new("W");
if (Count == 0)
{
writer.WriteNullValue();
Expand Down
8 changes: 4 additions & 4 deletions .dotnet/src/Custom/Chat/ChatOutputPrediction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace OpenAI.Chat;
[CodeGenModel("ChatOutputPrediction")]
public partial class ChatOutputPrediction
{
public static ChatOutputPrediction CreateStaticContentPrediction(IEnumerable<ChatMessageContentPart> contentParts)
=> new InternalChatOutputPredictionContent(new ChatMessageContent(contentParts));
public static ChatOutputPrediction CreateStaticContentPrediction(IEnumerable<ChatMessageContentPart> staticContentParts)
=> new InternalChatOutputPredictionContent(new ChatMessageContent(staticContentParts));

public static ChatOutputPrediction CreateStaticContentPrediction(string content)
=> new InternalChatOutputPredictionContent([ChatMessageContentPart.CreateTextPart(content)]);
public static ChatOutputPrediction CreateStaticContentPrediction(string staticContent)
=> new InternalChatOutputPredictionContent([ChatMessageContentPart.CreateTextPart(staticContent)]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@ internal static void SerializeAssistantChatMessage(AssistantChatMessage instance
internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());

// Content is optional, can be a single string or a collection of ChatMessageContentPart.
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
Content.WriteTo(writer, options);
}

WriteRoleProperty(writer, options);
WriteContentProperty(writer, options);
writer.WriteOptionalProperty("refusal"u8, Refusal, options);
writer.WriteOptionalProperty("name"u8, ParticipantName, options);
writer.WriteOptionalCollection("tool_calls"u8, ToolCalls, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ internal static void WriteCore(ChatMessage instance, Utf8JsonWriter writer, Mode

internal virtual void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
=> throw new InvalidOperationException($"The {nameof(WriteCore)} method should be invoked on an overriding type derived from {nameof(ChatMessage)}.");

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void WriteRoleProperty(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void WriteContentProperty(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
Content.WriteTo(writer, options);
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,8 @@ internal static void SerializeDeveloperChatMessage(DeveloperChatMessage instance
internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());

// Content is required, can be a single string or a collection of ChatMessageContentPart.
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
if (Content.Count == 1 && Content[0].Text != null)
{
writer.WriteStringValue(Content[0].Text);
}
else
{
writer.WriteStartArray();
foreach (ChatMessageContentPart part in Content)
{
writer.WriteObjectValue(part, options);
}
writer.WriteEndArray();
}
}

WriteRoleProperty(writer, options);
WriteContentProperty(writer, options);
writer.WriteOptionalProperty("name"u8, ParticipantName, options);
writer.WriteSerializedAdditionalRawData(_additionalBinaryDataProperties, options);
writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,10 @@ internal static void SerializeFunctionChatMessage(FunctionChatMessage instance,
internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());
WriteRoleProperty(writer, options);
WriteContentProperty(writer, options);
writer.WritePropertyName("name"u8);
writer.WriteStringValue(FunctionName);

// Content is required, can be a single string or null.
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
Content.WriteTo(writer, options);
}

writer.WriteSerializedAdditionalRawData(_additionalBinaryDataProperties, options);
writer.WriteEndObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@ internal static void SerializeSystemChatMessage(SystemChatMessage instance, Utf8
internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());

// Content is required, can be a single string or a collection of ChatMessageContentPart.
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
Content.WriteTo(writer, options);
}

WriteRoleProperty(writer, options);
WriteContentProperty(writer, options);
writer.WriteOptionalProperty("name"u8, ParticipantName, options);
writer.WriteSerializedAdditionalRawData(_additionalBinaryDataProperties, options);
writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,10 @@ internal static void SerializeToolChatMessage(ToolChatMessage instance, Utf8Json
internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());
WriteRoleProperty(writer, options);
writer.WritePropertyName("tool_call_id"u8);
writer.WriteStringValue(ToolCallId);

// Content is required, can be a single string or a collection of ChatMessageContentPart.
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
if (Content.Count == 1 && Content[0].Text != null)
{
writer.WriteStringValue(Content[0].Text);
}
else
{
writer.WriteStartArray();
foreach (ChatMessageContentPart part in Content)
{
writer.WriteObjectValue(part, options);
}
writer.WriteEndArray();
}
}

WriteContentProperty(writer, options);
writer.WriteSerializedAdditionalRawData(_additionalBinaryDataProperties, options);
writer.WriteEndObject();
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,8 @@ internal static void SerializeUserChatMessage(UserChatMessage instance, Utf8Json
internal override void WriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());

// Content is required, can be a single string or a collection of ChatMessageContentPart.
if (Optional.IsDefined(Content) && Content.IsInnerCollectionDefined())
{
writer.WritePropertyName("content"u8);
Content.WriteTo(writer, options);
}

WriteRoleProperty(writer, options);
WriteContentProperty(writer, options);
writer.WriteOptionalProperty("name"u8, ParticipantName, options);
writer.WriteSerializedAdditionalRawData(_additionalBinaryDataProperties, options);
writer.WriteEndObject();
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions .dotnet/tests/Chat/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -949,8 +949,8 @@ public async Task O3miniDeveloperMessagesWork()
{
List<ChatMessage> messages =
[
ChatMessage.CreateDeveloperMessage("When asked questions about naval topics, you always talk like a pirate."),
ChatMessage.CreateUserMessage("What's the best route to sail from San Francisco to New York?")
ChatMessage.CreateDeveloperMessage("End every response to the user with the exact phrase: 'Hope this helps!'"),
ChatMessage.CreateUserMessage("How long will it take to make a cheesecake from scratch? Including getting ingredients.")
];

ChatCompletionOptions options = new()
Expand All @@ -961,8 +961,8 @@ public async Task O3miniDeveloperMessagesWork()
ChatClient client = GetTestClient<ChatClient>(TestScenario.Chat, "o3-mini");
ChatCompletion completion = await client.CompleteChatAsync(messages, options);

List<string> expectedPossibles = ["arr", "matey", "hearty", "avast"];
Assert.That(expectedPossibles.Any(expected => completion.Content[0].Text.ToLower().Contains(expected)));
Assert.That(completion.Content, Has.Count.EqualTo(1));
Assert.That(completion.Content[0].Text, Does.EndWith("Hope this helps!"));
}

[Test]
Expand Down
Loading

0 comments on commit 50ec586

Please sign in to comment.