Skip to content

Commit

Permalink
added a few more thread extensions
Browse files Browse the repository at this point in the history
updated readme
  • Loading branch information
StephenHodgson committed Nov 20, 2023
1 parent 07cf855 commit 21e3024
Show file tree
Hide file tree
Showing 3 changed files with 438 additions and 101 deletions.
23 changes: 23 additions & 0 deletions OpenAI-DotNet/Threads/ThreadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,29 @@ public static async Task<ListResponse<MessageFileResponse>> ListFilesAsync(this
public static async Task<MessageFileResponse> RetrieveFileAsync(this MessageResponse message, string fileId, CancellationToken cancellationToken = default)
=> await message.Client.ThreadsEndpoint.RetrieveFileAsync(message, fileId, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Downloads a message file content to local disk.
/// </summary>
/// <param name="message"><see cref="MessageResponse"/>.</param>
/// <param name="fileId">The id of the file being retrieved.</param>
/// <param name="directory">Directory to save the file content.</param>
/// <param name="deleteCachedFile">Optional, delete cached file. Defaults to false.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>Path to the downloaded file content.</returns>
public static async Task<string> DownloadFileContentAsync(this MessageResponse message, string fileId, string directory, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
=> await message.Client.FilesEndpoint.DownloadFileAsync(fileId, directory, deleteCachedFile, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Downloads a message file content to local disk.
/// </summary>
/// <param name="file"><see cref="MessageFileResponse"/>.</param>
/// <param name="directory">Directory to save the file content.</param>
/// <param name="deleteCachedFile">Optional, delete cached file. Defaults to false.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>Path to the downloaded file content.</returns>
public static async Task<string> DownloadContentAsync(this MessageFileResponse file, string directory, bool deleteCachedFile = false, CancellationToken cancellationToken = default)
=> await file.Client.FilesEndpoint.DownloadFileAsync(file.Id, directory, deleteCachedFile, cancellationToken).ConfigureAwait(false);

#endregion Files

#region Runs
Expand Down
30 changes: 15 additions & 15 deletions OpenAI-DotNet/Threads/ThreadsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ public async Task<MessageFileResponse> RetrieveFileAsync(string threadId, string

#region Runs

/// <summary>
/// Returns a list of runs belonging to a thread.
/// </summary>
/// <param name="threadId">The id of the thread the run belongs to.</param>
/// <param name="query"><see cref="ListQuery"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ListResponse{RunResponse}"/></returns>
public async Task<ListResponse<RunResponse>> ListRunsAsync(string threadId, ListQuery query = null, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl($"/{threadId}/runs", query), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<ListResponse<RunResponse>>(responseAsString, Api);
}

/// <summary>
/// Create a run.
/// </summary>
Expand Down Expand Up @@ -286,21 +300,7 @@ public async Task<RunResponse> RetrieveRunAsync(string threadId, string runId, C
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<RunResponse>(responseAsString, Api);
}

/// <summary>
/// Returns a list of runs belonging to a thread.
/// </summary>
/// <param name="threadId">The id of the thread the run belongs to.</param>
/// <param name="query"><see cref="ListQuery"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ListResponse{RunResponse}"/></returns>
public async Task<ListResponse<RunResponse>> ListRunsAsync(string threadId, ListQuery query = null, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl($"/{threadId}/runs", query), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<ListResponse<RunResponse>>(responseAsString, Api);
}


/// <summary>
/// Modifies a run.
/// </summary>
Expand Down
Loading

0 comments on commit 21e3024

Please sign in to comment.