Skip to content

Commit

Permalink
RetrieveFileStreamAsync method added (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
gritse authored Jan 26, 2024
1 parent 54958d2 commit 666b35d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
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

0 comments on commit 666b35d

Please sign in to comment.