Skip to content

Commit b622276

Browse files
com.openai.unity 8.6.2 (#347)
- Fixed Assistant Reasoning Model request serialization - Added a way to set max_tokens on chat request to support older azure api - Updated predefined models - Improve AudioClip handling and performance - Convert Task.Delay to Awaiters.DelayAsync to improve performance on WebGL Platform
1 parent f4199ba commit b622276

23 files changed

+372
-107
lines changed

OpenAI/Packages/com.openai.unity/Runtime/Assistants/AssistantExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public static async Task<RunResponse> CreateThreadAndRunAsync(this AssistantResp
7979
assistant.Tools,
8080
assistant.ToolResources,
8181
assistant.Metadata,
82-
assistant.Temperature,
83-
assistant.TopP,
82+
assistant.ReasoningEffort > 0 ? null : assistant.Temperature,
83+
assistant.ReasoningEffort > 0 ? null : assistant.TopP,
8484
jsonSchema: assistant.ResponseFormatObject?.JsonSchema,
8585
responseFormat: assistant.ResponseFormat,
8686
createThreadRequest: request);

OpenAI/Packages/com.openai.unity/Runtime/Assistants/AssistantResponse.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ internal AssistantResponse(
110110
/// Tools can be of types 'code_interpreter', 'retrieval', or 'function'.
111111
/// </summary>
112112
[Preserve]
113-
[JsonProperty("tools")]
113+
[JsonProperty("tools", DefaultValueHandling = DefaultValueHandling.Ignore)]
114114
public IReadOnlyList<Tool> Tools { get; }
115115

116116
/// <summary>
@@ -120,7 +120,7 @@ internal AssistantResponse(
120120
/// while the file_search tool requires a list of vector store IDs.
121121
/// </summary>
122122
[Preserve]
123-
[JsonProperty("tool_resources")]
123+
[JsonProperty("tool_resources", DefaultValueHandling = DefaultValueHandling.Ignore)]
124124
public ToolResources ToolResources { get; }
125125

126126
/// <summary>
@@ -129,7 +129,7 @@ internal AssistantResponse(
129129
/// Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.
130130
/// </summary>
131131
[Preserve]
132-
[JsonProperty("metadata")]
132+
[JsonProperty("metadata", DefaultValueHandling = DefaultValueHandling.Ignore)]
133133
public IReadOnlyDictionary<string, string> Metadata { get; }
134134

135135
/// <summary>
@@ -138,7 +138,7 @@ internal AssistantResponse(
138138
/// while lower values like 0.2 will make it more focused and deterministic.
139139
/// </summary>
140140
[Preserve]
141-
[JsonProperty("temperature")]
141+
[JsonProperty("temperature", DefaultValueHandling = DefaultValueHandling.Ignore)]
142142
public double Temperature { get; }
143143

144144
/// <summary>
@@ -147,9 +147,18 @@ internal AssistantResponse(
147147
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
148148
/// </summary>
149149
[Preserve]
150-
[JsonProperty("top_p")]
150+
[JsonProperty("top_p", DefaultValueHandling = DefaultValueHandling.Ignore)]
151151
public double TopP { get; }
152152

153+
/// <summary>
154+
/// Constrains effort on reasoning for reasoning models.
155+
/// Currently supported values are low, medium, and high.
156+
/// Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.
157+
/// </summary>
158+
[Preserve]
159+
[JsonProperty("reasoning_effort", DefaultValueHandling = DefaultValueHandling.Ignore)]
160+
public ReasoningEffort ReasoningEffort { get; }
161+
153162
/// <summary>
154163
/// Specifies the format that the model must output.
155164
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,

OpenAI/Packages/com.openai.unity/Runtime/Assistants/CreateAssistantRequest.cs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Newtonsoft.Json;
44
using OpenAI.Extensions;
5+
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78
using UnityEngine.Scripting;
@@ -11,6 +12,36 @@ namespace OpenAI.Assistants
1112
[Preserve]
1213
public sealed class CreateAssistantRequest
1314
{
15+
[Obsolete("use new .ctr")]
16+
public CreateAssistantRequest(
17+
AssistantResponse assistant,
18+
string model,
19+
string name,
20+
string description,
21+
string instructions,
22+
IEnumerable<Tool> tools,
23+
ToolResources toolResources,
24+
IReadOnlyDictionary<string, string> metadata,
25+
double? temperature,
26+
double? topP,
27+
JsonSchema jsonSchema,
28+
ChatResponseFormat? responseFormat = null)
29+
: this(
30+
string.IsNullOrWhiteSpace(model) ? assistant.Model : model,
31+
string.IsNullOrWhiteSpace(name) ? assistant.Name : name,
32+
string.IsNullOrWhiteSpace(description) ? assistant.Description : description,
33+
string.IsNullOrWhiteSpace(instructions) ? assistant.Instructions : instructions,
34+
tools ?? assistant.Tools,
35+
toolResources ?? assistant.ToolResources,
36+
metadata ?? assistant.Metadata,
37+
temperature ?? (assistant.ReasoningEffort > 0 ? null : assistant.Temperature),
38+
topP ?? assistant.TopP,
39+
0,
40+
jsonSchema ?? assistant.ResponseFormatObject?.JsonSchema,
41+
responseFormat ?? assistant.ResponseFormat)
42+
{
43+
}
44+
1445
/// <summary>
1546
/// Constructor
1647
/// </summary>
@@ -57,6 +88,11 @@ public sealed class CreateAssistantRequest
5788
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
5889
/// We generally recommend altering this or temperature but not both.
5990
/// </param>
91+
/// <param name="reasoningEffort">
92+
/// Constrains effort on reasoning for reasoning models.
93+
/// Currently supported values are low, medium, and high.
94+
/// Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.
95+
/// </param>
6096
/// <param name="jsonSchema">
6197
/// The <see cref="JsonSchema"/> to use for structured JSON outputs.<br/>
6298
/// <see href="https://platform.openai.com/docs/guides/structured-outputs"/><br/>
@@ -83,6 +119,7 @@ public CreateAssistantRequest(
83119
IReadOnlyDictionary<string, string> metadata = null,
84120
double? temperature = null,
85121
double? topP = null,
122+
ReasoningEffort reasoningEffort = 0,
86123
JsonSchema jsonSchema = null,
87124
ChatResponseFormat? responseFormat = null)
88125
: this(
@@ -93,8 +130,9 @@ public CreateAssistantRequest(
93130
tools ?? assistant.Tools,
94131
toolResources ?? assistant.ToolResources,
95132
metadata ?? assistant.Metadata,
96-
temperature ?? assistant.Temperature,
97-
topP ?? assistant.TopP,
133+
temperature ?? (assistant.ReasoningEffort > 0 ? null : assistant.Temperature),
134+
topP ?? (assistant.ReasoningEffort > 0 ? null : assistant.TopP),
135+
reasoningEffort,
98136
jsonSchema ?? assistant.ResponseFormatObject?.JsonSchema,
99137
responseFormat ?? assistant.ResponseFormat)
100138
{
@@ -145,6 +183,11 @@ public CreateAssistantRequest(
145183
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
146184
/// We generally recommend altering this or temperature but not both.
147185
/// </param>
186+
/// <param name="reasoningEffort">
187+
/// Constrains effort on reasoning for reasoning models.
188+
/// Currently supported values are low, medium, and high.
189+
/// Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.
190+
/// </param>
148191
/// <param name="jsonSchema">
149192
/// The <see cref="JsonSchema"/> to use for structured JSON outputs.<br/>
150193
/// <see href="https://platform.openai.com/docs/guides/structured-outputs"/><br/>
@@ -170,6 +213,7 @@ public CreateAssistantRequest(
170213
IReadOnlyDictionary<string, string> metadata = null,
171214
double? temperature = null,
172215
double? topP = null,
216+
ReasoningEffort reasoningEffort = 0,
173217
JsonSchema jsonSchema = null,
174218
ChatResponseFormat responseFormat = ChatResponseFormat.Text)
175219
{
@@ -180,8 +224,9 @@ public CreateAssistantRequest(
180224
Tools = tools?.ToList();
181225
ToolResources = toolResources;
182226
Metadata = metadata;
183-
Temperature = temperature;
184-
TopP = topP;
227+
Temperature = reasoningEffort > 0 ? null : temperature;
228+
TopP = reasoningEffort > 0 ? null : topP;
229+
ReasoningEffort = reasoningEffort;
185230

186231
if (jsonSchema != null)
187232
{
@@ -232,7 +277,7 @@ public CreateAssistantRequest(
232277
/// Tools can be of types 'code_interpreter', 'retrieval', or 'function'.
233278
/// </summary>
234279
[Preserve]
235-
[JsonProperty("tools")]
280+
[JsonProperty("tools", DefaultValueHandling = DefaultValueHandling.Ignore)]
236281
public IReadOnlyList<Tool> Tools { get; }
237282

238283
/// <summary>
@@ -241,7 +286,7 @@ public CreateAssistantRequest(
241286
/// While the <see cref="Tool.FileSearch"/> requires a list vector store ids.
242287
/// </summary>
243288
[Preserve]
244-
[JsonProperty("tool_resources")]
289+
[JsonProperty("tool_resources", DefaultValueHandling = DefaultValueHandling.Ignore)]
245290
public ToolResources ToolResources { get; }
246291

247292
/// <summary>
@@ -250,7 +295,7 @@ public CreateAssistantRequest(
250295
/// while lower values like 0.2 will make it more focused and deterministic.
251296
/// </summary>
252297
[Preserve]
253-
[JsonProperty("temperature")]
298+
[JsonProperty("temperature", DefaultValueHandling = DefaultValueHandling.Ignore)]
254299
public double? Temperature { get; }
255300

256301
/// <summary>
@@ -259,9 +304,18 @@ public CreateAssistantRequest(
259304
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
260305
/// </summary>
261306
[Preserve]
262-
[JsonProperty("top_p")]
307+
[JsonProperty("top_p", DefaultValueHandling = DefaultValueHandling.Ignore)]
263308
public double? TopP { get; }
264309

310+
/// <summary>
311+
/// Constrains effort on reasoning for reasoning models.
312+
/// Currently supported values are low, medium, and high.
313+
/// Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response.
314+
/// </summary>
315+
[Preserve]
316+
[JsonProperty("reasoning_effort", DefaultValueHandling = DefaultValueHandling.Ignore)]
317+
public ReasoningEffort ReasoningEffort { get; }
318+
265319
/// <summary>
266320
/// Specifies the format that the model must output.
267321
/// Setting to <see cref="ChatResponseFormat.Json"/> or <see cref="ChatResponseFormat.JsonSchema"/> enables JSON mode,
@@ -289,7 +343,7 @@ public CreateAssistantRequest(
289343
/// Keys can be a maximum of 64 characters long and values can be a maximum of 512 characters long.
290344
/// </summary>
291345
[Preserve]
292-
[JsonProperty("metadata")]
346+
[JsonProperty("metadata", DefaultValueHandling = DefaultValueHandling.Ignore)]
293347
public IReadOnlyDictionary<string, string> Metadata { get; }
294348
}
295349
}

OpenAI/Packages/com.openai.unity/Runtime/Audio/SpeechClip.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ public AudioClip AudioClip
4040
{
4141
get
4242
{
43-
var clip = AudioClip.Create(Name, AudioSamples.Length, 1, AudioSettings.outputSampleRate, false);
44-
clip.SetData(AudioSamples, 0);
45-
return clip;
43+
if (audioClip == null)
44+
{
45+
audioClip = AudioClip.Create(Name, AudioSamples.Length, 1, AudioSettings.outputSampleRate, false);
46+
audioClip.SetData(AudioSamples, 0);
47+
}
48+
49+
return audioClip;
4650
}
4751
}
4852

53+
private AudioClip audioClip;
54+
55+
[Preserve]
56+
public float Length => AudioSamples.Length / (float)AudioSettings.outputSampleRate;
57+
4958
[Preserve]
5059
public static implicit operator AudioClip(SpeechClip clip) => clip?.AudioClip;
5160

OpenAI/Packages/com.openai.unity/Runtime/Batch/BatchExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using Utilities.Async;
67

78
namespace OpenAI.Batch
89
{
@@ -34,8 +35,7 @@ public static async Task<BatchResponse> WaitForStatusChangeAsync(this BatchRespo
3435
BatchResponse result;
3536
do
3637
{
37-
await Task.Delay(pollingInterval ?? 500, chainedCts.Token).ConfigureAwait(true);
38-
cancellationToken.ThrowIfCancellationRequested();
38+
await Awaiters.DelayAsync(pollingInterval ?? 500, chainedCts.Token).ConfigureAwait(true);
3939
result = await batchResponse.UpdateAsync(cancellationToken: chainedCts.Token);
4040
} while (result.Status is BatchStatus.NotStarted or BatchStatus.InProgress or BatchStatus.Cancelling);
4141
return result;

OpenAI/Packages/com.openai.unity/Runtime/Chat/AudioOutput.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ internal AudioOutput(
3939
? DateTimeOffset.FromUnixTimeSeconds(ExpiresAtUnixSeconds.Value).DateTime
4040
: null;
4141

42-
private Memory<byte> audioData;
43-
4442
[Preserve]
4543
[JsonIgnore]
4644
public ReadOnlyMemory<byte> AudioData
4745
{
4846
get
4947
{
50-
audioData = Convert.FromBase64String(Data);
48+
if (audioData.Length == 0)
49+
{
50+
audioData = Convert.FromBase64String(Data);
51+
}
52+
5153
return audioData;
5254
}
5355
}
56+
private Memory<byte> audioData;
5457

5558
[Preserve]
5659
[JsonIgnore]
@@ -59,19 +62,29 @@ public ReadOnlyMemory<byte> AudioData
5962
[Preserve]
6063
[JsonIgnore]
6164
public float[] AudioSamples
62-
=> PCMEncoder.Decode(AudioData.ToArray(), inputSampleRate: 24000, outputSampleRate: AudioSettings.outputSampleRate);
65+
=> audioSamples ??= PCMEncoder.Decode(AudioData.ToArray(), inputSampleRate: 24000, outputSampleRate: AudioSettings.outputSampleRate);
66+
private float[] audioSamples;
67+
68+
[Preserve]
69+
[JsonIgnore]
70+
public float Length => AudioSamples.Length / (float)AudioSettings.outputSampleRate;
6371

6472
[Preserve]
6573
[JsonIgnore]
6674
public AudioClip AudioClip
6775
{
6876
get
6977
{
70-
var audioClip = AudioClip.Create(Id, AudioSamples.Length, 1, AudioSettings.outputSampleRate, false);
71-
audioClip.SetData(AudioSamples, 0);
78+
if (audioClip == null)
79+
{
80+
audioClip = AudioClip.Create(Id, AudioSamples.Length, 1, AudioSettings.outputSampleRate, false);
81+
audioClip.SetData(AudioSamples, 0);
82+
}
83+
7284
return audioClip;
7385
}
7486
}
87+
private AudioClip audioClip;
7588

7689
[Preserve]
7790
[JsonIgnore]
@@ -80,6 +93,10 @@ public AudioClip AudioClip
8093
[Preserve]
8194
public override string ToString() => Transcript ?? string.Empty;
8295

96+
[Preserve]
97+
public static implicit operator AudioClip(AudioOutput audioOutput) => audioOutput?.AudioClip;
98+
99+
[Preserve]
83100
internal void AppendFrom(AudioOutput other)
84101
{
85102
if (other == null) { return; }

0 commit comments

Comments
 (0)