Skip to content

Commit

Permalink
com.openai.unity 7.3.2 (#165)
Browse files Browse the repository at this point in the history
- Fixed parameter name in Threads.CreateMessageRequest
- Added Stream overload to Threads.FileUploadRequest
  • Loading branch information
StephenHodgson authored Jan 14, 2024
1 parent 4cc9032 commit bd2a727
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using OpenAI.Files;
using OpenAI.Threads;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -79,6 +80,21 @@ public static async Task<AssistantFileResponse> UploadFileAsync(this AssistantRe
return await assistant.AttachFileAsync(file, cancellationToken);
}

/// <summary>
/// Uploads a new file at the specified path and attaches it to the assistant.
/// </summary>
/// <param name="assistant"><see cref="AssistantResponse"/>.</param>
/// <param name="stream">The file contents to upload.</param>
/// <param name="fileName">The name of the file.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="AssistantFileResponse"/>.</returns>

public static async Task<AssistantFileResponse> UploadFileAsync(this AssistantResponse assistant, Stream stream, string fileName, CancellationToken cancellationToken = default)
{
var file = await assistant.Client.FilesEndpoint.UploadFileAsync(new FileUploadRequest(stream, fileName, "assistants"), uploadProgress: null, cancellationToken);
return await assistant.AttachFileAsync(file, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Retrieves the <see cref="AssistantFileResponse"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class FileUploadRequest : IDisposable
/// fields representing your training examples.
/// </param>
/// <exception cref="FileNotFoundException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public FileUploadRequest(string filePath, string purpose)
{
if (!System.IO.File.Exists(filePath))
Expand All @@ -28,8 +29,25 @@ public FileUploadRequest(string filePath, string purpose)

File = System.IO.File.OpenRead(filePath);
FileName = Path.GetFileName(filePath);
Purpose = string.IsNullOrWhiteSpace(purpose) ? throw new InvalidOperationException("The file must have a purpose") : purpose;
}

Purpose = purpose;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="stream">The file contents to upload.</param>
/// <param name="fileName">The file name.</param>
/// <param name="purpose">
/// The intended purpose of the uploaded documents.
/// If the purpose is set to "fine-tune", each line is a JSON record with "prompt" and "completion"
/// fields representing your training examples.
/// </param>
/// <exception cref="InvalidOperationException"></exception>
public FileUploadRequest(Stream stream, string fileName, string purpose)
{
File = stream;
FileName = string.IsNullOrWhiteSpace(fileName) ? throw new InvalidOperationException("Must provide a valid file name") : fileName;
Purpose = string.IsNullOrWhiteSpace(purpose) ? throw new InvalidOperationException("The file must have a purpose") : purpose;
}

~FileUploadRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public sealed class CreateMessageRequest
/// Constructor.
/// </summary>
/// <param name="content"></param>
/// <param name="fieldIds"></param>
/// <param name="fileIds"></param>
/// <param name="metadata"></param>
[Preserve]
public CreateMessageRequest(string content, IEnumerable<string> fieldIds = null, IReadOnlyDictionary<string, string> metadata = null)
public CreateMessageRequest(string content, IEnumerable<string> fileIds = null, IReadOnlyDictionary<string, string> metadata = null)
{
Role = Role.User;
Content = content;
FileIds = fieldIds?.ToList();
FileIds = fileIds?.ToList();
Metadata = metadata;
}

Expand Down
2 changes: 1 addition & 1 deletion OpenAI/Packages/com.openai.unity/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": "7.3.1",
"version": "7.3.2",
"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 bd2a727

Please sign in to comment.