Skip to content

Commit 3467b53

Browse files
authored
prep files for release (#180)
1 parent 3ce26bd commit 3467b53

File tree

185 files changed

+6264
-1980
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+6264
-1980
lines changed

api/OpenAI.netstandard2.0.cs

Lines changed: 65 additions & 15 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using NUnit.Framework;
2+
using OpenAI.Chat;
3+
using System;
4+
using System.Text.Json;
5+
6+
namespace OpenAI.Examples;
7+
8+
public partial class ChatExamples
9+
{
10+
[Test]
11+
public void Example07_StructuredOutputs()
12+
{
13+
ChatClient client = new("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
14+
15+
ChatCompletionOptions options = new()
16+
{
17+
ResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
18+
name: "math_reasoning",
19+
jsonSchema: BinaryData.FromString("""
20+
{
21+
"type": "object",
22+
"properties": {
23+
"steps": {
24+
"type": "array",
25+
"items": {
26+
"type": "object",
27+
"properties": {
28+
"explanation": { "type": "string" },
29+
"output": { "type": "string" }
30+
},
31+
"required": ["explanation", "output"],
32+
"additionalProperties": false
33+
}
34+
},
35+
"final_answer": { "type": "string" }
36+
},
37+
"required": ["steps", "final_answer"],
38+
"additionalProperties": false
39+
}
40+
"""),
41+
strictSchemaEnabled: true)
42+
};
43+
44+
ChatCompletion chatCompletion = client.CompleteChat(
45+
["How can I solve 8x + 7 = -23?"],
46+
options);
47+
48+
using JsonDocument structuredJson = JsonDocument.Parse(chatCompletion.ToString());
49+
50+
Console.WriteLine($"Final answer: {structuredJson.RootElement.GetProperty("final_answer").GetString()}");
51+
Console.WriteLine("Reasoning steps:");
52+
53+
foreach (JsonElement stepElement in structuredJson.RootElement.GetProperty("steps").EnumerateArray())
54+
{
55+
Console.WriteLine($" - Explanation: {stepElement.GetProperty("explanation").GetString()}");
56+
Console.WriteLine($" Output: {stepElement.GetProperty("output")}");
57+
}
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using NUnit.Framework;
2+
using OpenAI.Chat;
3+
using System;
4+
using System.Text.Json;
5+
using System.Threading.Tasks;
6+
7+
namespace OpenAI.Examples;
8+
9+
public partial class ChatExamples
10+
{
11+
[Test]
12+
public async Task Example07_StructuredOutputsAsync()
13+
{
14+
ChatClient client = new("gpt-4o-mini", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
15+
16+
ChatCompletionOptions options = new()
17+
{
18+
ResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
19+
name: "math_reasoning",
20+
jsonSchema: BinaryData.FromString("""
21+
{
22+
"type": "object",
23+
"properties": {
24+
"steps": {
25+
"type": "array",
26+
"items": {
27+
"type": "object",
28+
"properties": {
29+
"explanation": { "type": "string" },
30+
"output": { "type": "string" }
31+
},
32+
"required": ["explanation", "output"],
33+
"additionalProperties": false
34+
}
35+
},
36+
"final_answer": { "type": "string" }
37+
},
38+
"required": ["steps", "final_answer"],
39+
"additionalProperties": false
40+
}
41+
"""),
42+
strictSchemaEnabled: true)
43+
};
44+
45+
ChatCompletion chatCompletion = await client.CompleteChatAsync(
46+
["How can I solve 8x + 7 = -23?"],
47+
options);
48+
49+
using JsonDocument structuredJson = JsonDocument.Parse(chatCompletion.ToString());
50+
51+
Console.WriteLine($"Final answer: {structuredJson.RootElement.GetProperty("final_answer").GetString()}");
52+
Console.WriteLine("Reasoning steps:");
53+
54+
foreach (JsonElement stepElement in structuredJson.RootElement.GetProperty("steps").EnumerateArray())
55+
{
56+
Console.WriteLine($" - Explanation: {stepElement.GetProperty("explanation").GetString()}");
57+
Console.WriteLine($" Output: {stepElement.GetProperty("output")}");
58+
}
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace OpenAI.Administration;
2+
3+
[CodeGenModel("AuditLogActorServiceAccount")] internal partial class InternalAuditLogActorServiceAccount { }
4+
[CodeGenModel("AuditLogActorUser")] internal partial class InternalAuditLogActorUser { }
5+
[CodeGenModel("AuditLogActorApiKey")] internal partial class InternalAuditLogActorApiKey { }
6+
[CodeGenModel("AuditLogActorSession")] internal partial class InternalAuditLogActorSession { }
7+
[CodeGenModel("AuditLogActor")] internal partial class InternalAuditLogActor { }
8+
[CodeGenModel("AuditLog")] internal partial class InternalAuditLog { }
9+
[CodeGenModel("ListAuditLogsResponse")] internal partial class InternalListAuditLogsResponse { }
10+
[CodeGenModel("Invite")] internal partial class InternalInvite { }
11+
[CodeGenModel("InviteListResponse")] internal partial class InternalInviteListResponse { }
12+
[CodeGenModel("InviteRequest")] internal partial class InternalInviteRequest { }
13+
[CodeGenModel("InviteDeleteResponse")] internal partial class InternalInviteDeleteResponse { }
14+
[CodeGenModel("User")] internal partial class InternalUser { }
15+
[CodeGenModel("UserListResponse")] internal partial class InternalUserListResponse { }
16+
[CodeGenModel("UserRoleUpdateRequest")] internal partial class InternalUserRoleUpdateRequest { }
17+
[CodeGenModel("UserDeleteResponse")] internal partial class InternalUserDeleteResponse { }
18+
[CodeGenModel("Project")] internal partial class InternalProject { }
19+
[CodeGenModel("ProjectListResponse")] internal partial class InternalProjectListResponse { }
20+
[CodeGenModel("ProjectCreateRequest")] internal partial class InternalProjectCreateRequest { }
21+
[CodeGenModel("ProjectUpdateRequest")] internal partial class InternalProjectUpdateRequest { }
22+
[CodeGenModel("DefaultProjectErrorResponse")] internal partial class InternalDefaultProjectErrorResponse { }
23+
[CodeGenModel("ProjectUser")] internal partial class InternalProjectUser { }
24+
[CodeGenModel("ProjectUserListResponse")] internal partial class InternalProjectUserListResponse { }
25+
[CodeGenModel("ProjectUserCreateRequest")] internal partial class InternalProjectUserCreateRequest { }
26+
[CodeGenModel("ProjectUserUpdateRequest")] internal partial class InternalProjectUserUpdateRequest { }
27+
[CodeGenModel("ProjectUserDeleteResponse")] internal partial class InternalProjectUserDeleteResponse { }
28+
[CodeGenModel("ProjectServiceAccount")] internal partial class InternalProjectServiceAccount { }
29+
[CodeGenModel("ProjectServiceAccountListResponse")] internal partial class InternalProjectServiceAccountListResponse { }
30+
[CodeGenModel("ProjectServiceAccountCreateRequest")] internal partial class InternalProjectServiceAccountCreateRequest { }
31+
[CodeGenModel("ProjectServiceAccountCreateResponse")] internal partial class InternalProjectServiceAccountCreateResponse { }
32+
[CodeGenModel("ProjectServiceAccountApiKey")] internal partial class InternalProjectServiceAccountApiKey { }
33+
[CodeGenModel("ProjectServiceAccountDeleteResponse")] internal partial class InternalProjectServiceAccountDeleteResponse { }
34+
[CodeGenModel("ProjectApiKey")] internal partial class InternalProjectApiKey { }
35+
[CodeGenModel("ProjectApiKeyListResponse")] internal partial class InternalProjectApiKeyListResponse { }
36+
[CodeGenModel("ProjectApiKeyDeleteResponse")] internal partial class InternalProjectApiKeyDeleteResponse { }
Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2+
using System.ClientModel;
23
using System.ClientModel.Primitives;
3-
using System.Collections.Generic;
44
using System.Text.Json;
55

66
namespace OpenAI.Assistants;
@@ -9,10 +9,33 @@ namespace OpenAI.Assistants;
99
[CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Assistants.AssistantResponseFormat>.Create", typeof(Utf8JsonReader), typeof(ModelReaderWriterOptions))]
1010
[CodeGenSuppress("global::System.ClientModel.Primitives.IPersistableModel<OpenAI.Assistants.AssistantResponseFormat>.Write", typeof(ModelReaderWriterOptions))]
1111
[CodeGenSuppress("global::System.ClientModel.Primitives.IPersistableModel<OpenAI.Assistants.AssistantResponseFormat>.Create", typeof(BinaryData), typeof(ModelReaderWriterOptions))]
12+
[CodeGenSuppress("global::System.ClientModel.Primitives.IPersistableModel<OpenAI.Assistants.AssistantResponseFormat>.GetFormatFromOptions", typeof(ModelReaderWriterOptions))]
1213
public partial class AssistantResponseFormat : IJsonModel<AssistantResponseFormat>
1314
{
15+
internal static void SerializeAssistantResponseFormat(AssistantResponseFormat instance, Utf8JsonWriter writer, ModelReaderWriterOptions options = null)
16+
{
17+
throw new InvalidOperationException();
18+
}
19+
20+
internal static AssistantResponseFormat DeserializeAssistantResponseFormat(JsonElement element, ModelReaderWriterOptions options = null)
21+
{
22+
return element.ValueKind switch
23+
{
24+
JsonValueKind.String => InternalAssistantResponseFormatPlainTextNoObject.DeserializeInternalAssistantResponseFormatPlainTextNoObject(element, options),
25+
JsonValueKind.Object when element.TryGetProperty("type", out JsonElement discriminatorElement)
26+
=> discriminatorElement.GetString() switch
27+
{
28+
"json_object" => InternalAssistantResponseFormatJsonObject.DeserializeInternalAssistantResponseFormatJsonObject(element, options),
29+
"json_schema" => InternalAssistantResponseFormatJsonSchema.DeserializeInternalAssistantResponseFormatJsonSchema(element, options),
30+
"text" => InternalAssistantResponseFormatText.DeserializeInternalAssistantResponseFormatText(element, options),
31+
_ => null,
32+
},
33+
_ => null,
34+
};
35+
}
36+
1437
void IJsonModel<AssistantResponseFormat>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
15-
=> CustomSerializationHelpers.SerializeInstance(this, SerializeAssistantResponseFormat, writer, options);
38+
=> CustomSerializationHelpers.SerializeInstance(this, SerializeAssistantResponseFormat, writer, options);
1639

1740
AssistantResponseFormat IJsonModel<AssistantResponseFormat>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
1841
=> CustomSerializationHelpers.DeserializeNewInstance(this, DeserializeAssistantResponseFormat, ref reader, options);
@@ -23,53 +46,15 @@ BinaryData IPersistableModel<AssistantResponseFormat>.Write(ModelReaderWriterOpt
2346
AssistantResponseFormat IPersistableModel<AssistantResponseFormat>.Create(BinaryData data, ModelReaderWriterOptions options)
2447
=> CustomSerializationHelpers.DeserializeNewInstance(this, DeserializeAssistantResponseFormat, data, options);
2548

26-
internal static void SerializeAssistantResponseFormat(AssistantResponseFormat formatInstance, Utf8JsonWriter writer, ModelReaderWriterOptions options)
49+
string IPersistableModel<AssistantResponseFormat>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
50+
51+
internal static AssistantResponseFormat FromResponse(PipelineResponse response)
2752
{
28-
if (formatInstance._plainTextValue is not null)
29-
{
30-
writer.WriteStringValue(formatInstance._plainTextValue);
31-
}
32-
else
33-
{
34-
writer.WriteStartObject();
35-
writer.WritePropertyName("type"u8);
36-
writer.WriteStringValue(formatInstance._objectType);
37-
writer.WriteSerializedAdditionalRawData(formatInstance.SerializedAdditionalRawData, options);
38-
writer.WriteEndObject();
39-
}
53+
throw new InvalidOperationException();
4054
}
4155

42-
internal static AssistantResponseFormat DeserializeAssistantResponseFormat(JsonElement element, ModelReaderWriterOptions options = null)
56+
internal virtual BinaryContent ToBinaryContent()
4357
{
44-
options ??= ModelSerializationExtensions.WireOptions;
45-
46-
string plainTextValue = null;
47-
string objectType = null;
48-
IDictionary<string, BinaryData> rawDataDictionary = new ChangeTrackingDictionary<string, BinaryData>();
49-
50-
if (element.ValueKind == JsonValueKind.Null)
51-
{
52-
return null;
53-
}
54-
else if (element.ValueKind == JsonValueKind.String)
55-
{
56-
plainTextValue = element.GetString();
57-
}
58-
else
59-
{
60-
foreach (var property in element.EnumerateObject())
61-
{
62-
if (property.NameEquals("type"u8))
63-
{
64-
objectType = property.Value.GetString();
65-
continue;
66-
}
67-
if (true)
68-
{
69-
rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
70-
}
71-
}
72-
}
73-
return new AssistantResponseFormat(plainTextValue, objectType, rawDataDictionary);
58+
return BinaryContent.Create(this, ModelSerializationExtensions.WireOptions);
7459
}
7560
}

0 commit comments

Comments
 (0)