Skip to content

Commit

Permalink
Replaced FileData -> FileResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Nov 19, 2023
1 parent f2d445e commit a11cff7
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 19 deletions.
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Tests/TestFixture_09_FineTuning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace OpenAI.Tests
{
internal class TestFixture_09_FineTuning : AbstractTestFixture
{
private async Task<FileData> CreateTestTrainingDataAsync()
private async Task<FileResponse> CreateTestTrainingDataAsync()
{
var conersations = new List<Conversation>
{
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Tests/TestFixture_11_Assistants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class TestFixture_11_Assistants : AbstractTestFixture
{
private static string testAssistantId;

private async Task<FileData> CreateTestFileAsync()
private async Task<FileResponse> CreateTestFileAsync()
{
const string testFilePath = "assistant_test.txt";
await File.WriteAllTextAsync(testFilePath, "Knowledge is power!");
Expand Down
4 changes: 2 additions & 2 deletions OpenAI-DotNet-Tests/TestFixture_12_Threads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public async Task Test_05_DeleteThread()
Console.WriteLine($"Deleted thread {testThreadId}");
}

private async Task<FileData> CreateTestFileAsync(string filePath)
private async Task<FileResponse> CreateTestFileAsync(string filePath)
{
await File.WriteAllTextAsync(filePath, "Knowledge is power!");
Assert.IsTrue(File.Exists(filePath));
Expand All @@ -171,7 +171,7 @@ private async Task<FileData> CreateTestFileAsync(string filePath)
return file;
}

private async Task CleanupFileAsync(FileData file)
private async Task CleanupFileAsync(FileResponse file)
{
var isDeleted = await OpenAIClient.FilesEndpoint.DeleteFileAsync(file);
Assert.IsTrue(isDeleted);
Expand Down
4 changes: 2 additions & 2 deletions OpenAI-DotNet/Assistants/AssistantsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public async Task<ListResponse<AssistantResponse>> ListAssistantsAsync(ListQuery
/// <param name="assistantId">The ID of the assistant for which to create a File.
/// </param>
/// <param name="file">
/// A <see cref="FileData"/> (with purpose="assistants") that the assistant should use.
/// A <see cref="FileResponse"/> (with purpose="assistants") that the assistant should use.
/// Useful for tools like retrieval and code_interpreter that can access files.
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="AssistantFileResponse"/>.</returns>
public async Task<AssistantFileResponse> CreateAssistantFileAsync(string assistantId, FileData file, CancellationToken cancellationToken = default)
public async Task<AssistantFileResponse> CreateAssistantFileAsync(string assistantId, FileResponse file, CancellationToken cancellationToken = default)
{
if (file?.Purpose?.Equals("assistants") != true)
{
Expand Down
3 changes: 3 additions & 0 deletions OpenAI-DotNet/Files/FileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace OpenAI.Files
/// <summary>
/// The File object represents a document that has been uploaded to OpenAI.
/// </summary>
[Obsolete("use FileResponse")]
public sealed class FileData : BaseResponse
{
/// <summary>
Expand Down Expand Up @@ -60,6 +61,8 @@ public sealed class FileData : BaseResponse

public static implicit operator string(FileData fileData) => fileData?.ToString();

public static implicit operator FileResponse(FileData fileData) => new FileResponse(fileData);

public override string ToString() => Id;
}
}
79 changes: 79 additions & 0 deletions OpenAI-DotNet/Files/FileResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Text.Json.Serialization;

namespace OpenAI.Files
{
/// <summary>
/// The File object represents a document that has been uploaded to OpenAI.
/// </summary>
public sealed class FileResponse : BaseResponse
{
public FileResponse() { }

#pragma warning disable CS0618 // Type or member is obsolete
internal FileResponse(FileData file)
{
Id = file.Id;
Object = file.Object;
Size = file.Size;
CreatedAtUnixTimeSeconds = file.CreatedAtUnixTimeSeconds;
FileName = file.FileName;
Purpose = file.Purpose;
}
#pragma warning restore CS0618 // Type or member is obsolete

/// <summary>
/// The file identifier, which can be referenced in the API endpoints.
/// </summary>
[JsonInclude]
[JsonPropertyName("id")]
public string Id { get; private set; }

/// <summary>
/// The object type, which is always 'file'.
/// </summary>
[JsonInclude]
[JsonPropertyName("object")]
public string Object { get; private set; }

/// <summary>
/// The size of the file, in bytes.
/// </summary>
[JsonInclude]
[JsonPropertyName("bytes")]
public int Size { get; private set; }

/// <summary>
/// The Unix timestamp (in seconds) for when the file was created.
/// </summary>
[JsonInclude]
[JsonPropertyName("created_at")]
public int CreatedAtUnixTimeSeconds { get; private set; }

[JsonIgnore]
[Obsolete("Use CreatedAtUnixTimeSeconds")]
public int CreatedUnixTime => CreatedAtUnixTimeSeconds;

[JsonIgnore]
public DateTime CreatedAt => DateTimeOffset.FromUnixTimeSeconds(CreatedAtUnixTimeSeconds).DateTime;

/// <summary>
/// The name of the file.
/// </summary>
[JsonInclude]
[JsonPropertyName("filename")]
public string FileName { get; private set; }

/// <summary>
/// The intended purpose of the file.
/// Supported values are 'fine-tune', 'fine-tune-results', 'assistants', and 'assistants_output'.
/// </summary>
[JsonInclude]
[JsonPropertyName("purpose")]
public string Purpose { get; private set; }

public static implicit operator string(FileResponse file) => file?.ToString();

public override string ToString() => Id;
}
}
26 changes: 13 additions & 13 deletions OpenAI-DotNet/Files/FilesEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class FilesEndpoint : BaseEndPoint
private class FilesList
{
[JsonPropertyName("data")]
public IReadOnlyList<FileData> Files { get; set; }
public IReadOnlyList<FileResponse> Files { get; set; }
}

/// <inheritdoc />
Expand All @@ -32,8 +32,8 @@ public FilesEndpoint(OpenAIClient api) : base(api) { }
/// Returns a list of files that belong to the user's organization.
/// </summary>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>List of <see cref="FileData"/>.</returns>
public async Task<IReadOnlyList<FileData>> ListFilesAsync(CancellationToken cancellationToken = default)
/// <returns>List of <see cref="FileResponse"/>.</returns>
public async Task<IReadOnlyList<FileResponse>> ListFilesAsync(CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl(), cancellationToken).ConfigureAwait(false);
var resultAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
Expand All @@ -54,8 +54,8 @@ public async Task<IReadOnlyList<FileData>> ListFilesAsync(CancellationToken canc
/// fields representing your training examples.
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="FileData"/>.</returns>
public async Task<FileData> UploadFileAsync(string filePath, string purpose, CancellationToken cancellationToken = default)
/// <returns><see cref="FileResponse"/>.</returns>
public async Task<FileResponse> UploadFileAsync(string filePath, string purpose, CancellationToken cancellationToken = default)
=> await UploadFileAsync(new FileUploadRequest(filePath, purpose), cancellationToken).ConfigureAwait(false);

/// <summary>
Expand All @@ -65,8 +65,8 @@ public async Task<FileData> UploadFileAsync(string filePath, string purpose, Can
/// </summary>
/// <param name="request"><see cref="FileUploadRequest"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="FileData"/>.</returns>
public async Task<FileData> UploadFileAsync(FileUploadRequest request, CancellationToken cancellationToken = default)
/// <returns><see cref="FileResponse"/>.</returns>
public async Task<FileResponse> UploadFileAsync(FileUploadRequest request, CancellationToken cancellationToken = default)
{
using var fileData = new MemoryStream();
using var content = new MultipartFormDataContent();
Expand All @@ -77,7 +77,7 @@ public async Task<FileData> UploadFileAsync(FileUploadRequest request, Cancellat

var response = await Api.Client.PostAsync(GetUrl(), content, cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return JsonSerializer.Deserialize<FileData>(responseAsString, OpenAIClient.JsonSerializationOptions);
return JsonSerializer.Deserialize<FileResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand Down Expand Up @@ -117,12 +117,12 @@ async Task<bool> InternalDeleteFileAsync(int attempt)
/// </summary>
/// <param name="fileId">The ID of the file to use for this request.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="FileData"/></returns>
public async Task<FileData> GetFileInfoAsync(string fileId, CancellationToken cancellationToken = default)
/// <returns><see cref="FileResponse"/></returns>
public async Task<FileResponse> GetFileInfoAsync(string fileId, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl($"/{fileId}"), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return JsonSerializer.Deserialize<FileData>(responseAsString, OpenAIClient.JsonSerializationOptions);
return JsonSerializer.Deserialize<FileResponse>(responseAsString, OpenAIClient.JsonSerializationOptions);
}

/// <summary>
Expand All @@ -142,12 +142,12 @@ public async Task<string> DownloadFileAsync(string fileId, string directory, boo
/// <summary>
/// Downloads the specified file.
/// </summary>
/// <param name="fileData"><see cref="FileData"/> to download.</param>
/// <param name="fileData"><see cref="FileResponse"/> to download.</param>
/// <param name="directory">The directory to download the file into.</param>
/// <param name="deleteCachedFile">Optional, delete cached file. Default is false.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/></param>
/// <returns>The full path of the downloaded file.</returns>
public async Task<string> DownloadFileAsync(FileData fileData, string directory, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
public async Task<string> DownloadFileAsync(FileResponse fileData, string directory, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(directory))
{
Expand Down

0 comments on commit a11cff7

Please sign in to comment.