Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RetrieveFileStreamAsync method added #228

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_08_Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,21 @@ public async Task Test_04_DeleteFiles()
Assert.IsNotNull(fileList);
Assert.IsEmpty(fileList);
}

[Test]
public async Task Test_05_RetrieveFileStreamAsync()
{
Assert.IsNotNull(OpenAIClient.FilesEndpoint);
var fileList = await OpenAIClient.FilesEndpoint.ListFilesAsync();

Assert.IsNotNull(fileList);
Assert.IsNotEmpty(fileList);

var testFileData = fileList[0];
var result = await OpenAIClient.FilesEndpoint.RetrieveFileStreamAsync(testFileData);

Assert.IsNotNull(result);
Assert.IsTrue(result.CanRead);
}
}
}
13 changes: 12 additions & 1 deletion OpenAI-DotNet/Files/FilesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,21 @@ public async Task<string> DownloadFileAsync(FileResponse fileData, string direct
}
}

await using var response = await client.Client.GetStreamAsync(GetUrl($"/{fileData.Id}/content"), cancellationToken).ConfigureAwait(false);
await using var response = await RetrieveFileStreamAsync(fileData, cancellationToken).ConfigureAwait(false);
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
await response.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
return filePath;
}

/// <summary>
/// Gets the specified file as stream
/// </summary>
/// <param name="fileData"><see cref="FileResponse"/> to download.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/></param>
/// <returns>The file as a stream in an asynchronous operation.</returns>
public async Task<Stream> RetrieveFileStreamAsync(FileResponse fileData, CancellationToken cancellationToken = default)
{
return await client.Client.GetStreamAsync(GetUrl($"/{fileData.Id}/content"), cancellationToken).ConfigureAwait(false);
}
}
}
4 changes: 3 additions & 1 deletion OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ 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.2</Version>
<Version>7.6.3</Version>
<PackageReleaseNotes>
Version 7.6.3
- Added RetrieveFileStreamAsync method to Files.FilesEndpoint
Version 7.6.2
- Fixed parameter name in Threads.CreateMessageRequest
- Added Stream overload to Threads.FileUploadRequest
Expand Down