|
| 1 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 2 | + |
| 3 | +using OpenAI.Extensions; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace OpenAI.Responses |
| 11 | +{ |
| 12 | + public sealed class ConversationsEndpoint : OpenAIBaseEndpoint |
| 13 | + { |
| 14 | + public ConversationsEndpoint(OpenAIClient client) : base(client) { } |
| 15 | + |
| 16 | + protected override string Root => "conversations"; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Create a conversation. |
| 20 | + /// </summary> |
| 21 | + /// <param name="request"><see cref="CreateConversationRequest"/>.</param> |
| 22 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 23 | + /// <returns><see cref="Conversation"/>.</returns> |
| 24 | + public async Task<Conversation> CreateConversationAsync(CreateConversationRequest request, CancellationToken cancellationToken = default) |
| 25 | + { |
| 26 | + var payload = JsonSerializer.Serialize(request, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(); |
| 27 | + var response = await HttpClient.PostAsync(GetUrl(), payload, cancellationToken).ConfigureAwait(false); |
| 28 | + return await response.DeserializeAsync<Conversation>(EnableDebug, payload, client, cancellationToken).ConfigureAwait(false); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Get a conversation. |
| 34 | + /// </summary> |
| 35 | + /// <param name="conversationId">The id of the conversation to retrieve.</param> |
| 36 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 37 | + /// <returns><see cref="Conversation"/>.</returns> |
| 38 | + public async Task<Conversation> GetConversationAsync(string conversationId, CancellationToken cancellationToken = default) |
| 39 | + { |
| 40 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(conversationId)); |
| 43 | + } |
| 44 | + |
| 45 | + var response = await HttpClient.GetAsync(GetUrl($"/{conversationId}"), cancellationToken).ConfigureAwait(false); |
| 46 | + return await response.DeserializeAsync<Conversation>(EnableDebug, client, cancellationToken).ConfigureAwait(false); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Update a conversation. |
| 52 | + /// </summary> |
| 53 | + /// <param name="conversationId"> |
| 54 | + /// The id of the conversation to retrieve. |
| 55 | + /// </param> |
| 56 | + /// <param name="metadata"> |
| 57 | + /// Set of 16 key-value pairs that can be attached to an object. |
| 58 | + /// This can be useful for storing additional information about the object in a structured format, |
| 59 | + /// and querying for objects via API or the dashboard. |
| 60 | + /// Keys are strings with a maximum length of 64 characters.Values are strings with a maximum length of 512 characters. |
| 61 | + /// </param> |
| 62 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 63 | + /// <returns><see cref="Conversation"/>.</returns> |
| 64 | + public async Task<Conversation> UpdateConversationAsync(string conversationId, IReadOnlyDictionary<string, string> metadata, CancellationToken cancellationToken = default) |
| 65 | + { |
| 66 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 67 | + { |
| 68 | + throw new ArgumentNullException(nameof(conversationId)); |
| 69 | + } |
| 70 | + |
| 71 | + var payload = JsonSerializer.Serialize(new { metadata }, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(); |
| 72 | + var response = await HttpClient.PatchAsync(GetUrl($"/{conversationId}"), payload, cancellationToken).ConfigureAwait(false); |
| 73 | + return await response.DeserializeAsync<Conversation>(EnableDebug, payload, client, cancellationToken).ConfigureAwait(false); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Delete a conversation. |
| 79 | + /// </summary> |
| 80 | + /// <remarks> |
| 81 | + /// Items in the conversation will not be deleted. |
| 82 | + /// </remarks> |
| 83 | + /// <param name="conversationId">The id of the conversation to retrieve.</param> |
| 84 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 85 | + /// <returns>True, if the <see cref="Conversation"/> was deleted successfully, otherwise False.</returns> |
| 86 | + public async Task<bool> DeleteConversationAsync(string conversationId, CancellationToken cancellationToken = default) |
| 87 | + { |
| 88 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 89 | + { |
| 90 | + throw new ArgumentNullException(nameof(conversationId)); |
| 91 | + } |
| 92 | + |
| 93 | + var response = await HttpClient.DeleteAsync(GetUrl($"/{conversationId}"), cancellationToken).ConfigureAwait(false); |
| 94 | + var result = await response.DeserializeAsync<DeletedResponse>(EnableDebug, client, cancellationToken).ConfigureAwait(false); |
| 95 | + return result.Deleted; |
| 96 | + } |
| 97 | + |
| 98 | + #region Conversation Items |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// List all items for a conversation with the given ID. |
| 102 | + /// </summary> |
| 103 | + /// <param name="conversationId">The ID of the conversation to list items for.</param> |
| 104 | + /// <param name="query">Optional, <see cref="ListQuery"/>.</param> |
| 105 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 106 | + /// <returns><see cref="ListResponse{IResponseItem}"/>.</returns> |
| 107 | + public async Task<ListResponse<IResponseItem>> ListConversationItemsAsync(string conversationId, ListQuery query = null, CancellationToken cancellationToken = default) |
| 108 | + { |
| 109 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 110 | + { |
| 111 | + throw new ArgumentNullException(nameof(conversationId)); |
| 112 | + } |
| 113 | + |
| 114 | + var response = await HttpClient.GetAsync(GetUrl($"/{conversationId}/items", query), cancellationToken).ConfigureAwait(false); |
| 115 | + return await response.DeserializeAsync<ListResponse<IResponseItem>>(EnableDebug, client, cancellationToken).ConfigureAwait(false); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Create items in a conversation with the given ID. |
| 121 | + /// </summary> |
| 122 | + /// <param name="conversationId">The ID of the conversation to add the item to.</param> |
| 123 | + /// <param name="items">The items to add to the conversation. You may add up to 20 items at a time.</param> |
| 124 | + /// <param name="include">Optional, Additional fields to include in the response.</param> |
| 125 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 126 | + /// <returns><see cref="ListResponse{IResponseItem}"/>.</returns> |
| 127 | + public async Task<ListResponse<IResponseItem>> CreateConversationItemsAsync(string conversationId, IEnumerable<IResponseItem> items, string[] include = null, CancellationToken cancellationToken = default) |
| 128 | + { |
| 129 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 130 | + { |
| 131 | + throw new ArgumentNullException(nameof(conversationId)); |
| 132 | + } |
| 133 | + |
| 134 | + if (items == null) |
| 135 | + { |
| 136 | + throw new ArgumentNullException(nameof(items)); |
| 137 | + } |
| 138 | + |
| 139 | + var payload = JsonSerializer.Serialize(new { items }, OpenAIClient.JsonSerializationOptions).ToJsonStringContent(); |
| 140 | + Dictionary<string, string> query = null; |
| 141 | + |
| 142 | + if (include is { Length: > 0 }) |
| 143 | + { |
| 144 | + query = new Dictionary<string, string> |
| 145 | + { |
| 146 | + { "include", string.Join(",", include) } |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + var response = await HttpClient.PostAsync(GetUrl($"/{conversationId}/items", query), payload, cancellationToken).ConfigureAwait(false); |
| 151 | + return await response.DeserializeAsync<ListResponse<IResponseItem>>(EnableDebug, payload, client, cancellationToken).ConfigureAwait(false); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Retrieve an item from a conversation. |
| 156 | + /// </summary> |
| 157 | + /// <param name="conversationId">The ID of the conversation that contains the item.</param> |
| 158 | + /// <param name="itemId">The ID of the item to retrieve.</param> |
| 159 | + /// <param name="include">Optional, Additional fields to include in the response.</param> |
| 160 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 161 | + /// <returns><see cref="IResponseItem"/>.</returns> |
| 162 | + public async Task<IResponseItem> GetConversationItemAsync(string conversationId, string itemId, string[] include = null, CancellationToken cancellationToken = default) |
| 163 | + { |
| 164 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 165 | + { |
| 166 | + throw new ArgumentNullException(nameof(conversationId)); |
| 167 | + } |
| 168 | + |
| 169 | + if (string.IsNullOrWhiteSpace(itemId)) |
| 170 | + { |
| 171 | + throw new ArgumentNullException(nameof(itemId)); |
| 172 | + } |
| 173 | + |
| 174 | + Dictionary<string, string> query = null; |
| 175 | + |
| 176 | + if (include is { Length: > 0 }) |
| 177 | + { |
| 178 | + query = new Dictionary<string, string> |
| 179 | + { |
| 180 | + { "include", string.Join(",", include) } |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + var response = await HttpClient.GetAsync(GetUrl($"/{conversationId}/items/{itemId}", query), cancellationToken).ConfigureAwait(false); |
| 185 | + return await response.DeserializeAsync<IResponseItem>(EnableDebug, client, cancellationToken).ConfigureAwait(false); |
| 186 | + } |
| 187 | + |
| 188 | + |
| 189 | + /// <summary> |
| 190 | + /// Delete an item from a conversation with the given IDs. |
| 191 | + /// </summary> |
| 192 | + /// <param name="conversationId">The ID of the conversation that contains the item.</param> |
| 193 | + /// <param name="itemId">The ID of the item to delete.</param> |
| 194 | + /// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> |
| 195 | + /// <returns>Returns the updated <see cref="Conversation"/>>.</returns> |
| 196 | + public async Task<Conversation> DeleteConversationItemAsync(string conversationId, string itemId, CancellationToken cancellationToken = default) |
| 197 | + { |
| 198 | + if (string.IsNullOrWhiteSpace(conversationId)) |
| 199 | + { |
| 200 | + throw new ArgumentNullException(nameof(conversationId)); |
| 201 | + } |
| 202 | + |
| 203 | + if (string.IsNullOrWhiteSpace(itemId)) |
| 204 | + { |
| 205 | + throw new ArgumentNullException(nameof(itemId)); |
| 206 | + } |
| 207 | + |
| 208 | + var response = await HttpClient.DeleteAsync(GetUrl($"/{conversationId}/items/{itemId}"), cancellationToken).ConfigureAwait(false); |
| 209 | + return await response.DeserializeAsync<Conversation>(EnableDebug, client, cancellationToken).ConfigureAwait(false); |
| 210 | + } |
| 211 | + |
| 212 | + #endregion Conversation Items |
| 213 | + } |
| 214 | +} |
0 commit comments