Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
renamed a few assistant methods
  • Loading branch information
StephenHodgson committed Nov 20, 2023
1 parent 6e8ec42 commit 07cf855
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 33 deletions.
42 changes: 34 additions & 8 deletions OpenAI-DotNet/Assistants/AssistantExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async Task<RunResponse> CreateThreadAndRunAsync(this AssistantResp
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ListResponse{AssistantFile}"/>.</returns>
public static async Task<ListResponse<AssistantFileResponse>> ListFilesAsync(this AssistantResponse assistant, ListQuery query = null, CancellationToken cancellationToken = default)
=> await assistant.Client.AssistantsEndpoint.ListAssistantFilesAsync(assistant.Id, query, cancellationToken).ConfigureAwait(false);
=> await assistant.Client.AssistantsEndpoint.ListFilesAsync(assistant.Id, query, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Attach a file to the <see cref="assistant"/>.
Expand All @@ -62,7 +62,7 @@ public static async Task<ListResponse<AssistantFileResponse>> ListFilesAsync(thi
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="AssistantFileResponse"/>.</returns>
public static async Task<AssistantFileResponse> AttachFileAsync(this AssistantResponse assistant, FileResponse file, CancellationToken cancellationToken = default)
=> await assistant.Client.AssistantsEndpoint.AttachAssistantFileAsync(assistant.Id, file, cancellationToken).ConfigureAwait(false);
=> await assistant.Client.AssistantsEndpoint.AttachFileAsync(assistant.Id, file, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Uploads a new file at the specified <see cref="filePath"/> and attaches it to the <see cref="assistant"/>.
Expand All @@ -85,7 +85,7 @@ public static async Task<AssistantFileResponse> UploadFileAsync(this AssistantRe
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="AssistantFileResponse"/>.</returns>
public static async Task<AssistantFileResponse> RetrieveFileAsync(this AssistantResponse assistant, string fileId, CancellationToken cancellationToken = default)
=> await assistant.Client.AssistantsEndpoint.RetrieveAssistantFileAsync(assistant.Id, fileId, cancellationToken).ConfigureAwait(false);
=> await assistant.Client.AssistantsEndpoint.RetrieveFileAsync(assistant.Id, fileId, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Downloads the <see cref="assistantFile"/> to the specified <see cref="directory"/>.
Expand All @@ -99,19 +99,45 @@ public static async Task<string> DownloadFileAsync(this AssistantFileResponse as
=> await assistantFile.Client.FilesEndpoint.DownloadFileAsync(assistantFile.Id, directory, deleteCachedFile, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Remove <see cref="assistant"/> file.
/// Remove AssistantFile.
/// </summary>
/// <remarks>
/// Note that removing an AssistantFile does not delete the original File object,
/// it simply removes the association between that File and the Assistant.
/// To delete a File, use <see cref="DeleteFileAsync"/>.
/// To delete a File, use <see cref="DeleteFileAsync(AssistantFileResponse,CancellationToken)"/>.
/// </remarks>
/// <param name="file"><see cref="AssistantResponse"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>True, if file was removed.</returns>
public static async Task<bool> RemoveFileAsync(this AssistantFileResponse file, CancellationToken cancellationToken = default)
=> await file.Client.AssistantsEndpoint.RemoveFileAsync(file, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Remove AssistantFile.
/// </summary>
/// <remarks>
/// Note that removing an AssistantFile does not delete the original File object,
/// it simply removes the association between that File and the Assistant.
/// To delete a File, use <see cref="DeleteFileAsync(AssistantFileResponse,CancellationToken)"/>.
/// </remarks>
/// <param name="assistant"><see cref="AssistantResponse"/>.</param>
/// <param name="fileId">The ID of the file to delete.</param>
/// <param name="fileId">The ID of the file to remove.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>True, if file was removed.</returns>
public static async Task<bool> RemoveFileAsync(this AssistantResponse assistant, string fileId, CancellationToken cancellationToken = default)
=> await assistant.Client.AssistantsEndpoint.RemoveAssistantFileAsync(assistant.Id, fileId, cancellationToken).ConfigureAwait(false);
=> await assistant.Client.AssistantsEndpoint.RemoveFileAsync(assistant.Id, fileId, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Removes and Deletes a file from the assistant.
/// </summary>
/// <param name="file"><see cref="AssistantResponse"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>True, if the file was successfully removed from the assistant and deleted.</returns>
public static async Task<bool> DeleteFileAsync(this AssistantFileResponse file, CancellationToken cancellationToken = default)
{
var isRemoved = await file.Client.AssistantsEndpoint.RemoveFileAsync(file, cancellationToken).ConfigureAwait(false);
return isRemoved && await file.Client.FilesEndpoint.DeleteFileAsync(file.Id, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Removes and Deletes a file from the <see cref="assistant"/>.
Expand All @@ -122,7 +148,7 @@ public static async Task<bool> RemoveFileAsync(this AssistantResponse assistant,
/// <returns>True, if the file was successfully removed from the assistant and deleted.</returns>
public static async Task<bool> DeleteFileAsync(this AssistantResponse assistant, string fileId, CancellationToken cancellationToken = default)
{
var isRemoved = await assistant.RemoveFileAsync(fileId, cancellationToken).ConfigureAwait(false);
var isRemoved = await assistant.Client.AssistantsEndpoint.RemoveFileAsync(assistant.Id, fileId, cancellationToken).ConfigureAwait(false);
return isRemoved && await assistant.Client.FilesEndpoint.DeleteFileAsync(fileId, cancellationToken).ConfigureAwait(false);
}

Expand Down
55 changes: 32 additions & 23 deletions OpenAI-DotNet/Assistants/AssistantsEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ internal AssistantsEndpoint(OpenAIClient api) : base(api) { }

protected override string Root => "assistants";

/// <summary>
/// Get list of assistants.
/// </summary>
/// <param name="query"><see cref="ListQuery"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ListResponse{Assistant}"/></returns>
public async Task<ListResponse<AssistantResponse>> ListAssistantsAsync(ListQuery query = null, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl(queryParameters: query), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<ListResponse<AssistantResponse>>(responseAsString, Api);
}

/// <summary>
/// Create an assistant.
/// </summary>
Expand Down Expand Up @@ -69,21 +82,22 @@ public async Task<bool> DeleteAssistantAsync(string assistantId, CancellationTok
return JsonSerializer.Deserialize<DeletedResponse>(responseAsString, OpenAIClient.JsonSerializationOptions)?.Deleted ?? false;
}

#region Files

/// <summary>
/// Get list of assistants.
/// Returns a list of assistant files.
/// </summary>
/// <param name="assistantId">The ID of the assistant the file belongs to.</param>
/// <param name="query"><see cref="ListQuery"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ListResponse{Assistant}"/></returns>
public async Task<ListResponse<AssistantResponse>> ListAssistantsAsync(ListQuery query = null, CancellationToken cancellationToken = default)
/// <returns><see cref="ListResponse{AssistantFile}"/>.</returns>
public async Task<ListResponse<AssistantFileResponse>> ListFilesAsync(string assistantId, ListQuery query = null, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl(queryParameters: query), cancellationToken).ConfigureAwait(false);
var response = await Api.Client.GetAsync(GetUrl($"/{assistantId}/files", query), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<ListResponse<AssistantResponse>>(responseAsString, Api);
return response.Deserialize<ListResponse<AssistantFileResponse>>(responseAsString, Api);
}

#region Files

/// <summary>
/// Attach a file to an assistant.
/// </summary>
Expand All @@ -94,7 +108,7 @@ public async Task<ListResponse<AssistantResponse>> ListAssistantsAsync(ListQuery
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="AssistantFileResponse"/>.</returns>
public async Task<AssistantFileResponse> AttachAssistantFileAsync(string assistantId, FileResponse file, CancellationToken cancellationToken = default)
public async Task<AssistantFileResponse> AttachFileAsync(string assistantId, FileResponse file, CancellationToken cancellationToken = default)
{
if (file?.Purpose?.Equals("assistants") != true)
{
Expand All @@ -108,18 +122,13 @@ public async Task<AssistantFileResponse> AttachAssistantFileAsync(string assista
}

/// <summary>
/// Returns a list of assistant files.
/// Retrieves the latest version of the AssistantFile.
/// </summary>
/// <param name="assistantId">The ID of the assistant the file belongs to.</param>
/// <param name="query"><see cref="ListQuery"/>.</param>
/// <param name="file"><see cref="AssistantFileResponse"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="ListResponse{AssistantFile}"/>.</returns>
public async Task<ListResponse<AssistantFileResponse>> ListAssistantFilesAsync(string assistantId, ListQuery query = null, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl($"/{assistantId}/files", query), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<ListResponse<AssistantFileResponse>>(responseAsString, Api);
}
/// <returns><see cref="AssistantFileResponse"/>.</returns>
public async Task<AssistantFileResponse> RetrieveFileAsync(AssistantFileResponse file, CancellationToken cancellationToken = default)
=> await RetrieveFileAsync(file.AssistantId, file.Id, cancellationToken);

/// <summary>
/// Retrieves an AssistantFile.
Expand All @@ -128,15 +137,15 @@ public async Task<ListResponse<AssistantFileResponse>> ListAssistantFilesAsync(s
/// <param name="fileId">The ID of the file we're getting.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="AssistantFileResponse"/>.</returns>
public async Task<AssistantFileResponse> RetrieveAssistantFileAsync(string assistantId, string fileId, CancellationToken cancellationToken = default)
public async Task<AssistantFileResponse> RetrieveFileAsync(string assistantId, string fileId, CancellationToken cancellationToken = default)
{
var response = await Api.Client.GetAsync(GetUrl($"/{assistantId}/files/{fileId}"), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
return response.Deserialize<AssistantFileResponse>(responseAsString, Api);
}

/// <summary>
/// Delete an assistant file.
/// Remove an assistant file.
/// </summary>
/// <remarks>
/// Note that removing an AssistantFile does not delete the original File object,
Expand All @@ -146,8 +155,8 @@ public async Task<AssistantFileResponse> RetrieveAssistantFileAsync(string assis
/// <param name="file"><see cref="AssistantFileResponse"/>.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>True, if file was removed.</returns>
public async Task<bool> RemoveAssistantFileAsync(AssistantFileResponse file, CancellationToken cancellationToken = default)
=> await RemoveAssistantFileAsync(file.AssistantId, file.Id, cancellationToken).ConfigureAwait(false);
public async Task<bool> RemoveFileAsync(AssistantFileResponse file, CancellationToken cancellationToken = default)
=> await RemoveFileAsync(file.AssistantId, file.Id, cancellationToken).ConfigureAwait(false);

/// <summary>
/// Remove an assistant file.
Expand All @@ -161,7 +170,7 @@ public async Task<bool> RemoveAssistantFileAsync(AssistantFileResponse file, Can
/// <param name="fileId">The ID of the file to delete.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>True, if file was removed.</returns>
public async Task<bool> RemoveAssistantFileAsync(string assistantId, string fileId, CancellationToken cancellationToken = default)
public async Task<bool> RemoveFileAsync(string assistantId, string fileId, CancellationToken cancellationToken = default)
{
var response = await Api.Client.DeleteAsync(GetUrl($"/{assistantId}/files/{fileId}"), cancellationToken).ConfigureAwait(false);
var responseAsString = await response.ReadAsStringAsync(EnableDebug, cancellationToken).ConfigureAwait(false);
Expand Down
4 changes: 2 additions & 2 deletions OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet-
- Updated ImagesEndpoint return types to ImageResult list
- Updated FilesEndpoint.ListFilesAsync with optional purpose filter query parameter.
- Refactored list responses with a more generic ListQuery and ListResponse&lt;TObject&gt; pattern
- EventList -&gt; ListResponse&lt;Event&gt;
- FineTuneJobList -&gt; ListResponse&lt;FineTuneJob&gt;
- EventList -&gt; ListResponse&lt;EventResponse&gt;
- FineTuneJobList -&gt; ListResponse&lt;FineTuneJobResponse&gt;
- Standardized names for timestamps to have suffix: UnixTimeSeconds
- Standardized response class names (existing classes depreciated)
- FileData -&gt; FileResponse
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ Install-Package OpenAI-DotNet
- [Retrieve Models](#retrieve-model)
- [Delete Fine Tuned Model](#delete-fine-tuned-model)
- [Assistants](#assistants) :new:
- [List Assistants](#list-assistants)
- [Create Assistant](#create-assistant) :new:
- [Retrieve Assistant](#retrieve-assistant) :new:
- [Modify Assistant](#modify=assistant) :new:
- [Delete Assistant](#delete-assitant) :new:
- [Attach File to Assistant](#attach-file-to-assistant)
- [Remove File from Assistant](#remove=file-from-assistant)
- [Delete File from Assistant](#delete-file-fromassistant)
- [Completions](#completions)
- [Streaming](#completion-streaming)
- [Chat](#chat)
Expand Down

0 comments on commit 07cf855

Please sign in to comment.