Skip to content

Commit ea6b3a2

Browse files
com.openai.unity 8.0.0 (#235)
- Updated Assistants Beta v2 - Added support for specifying project id - Added BatchEndpoint - Added VectorStoresEndpoint - Added Message.ctr to specify specific tool call id, function name, and content - Renamed OpenAI.Images.ResponseFormat to OpenAI.Images.ImageResponseFormat - Changed ThreadEndpoint.CancelRunAsync return type from RunResponse to bool - Fixed Json defined Tools/Functions being improperly added to tool cache - Added Tool.TryUnregisterTool to remove a tool from the cache
1 parent d647d71 commit ea6b3a2

File tree

202 files changed

+6842
-1882
lines changed

Some content is hidden

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

202 files changed

+6842
-1882
lines changed

Documentation~/README.md

Lines changed: 424 additions & 201 deletions
Large diffs are not rendered by default.

Editor/OpenAIDashboard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace OpenAI.Editor
1717
{
18-
public class OpenAIDashboard : AbstractEditorDashboard
18+
public sealed class OpenAIDashboard : AbstractEditorDashboard
1919
{
2020
#region GUIContent
2121

Runtime/Assistants/AssistantExtensions.cs

Lines changed: 142 additions & 109 deletions
Large diffs are not rendered by default.

Runtime/Assistants/AssistantFileResponse.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed under the MIT License. See LICENSE in the project root for license information.
22

3-
using System;
43
using Newtonsoft.Json;
4+
using System;
55
using UnityEngine.Scripting;
66

77
namespace OpenAI.Assistants
@@ -10,11 +10,12 @@ namespace OpenAI.Assistants
1010
/// File attached to an assistant.
1111
/// </summary>
1212
[Preserve]
13+
[Obsolete("Removed. Use Assistant.ToolResources instead.")]
1314
public sealed class AssistantFileResponse : BaseResponse
1415
{
1516
[Preserve]
1617
[JsonConstructor]
17-
public AssistantFileResponse(
18+
internal AssistantFileResponse(
1819
string id,
1920
string @object,
2021
int createdAtUnixTimeSeconds,

Runtime/Assistants/AssistantResponse.cs

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
// Licensed under the MIT License. See LICENSE in the project root for license information.
22

33
using Newtonsoft.Json;
4+
using OpenAI.Extensions;
45
using System;
56
using System.Collections.Generic;
67
using UnityEngine.Scripting;
78

89
namespace OpenAI.Assistants
910
{
1011
/// <summary>
11-
/// Purpose-built AI that uses OpenAIs models and calls tools.
12+
/// Purpose-built AI that uses OpenAI's models and calls tools.
1213
/// </summary>
1314
[Preserve]
1415
public sealed class AssistantResponse : BaseResponse
1516
{
1617
[Preserve]
1718
[JsonConstructor]
18-
public AssistantResponse(
19-
string id,
20-
string @object,
21-
int createdAtUnixTimeSeconds,
22-
string name,
23-
string description,
24-
string model,
25-
string instructions,
26-
IReadOnlyList<Tool> tools,
27-
IReadOnlyList<string> fileIds,
28-
Dictionary<string, string> metadata)
19+
internal AssistantResponse(
20+
[JsonProperty("id")] string id,
21+
[JsonProperty("object")] string @object,
22+
[JsonProperty("created_at")] int createdAtUnixTimeSeconds,
23+
[JsonProperty("name")] string name,
24+
[JsonProperty("description")] string description,
25+
[JsonProperty("model")] string model,
26+
[JsonProperty("instructions")] string instructions,
27+
[JsonProperty("tools")] IReadOnlyList<Tool> tools,
28+
[JsonProperty("tool_resources")] ToolResources toolResources,
29+
[JsonProperty("metadata")] Dictionary<string, string> metadata,
30+
[JsonProperty("temperature")] double temperature,
31+
[JsonProperty("top_p")] double topP,
32+
[JsonProperty("response_format")][JsonConverter(typeof(ResponseFormatConverter))] ChatResponseFormat responseFormat)
2933
{
3034
Id = id;
3135
Object = @object;
@@ -35,8 +39,11 @@ public AssistantResponse(
3539
Model = model;
3640
Instructions = instructions;
3741
Tools = tools;
38-
FileIds = fileIds;
42+
ToolResources = toolResources;
3943
Metadata = metadata;
44+
Temperature = temperature;
45+
TopP = topP;
46+
ResponseFormat = responseFormat;
4047
}
4148

4249
/// <summary>
@@ -106,14 +113,24 @@ public AssistantResponse(
106113
[JsonProperty("tools")]
107114
public IReadOnlyList<Tool> Tools { get; }
108115

116+
/// <summary>
117+
/// A set of resources that are used by the assistant's tools.
118+
/// The resources are specific to the type of tool.
119+
/// For example, the code_interpreter tool requires a list of file IDs,
120+
/// while the file_search tool requires a list of vector store IDs.
121+
/// </summary>
122+
[Preserve]
123+
[JsonProperty("tool_resources")]
124+
public ToolResources ToolResources { get; }
125+
109126
/// <summary>
110127
/// A list of file IDs attached to this assistant.
111128
/// There can be a maximum of 20 files attached to the assistant.
112129
/// Files are ordered by their creation date in ascending order.
113130
/// </summary>
114-
[Preserve]
115-
[JsonProperty("file_ids")]
116-
public IReadOnlyList<string> FileIds { get; }
131+
[JsonIgnore]
132+
[Obsolete("Files removed from Assistants. Files now belong to ToolResources.")]
133+
public IReadOnlyList<string> FileIds => null;
117134

118135
/// <summary>
119136
/// Set of 16 key-value pairs that can be attached to an object.
@@ -124,6 +141,41 @@ public AssistantResponse(
124141
[JsonProperty("metadata")]
125142
public IReadOnlyDictionary<string, string> Metadata { get; }
126143

144+
/// <summary>
145+
/// What sampling temperature to use, between 0 and 2.
146+
/// Higher values like 0.8 will make the output more random,
147+
/// while lower values like 0.2 will make it more focused and deterministic.
148+
/// </summary>
149+
[Preserve]
150+
[JsonProperty("temperature")]
151+
public double Temperature { get; }
152+
153+
/// <summary>
154+
/// An alternative to sampling with temperature, called nucleus sampling,
155+
/// where the model considers the results of the tokens with top_p probability mass.
156+
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
157+
/// </summary>
158+
[Preserve]
159+
[JsonProperty("top_p")]
160+
public double TopP { get; }
161+
162+
/// <summary>
163+
/// Specifies the format that the model must output.
164+
/// Setting to <see cref="ChatResponseFormat.Json"/> enables JSON mode,
165+
/// which guarantees the message the model generates is valid JSON.
166+
/// </summary>
167+
/// <remarks>
168+
/// Important: When using JSON mode you must still instruct the model to produce JSON yourself via some conversation message,
169+
/// for example via your system message. If you don't do this, the model may generate an unending stream of
170+
/// whitespace until the generation reaches the token limit, which may take a lot of time and give the appearance
171+
/// of a "stuck" request. Also note that the message content may be partial (i.e. cut off) if finish_reason="length",
172+
/// which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
173+
/// </remarks>
174+
[Preserve]
175+
[JsonProperty("response_format")]
176+
[JsonConverter(typeof(ResponseFormatConverter))]
177+
public ChatResponseFormat ResponseFormat { get; }
178+
127179
[Preserve]
128180
public static implicit operator string(AssistantResponse assistant) => assistant?.Id;
129181

Runtime/Assistants/AssistantsEndpoint.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal AssistantsEndpoint(OpenAIClient client) : base(client) { }
2121
/// </summary>
2222
/// <param name="query"><see cref="ListQuery"/>.</param>
2323
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
24-
/// <returns><see cref="ListResponse{Assistant}"/></returns>
24+
/// <returns><see cref="ListResponse{AssistantResponse}"/></returns>
2525
public async Task<ListResponse<AssistantResponse>> ListAssistantsAsync(ListQuery query = null, CancellationToken cancellationToken = default)
2626
{
2727
var response = await Rest.GetAsync(GetUrl(queryParameters: query), parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
@@ -38,8 +38,8 @@ public async Task<ListResponse<AssistantResponse>> ListAssistantsAsync(ListQuery
3838
public async Task<AssistantResponse> CreateAssistantAsync(CreateAssistantRequest request = null, CancellationToken cancellationToken = default)
3939
{
4040
request ??= new CreateAssistantRequest();
41-
var jsonContent = JsonConvert.SerializeObject(request, OpenAIClient.JsonSerializationOptions);
42-
var response = await Rest.PostAsync(GetUrl(), jsonContent, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
41+
var payload = JsonConvert.SerializeObject(request, OpenAIClient.JsonSerializationOptions);
42+
var response = await Rest.PostAsync(GetUrl(), payload, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
4343
response.Validate(EnableDebug);
4444
return response.Deserialize<AssistantResponse>(client);
4545
}
@@ -66,8 +66,8 @@ public async Task<AssistantResponse> RetrieveAssistantAsync(string assistantId,
6666
/// <returns><see cref="AssistantResponse"/>.</returns>
6767
public async Task<AssistantResponse> ModifyAssistantAsync(string assistantId, CreateAssistantRequest request, CancellationToken cancellationToken = default)
6868
{
69-
var jsonContent = JsonConvert.SerializeObject(request, OpenAIClient.JsonSerializationOptions);
70-
var response = await Rest.PostAsync(GetUrl($"/{assistantId}"), jsonContent, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
69+
var payload = JsonConvert.SerializeObject(request, OpenAIClient.JsonSerializationOptions);
70+
var response = await Rest.PostAsync(GetUrl($"/{assistantId}"), payload, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
7171
response.Validate(EnableDebug);
7272
return response.Deserialize<AssistantResponse>(client);
7373
}
@@ -82,10 +82,10 @@ public async Task<bool> DeleteAssistantAsync(string assistantId, CancellationTok
8282
{
8383
var response = await Rest.DeleteAsync(GetUrl($"/{assistantId}"), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
8484
response.Validate(EnableDebug);
85-
return JsonConvert.DeserializeObject<DeletedResponse>(response.Body, OpenAIClient.JsonSerializationOptions)?.Deleted ?? false;
85+
return response.Deserialize<DeletedResponse>(client)?.Deleted ?? false;
8686
}
8787

88-
#region Files
88+
#region Files (Obsolete)
8989

9090
/// <summary>
9191
/// Returns a list of assistant files.
@@ -94,6 +94,7 @@ public async Task<bool> DeleteAssistantAsync(string assistantId, CancellationTok
9494
/// <param name="query"><see cref="ListQuery"/>.</param>
9595
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
9696
/// <returns><see cref="ListResponse{AssistantFile}"/>.</returns>
97+
[Obsolete("Files removed from Assistants. Files now belong to ToolResources.")]
9798
public async Task<ListResponse<AssistantFileResponse>> ListFilesAsync(string assistantId, ListQuery query = null, CancellationToken cancellationToken = default)
9899
{
99100
var response = await Rest.GetAsync(GetUrl($"/{assistantId}/files", query), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
@@ -111,15 +112,16 @@ public async Task<ListResponse<AssistantFileResponse>> ListFilesAsync(string ass
111112
/// </param>
112113
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
113114
/// <returns><see cref="AssistantFileResponse"/>.</returns>
115+
[Obsolete("Files removed from Assistants. Files now belong to ToolResources.")]
114116
public async Task<AssistantFileResponse> AttachFileAsync(string assistantId, FileResponse file, CancellationToken cancellationToken = default)
115117
{
116-
if (file?.Purpose?.Equals("assistants") != true)
118+
if (file?.Purpose?.Equals(FilePurpose.Assistants) != true)
117119
{
118120
throw new InvalidOperationException($"{nameof(file)}.{nameof(file.Purpose)} must be 'assistants'!");
119121
}
120122

121-
var jsonContent = JsonConvert.SerializeObject(new { file_id = file.Id }, OpenAIClient.JsonSerializationOptions);
122-
var response = await Rest.PostAsync(GetUrl($"/{assistantId}/files"), jsonContent, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
123+
var payload = JsonConvert.SerializeObject(new { file_id = file.Id }, OpenAIClient.JsonSerializationOptions);
124+
var response = await Rest.PostAsync(GetUrl($"/{assistantId}/files"), payload, new RestParameters(client.DefaultRequestHeaders), cancellationToken);
123125
response.Validate(EnableDebug);
124126
return response.Deserialize<AssistantFileResponse>(client);
125127
}
@@ -131,6 +133,7 @@ public async Task<AssistantFileResponse> AttachFileAsync(string assistantId, Fil
131133
/// <param name="fileId">The ID of the file we're getting.</param>
132134
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
133135
/// <returns><see cref="AssistantFileResponse"/>.</returns>
136+
[Obsolete("Files removed from Assistants. Files now belong to ToolResources.")]
134137
public async Task<AssistantFileResponse> RetrieveFileAsync(string assistantId, string fileId, CancellationToken cancellationToken = default)
135138
{
136139
var response = await Rest.GetAsync(GetUrl($"/{assistantId}/files/{fileId}"), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
@@ -150,13 +153,14 @@ public async Task<AssistantFileResponse> RetrieveFileAsync(string assistantId, s
150153
/// <param name="fileId">The ID of the file to delete.</param>
151154
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
152155
/// <returns>True, if file was removed.</returns>
156+
[Obsolete("Files removed from Assistants. Files now belong to ToolResources.")]
153157
public async Task<bool> RemoveFileAsync(string assistantId, string fileId, CancellationToken cancellationToken = default)
154158
{
155159
var response = await Rest.DeleteAsync(GetUrl($"/{assistantId}/files/{fileId}"), new RestParameters(client.DefaultRequestHeaders), cancellationToken);
156160
response.Validate(EnableDebug);
157-
return JsonConvert.DeserializeObject<DeletedResponse>(response.Body, OpenAIClient.JsonSerializationOptions)?.Deleted ?? false;
161+
return response.Deserialize<DeletedResponse>(client)?.Deleted ?? false;
158162
}
159163

160-
#endregion Files
164+
#endregion Files (Obsolete)
161165
}
162166
}

0 commit comments

Comments
 (0)