From 54958d2e1832f13014e9a4b6346b27bef9dce459 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Sun, 14 Jan 2024 10:59:47 -0500 Subject: [PATCH] OpenAI-DotNet 7.6.2 (#222) - Fixed parameter name in Threads.CreateMessageRequest - Added Stream overload to Threads.FileUploadRequest --- .../Assistants/AssistantExtensions.cs | 16 +++++++++++++++ OpenAI-DotNet/Files/FileUploadRequest.cs | 20 ++++++++++++++++++- OpenAI-DotNet/OpenAI-DotNet.csproj | 5 ++++- OpenAI-DotNet/Threads/CreateMessageRequest.cs | 9 ++++----- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/OpenAI-DotNet/Assistants/AssistantExtensions.cs b/OpenAI-DotNet/Assistants/AssistantExtensions.cs index 9333ab33..4944b4fb 100644 --- a/OpenAI-DotNet/Assistants/AssistantExtensions.cs +++ b/OpenAI-DotNet/Assistants/AssistantExtensions.cs @@ -2,6 +2,7 @@ using OpenAI.Files; using OpenAI.Threads; +using System.IO; using System.Threading; using System.Threading.Tasks; @@ -79,6 +80,21 @@ public static async Task UploadFileAsync(this AssistantRe return await assistant.AttachFileAsync(file, cancellationToken).ConfigureAwait(false); } + /// + /// Uploads a new file at the specified path and attaches it to the assistant. + /// + /// . + /// The file contents to upload. + /// The name of the file. + /// Optional, . + /// . + + public static async Task UploadFileAsync(this AssistantResponse assistant, Stream stream, string fileName, CancellationToken cancellationToken = default) + { + var file = await assistant.Client.FilesEndpoint.UploadFileAsync(new FileUploadRequest(stream, fileName, "assistants"), cancellationToken).ConfigureAwait(false); + return await assistant.AttachFileAsync(file, cancellationToken).ConfigureAwait(false); + } + /// /// Retrieves the . /// diff --git a/OpenAI-DotNet/Files/FileUploadRequest.cs b/OpenAI-DotNet/Files/FileUploadRequest.cs index 93f5a983..db33f0eb 100644 --- a/OpenAI-DotNet/Files/FileUploadRequest.cs +++ b/OpenAI-DotNet/Files/FileUploadRequest.cs @@ -19,6 +19,7 @@ public sealed class FileUploadRequest : IDisposable /// fields representing your training examples. /// /// + /// public FileUploadRequest(string filePath, string purpose) { if (!System.IO.File.Exists(filePath)) @@ -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; + /// + /// Constructor. + /// + /// The file contents to upload. + /// The file name. + /// + /// 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. + /// + /// + 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() diff --git a/OpenAI-DotNet/OpenAI-DotNet.csproj b/OpenAI-DotNet/OpenAI-DotNet.csproj index 98fcc04e..e52797c9 100644 --- a/OpenAI-DotNet/OpenAI-DotNet.csproj +++ b/OpenAI-DotNet/OpenAI-DotNet.csproj @@ -28,8 +28,11 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet- OpenAI-DotNet.pfx True True - 7.6.1 + 7.6.2 +Version 7.6.2 +- Fixed parameter name in Threads.CreateMessageRequest +- Added Stream overload to Threads.FileUploadRequest Version 7.6.1 - Include Output in Threads.FunctionCall Version 7.6.0 diff --git a/OpenAI-DotNet/Threads/CreateMessageRequest.cs b/OpenAI-DotNet/Threads/CreateMessageRequest.cs index 556eda8e..5b1bbaba 100644 --- a/OpenAI-DotNet/Threads/CreateMessageRequest.cs +++ b/OpenAI-DotNet/Threads/CreateMessageRequest.cs @@ -8,20 +8,19 @@ namespace OpenAI.Threads { public sealed class CreateMessageRequest { - public static implicit operator CreateMessageRequest(string content) - => new CreateMessageRequest(content); + public static implicit operator CreateMessageRequest(string content) => new(content); /// /// Constructor. /// /// - /// + /// /// - public CreateMessageRequest(string content, IEnumerable fieldIds = null, IReadOnlyDictionary metadata = null) + public CreateMessageRequest(string content, IEnumerable fileIds = null, IReadOnlyDictionary metadata = null) { Role = Role.User; Content = content; - FileIds = fieldIds?.ToList(); + FileIds = fileIds?.ToList(); Metadata = metadata; }