Skip to content

Commit

Permalink
OpenAI-DotNet 7.6.2 (#222)
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 2606b7f commit 54958d2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
16 changes: 16 additions & 0 deletions OpenAI-DotNet/Assistants/AssistantExtensions.cs
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).ConfigureAwait(false);
}

/// <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"), cancellationToken).ConfigureAwait(false);
return await assistant.AttachFileAsync(file, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Retrieves the <see cref="AssistantFileResponse"/>.
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion OpenAI-DotNet/Files/FileUploadRequest.cs
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
5 changes: 4 additions & 1 deletion OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet-
<AssemblyOriginatorKeyFile>OpenAI-DotNet.pfx</AssemblyOriginatorKeyFile>
<IncludeSymbols>True</IncludeSymbols>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<Version>7.6.1</Version>
<Version>7.6.2</Version>
<PackageReleaseNotes>
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
Expand Down
9 changes: 4 additions & 5 deletions OpenAI-DotNet/Threads/CreateMessageRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// Constructor.
/// </summary>
/// <param name="content"></param>
/// <param name="fieldIds"></param>
/// <param name="fileIds"></param>
/// <param name="metadata"></param>
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

0 comments on commit 54958d2

Please sign in to comment.