From eff167fc5915657bbef8c068b95fad605ff321af Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Mon, 26 Jun 2023 10:07:15 +0200 Subject: [PATCH 1/3] Add ConversationExistsAsync method #80 --- README.md | 6 ++++++ samples/ChatGptConsole/Program.cs | 6 ++++++ samples/ChatGptFunctionCallingConsole/Program.cs | 6 ++++++ src/ChatGptNet/ChatGptClient.cs | 6 ++++++ src/ChatGptNet/ChatGptMemoryCache.cs | 6 ++++++ src/ChatGptNet/IChatGptCache.cs | 8 ++++++++ src/ChatGptNet/IChatGptClient.cs | 8 ++++++++ 7 files changed, 46 insertions(+) diff --git a/README.md b/README.md index 8fa0f29..4a490b1 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,12 @@ If necessary, it is possibile to provide a custom Cache by implementing the [ICh localCache.Remove(conversationId); return Task.CompletedTask; } + + public Task ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default) + { + var exists = localCache.ContainsKey(conversationId); + return Task.FromResult(exists); + } } // Registers the custom cache at application startup. diff --git a/samples/ChatGptConsole/Program.cs b/samples/ChatGptConsole/Program.cs index e1a0976..7963359 100644 --- a/samples/ChatGptConsole/Program.cs +++ b/samples/ChatGptConsole/Program.cs @@ -63,4 +63,10 @@ public Task RemoveAsync(Guid conversationId, CancellationToken cancellationToken localCache.Remove(conversationId); return Task.CompletedTask; } + + public Task ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default) + { + var exists = localCache.ContainsKey(conversationId); + return Task.FromResult(exists); + } } \ No newline at end of file diff --git a/samples/ChatGptFunctionCallingConsole/Program.cs b/samples/ChatGptFunctionCallingConsole/Program.cs index e1a0976..7963359 100644 --- a/samples/ChatGptFunctionCallingConsole/Program.cs +++ b/samples/ChatGptFunctionCallingConsole/Program.cs @@ -63,4 +63,10 @@ public Task RemoveAsync(Guid conversationId, CancellationToken cancellationToken localCache.Remove(conversationId); return Task.CompletedTask; } + + public Task ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default) + { + var exists = localCache.ContainsKey(conversationId); + return Task.FromResult(exists); + } } \ No newline at end of file diff --git a/src/ChatGptNet/ChatGptClient.cs b/src/ChatGptNet/ChatGptClient.cs index b9f719d..1ffac11 100644 --- a/src/ChatGptNet/ChatGptClient.cs +++ b/src/ChatGptNet/ChatGptClient.cs @@ -180,6 +180,12 @@ public async Task> GetConversationAsync(Guid convers return messages; } + public async Task ConversationExistsAsync(Guid conversationId, CancellationToken cancellationToken = default) + { + var exists = await cache.ExistsAsync(conversationId, cancellationToken); + return exists; + } + public async Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false, CancellationToken cancellationToken = default) { if (!preserveSetup) diff --git a/src/ChatGptNet/ChatGptMemoryCache.cs b/src/ChatGptNet/ChatGptMemoryCache.cs index a66d0ac..32ce63c 100644 --- a/src/ChatGptNet/ChatGptMemoryCache.cs +++ b/src/ChatGptNet/ChatGptMemoryCache.cs @@ -29,4 +29,10 @@ public Task RemoveAsync(Guid conversationId, CancellationToken cancellationToken cache.Remove(conversationId); return Task.CompletedTask; } + + public Task ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default) + { + var exists = cache.TryGetValue(conversationId, out _); + return Task.FromResult(exists); + } } diff --git a/src/ChatGptNet/IChatGptCache.cs b/src/ChatGptNet/IChatGptCache.cs index 38ed484..ebf6997 100644 --- a/src/ChatGptNet/IChatGptCache.cs +++ b/src/ChatGptNet/IChatGptCache.cs @@ -34,4 +34,12 @@ public interface IChatGptCache /// The token to monitor for cancellation requests. /// The corresponding to the asynchronous operation. Task RemoveAsync(Guid conversationId, CancellationToken cancellationToken = default); + + /// + /// Gets a value that indicates whether the given conversation exists in the cache. + /// + /// The unique identifier of the conversation. + /// The token to monitor for cancellation requests. + /// The corresponding to the asynchronous operation. + Task ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/ChatGptNet/IChatGptClient.cs b/src/ChatGptNet/IChatGptClient.cs index aefc261..33b0de1 100644 --- a/src/ChatGptNet/IChatGptClient.cs +++ b/src/ChatGptNet/IChatGptClient.cs @@ -182,6 +182,14 @@ Task LoadConversationAsync(IEnumerable messages, Cancellat /// Task LoadConversationAsync(Guid conversationId, IEnumerable messages, bool replaceHistory = true, CancellationToken cancellationToken = default); + /// + /// Determines if a chat conversation exists. + /// + /// The unique identifier of the conversation. + /// The token to monitor for cancellation requests. + /// if the conversation exists; otherwise, . + public Task ConversationExistsAsync(Guid conversationId, CancellationToken cancellationToken = default); + /// /// Deletes a chat conversation, clearing all the history. /// From 4e36d48fc5dbe604596e1e67e2dac16f517a2b13 Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Mon, 26 Jun 2023 10:10:46 +0200 Subject: [PATCH 2/3] Remove unnecessary references #80 --- src/ChatGptNet/ChatGptNet.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ChatGptNet/ChatGptNet.csproj b/src/ChatGptNet/ChatGptNet.csproj index e202bc1..3f61d15 100644 --- a/src/ChatGptNet/ChatGptNet.csproj +++ b/src/ChatGptNet/ChatGptNet.csproj @@ -30,8 +30,6 @@ - - @@ -39,8 +37,6 @@ - - From 4485a83dd9ce886814f99cc90ad9b65d317c827b Mon Sep 17 00:00:00 2001 From: Marco Minerva Date: Mon, 26 Jun 2023 10:19:19 +0200 Subject: [PATCH 3/3] Documentation update #80 --- docs/ChatGptNet.Models/OpenAIChatGptModels.md | 4 ++-- .../OpenAIChatGptModels/Gpt35Turbo.md | 2 +- .../OpenAIChatGptModels/Gpt35Turbo_16k.md | 4 ++-- .../OpenAIChatGptModels/Gpt4.md | 2 +- .../OpenAIChatGptModels/Gpt4_32k.md | 4 ++-- docs/ChatGptNet/IChatGptCache.md | 1 + docs/ChatGptNet/IChatGptCache/ExistsAsync.md | 23 ++++++++++++++++++ docs/ChatGptNet/IChatGptClient.md | 1 + .../IChatGptClient/ConversationExistsAsync.md | 24 +++++++++++++++++++ src/ChatGptNet/Models/OpenAIChatGptModels.cs | 12 +++++----- 10 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 docs/ChatGptNet/IChatGptCache/ExistsAsync.md create mode 100644 docs/ChatGptNet/IChatGptClient/ConversationExistsAsync.md diff --git a/docs/ChatGptNet.Models/OpenAIChatGptModels.md b/docs/ChatGptNet.Models/OpenAIChatGptModels.md index 4b1b8e5..5d58359 100644 --- a/docs/ChatGptNet.Models/OpenAIChatGptModels.md +++ b/docs/ChatGptNet.Models/OpenAIChatGptModels.md @@ -11,9 +11,9 @@ public static class OpenAIChatGptModels | name | description | | --- | --- | | const [Gpt35Turbo](OpenAIChatGptModels/Gpt35Turbo.md) | GPT-3.5 model can understand and generate natural language or code and it is optimized for chat. | -| const [Gpt35Turbo_16k](OpenAIChatGptModels/Gpt35Turbo_16k.md) | A model with the same capabilities as the standard [`Gpt35Turbo`](./OpenAIChatGptModels/Gpt35Turbo.md) model but with 4 times the context. | +| const [Gpt35Turbo_16k](OpenAIChatGptModels/Gpt35Turbo_16k.md) | A model with the same capabilities as the standard [`Gpt35Turbo`](./OpenAIChatGptModels/Gpt35Turbo.md) model but with 4 times the token limit of [`Gpt35Turbo`](./OpenAIChatGptModels/Gpt35Turbo.md). | | const [Gpt4](OpenAIChatGptModels/Gpt4.md) | GPT-4 is a large multimodal model that can solve difficult problems with greater accuracy than any of our previous models, thanks to its broader general knowledge and advanced reasoning capabilities. is optimized for chat but works well for traditional completions tasks. | -| const [Gpt4_32k](OpenAIChatGptModels/Gpt4_32k.md) | A model with the same capabilities as the base [`Gpt4`](./OpenAIChatGptModels/Gpt4.md) model but with 4x the context length. | +| const [Gpt4_32k](OpenAIChatGptModels/Gpt4_32k.md) | A model with the same capabilities as the base [`Gpt4`](./OpenAIChatGptModels/Gpt4.md) model but with 4 times the token limit of [`Gpt4`](./OpenAIChatGptModels/Gpt4.md). | ## Remarks diff --git a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo.md b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo.md index de752ba..dc5cfd6 100644 --- a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo.md +++ b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo.md @@ -8,7 +8,7 @@ public const string Gpt35Turbo; ## Remarks -See [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5) for more information. +This model supports 4.096 tokens. See [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5) for more information. ## See Also diff --git a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo_16k.md b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo_16k.md index a776d5e..e47286b 100644 --- a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo_16k.md +++ b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt35Turbo_16k.md @@ -1,6 +1,6 @@ # OpenAIChatGptModels.Gpt35Turbo_16k field -A model with the same capabilities as the standard [`Gpt35Turbo`](./Gpt35Turbo.md) model but with 4 times the context. +A model with the same capabilities as the standard [`Gpt35Turbo`](./Gpt35Turbo.md) model but with 4 times the token limit of [`Gpt35Turbo`](./Gpt35Turbo.md). ```csharp public const string Gpt35Turbo_16k; @@ -8,7 +8,7 @@ public const string Gpt35Turbo_16k; ## Remarks -See [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5) for more information. +This model supports 16.384 tokens. See [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5) for more information. ## See Also diff --git a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4.md b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4.md index 9d8e1bd..e70861a 100644 --- a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4.md +++ b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4.md @@ -8,7 +8,7 @@ public const string Gpt4; ## Remarks -This model is currently in a limited beta and only accessible to those who have been granted access. See [GPT-4](https://platform.openai.com/docs/models/gpt-4) for more information. +This model supports 8.192 tokens and is currently in a limited beta and only accessible to those who have been granted access. See [GPT-4](https://platform.openai.com/docs/models/gpt-4) for more information. ## See Also diff --git a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4_32k.md b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4_32k.md index 48c4977..b9bcae9 100644 --- a/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4_32k.md +++ b/docs/ChatGptNet.Models/OpenAIChatGptModels/Gpt4_32k.md @@ -1,6 +1,6 @@ # OpenAIChatGptModels.Gpt4_32k field -A model with the same capabilities as the base [`Gpt4`](./Gpt4.md) model but with 4x the context length. +A model with the same capabilities as the base [`Gpt4`](./Gpt4.md) model but with 4 times the token limit of [`Gpt4`](./Gpt4.md). ```csharp public const string Gpt4_32k; @@ -8,7 +8,7 @@ public const string Gpt4_32k; ## Remarks -This model is currently in a limited beta and only accessible to those who have been granted access. See [GPT-4](https://platform.openai.com/docs/models/gpt-4) for more information. +This model supports 32.768 tokens and is currently in a limited beta and only accessible to those who have been granted access. See [GPT-4](https://platform.openai.com/docs/models/gpt-4) for more information. ## See Also diff --git a/docs/ChatGptNet/IChatGptCache.md b/docs/ChatGptNet/IChatGptCache.md index 73aa315..011d2ad 100644 --- a/docs/ChatGptNet/IChatGptCache.md +++ b/docs/ChatGptNet/IChatGptCache.md @@ -10,6 +10,7 @@ public interface IChatGptCache | name | description | | --- | --- | +| [ExistsAsync](IChatGptCache/ExistsAsync.md)(…) | Gets a value that indicates whether the given conversation exists in the cache. | | [GetAsync](IChatGptCache/GetAsync.md)(…) | Gets the list of messages for the given *conversationId*. | | [RemoveAsync](IChatGptCache/RemoveAsync.md)(…) | Removes from the cache all the message for the given *conversationId*. | | [SetAsync](IChatGptCache/SetAsync.md)(…) | Saves the list of messages for the given *conversationId*, using the specified *expiration*. | diff --git a/docs/ChatGptNet/IChatGptCache/ExistsAsync.md b/docs/ChatGptNet/IChatGptCache/ExistsAsync.md new file mode 100644 index 0000000..fdd5102 --- /dev/null +++ b/docs/ChatGptNet/IChatGptCache/ExistsAsync.md @@ -0,0 +1,23 @@ +# IChatGptCache.ExistsAsync method + +Gets a value that indicates whether the given conversation exists in the cache. + +```csharp +public Task ExistsAsync(Guid conversationId, CancellationToken cancellationToken = default) +``` + +| parameter | description | +| --- | --- | +| conversationId | The unique identifier of the conversation. | +| cancellationToken | The token to monitor for cancellation requests. | + +## Return Value + +The Task corresponding to the asynchronous operation. + +## See Also + +* interface [IChatGptCache](../IChatGptCache.md) +* namespace [ChatGptNet](../../ChatGptNet.md) + + diff --git a/docs/ChatGptNet/IChatGptClient.md b/docs/ChatGptNet/IChatGptClient.md index bfc4447..8932e54 100644 --- a/docs/ChatGptNet/IChatGptClient.md +++ b/docs/ChatGptNet/IChatGptClient.md @@ -13,6 +13,7 @@ public interface IChatGptClient | [AddFunctionResponseAsync](IChatGptClient/AddFunctionResponseAsync.md)(…) | Adds a function response to the conversation history. | | [AskAsync](IChatGptClient/AskAsync.md)(…) | Requests a new chat interaction using the default completion model specified in the [`DefaultModel`](./ChatGptOptions/DefaultModel.md) property. (4 methods) | | [AskStreamAsync](IChatGptClient/AskStreamAsync.md)(…) | Requests a new chat interaction (using the default completion model specified in the [`DefaultModel`](./ChatGptOptions/DefaultModel.md) property) with streaming response, like in ChatGPT. (2 methods) | +| [ConversationExistsAsync](IChatGptClient/ConversationExistsAsync.md)(…) | Determines if a chat conversation exists. | | [DeleteConversationAsync](IChatGptClient/DeleteConversationAsync.md)(…) | Deletes a chat conversation, clearing all the history. | | [GetConversationAsync](IChatGptClient/GetConversationAsync.md)(…) | Retrieves a chat conversation from the cache. | | [LoadConversationAsync](IChatGptClient/LoadConversationAsync.md)(…) | Loads messages into a new conversation. (2 methods) | diff --git a/docs/ChatGptNet/IChatGptClient/ConversationExistsAsync.md b/docs/ChatGptNet/IChatGptClient/ConversationExistsAsync.md new file mode 100644 index 0000000..19d8ab6 --- /dev/null +++ b/docs/ChatGptNet/IChatGptClient/ConversationExistsAsync.md @@ -0,0 +1,24 @@ +# IChatGptClient.ConversationExistsAsync method + +Determines if a chat conversation exists. + +```csharp +public Task ConversationExistsAsync(Guid conversationId, + CancellationToken cancellationToken = default) +``` + +| parameter | description | +| --- | --- | +| conversationId | The unique identifier of the conversation. | +| cancellationToken | The token to monitor for cancellation requests. | + +## Return Value + +`true` if the conversation exists; otherwise, `false`. + +## See Also + +* interface [IChatGptClient](../IChatGptClient.md) +* namespace [ChatGptNet](../../ChatGptNet.md) + + diff --git a/src/ChatGptNet/Models/OpenAIChatGptModels.cs b/src/ChatGptNet/Models/OpenAIChatGptModels.cs index 926d6e5..52aff3b 100644 --- a/src/ChatGptNet/Models/OpenAIChatGptModels.cs +++ b/src/ChatGptNet/Models/OpenAIChatGptModels.cs @@ -15,16 +15,16 @@ public static class OpenAIChatGptModels /// GPT-3.5 model can understand and generate natural language or code and it is optimized for chat. /// /// - /// See GPT-3.5 for more information. + /// This model supports 4.096 tokens. See GPT-3.5 for more information. /// /// public const string Gpt35Turbo = "gpt-3.5-turbo"; /// - /// A model with the same capabilities as the standard model but with 4 times the context. + /// A model with the same capabilities as the standard model but with 4 times the token limit of . /// /// - /// See GPT-3.5 for more information. + /// This model supports 16.384 tokens. See GPT-3.5 for more information. /// /// public const string Gpt35Turbo_16k = "gpt-3.5-turbo-16k"; @@ -33,16 +33,16 @@ public static class OpenAIChatGptModels /// GPT-4 is a large multimodal model that can solve difficult problems with greater accuracy than any of our previous models, thanks to its broader general knowledge and advanced reasoning capabilities. is optimized for chat but works well for traditional completions tasks. /// /// - /// This model is currently in a limited beta and only accessible to those who have been granted access. See GPT-4 for more information. + /// This model supports 8.192 tokens and is currently in a limited beta and only accessible to those who have been granted access. See GPT-4 for more information. /// /// public const string Gpt4 = "gpt-4"; /// - /// A model with the same capabilities as the base model but with 4x the context length. + /// A model with the same capabilities as the base model but with 4 times the token limit of . /// /// - /// This model is currently in a limited beta and only accessible to those who have been granted access. See GPT-4 for more information. + /// This model supports 32.768 tokens and is currently in a limited beta and only accessible to those who have been granted access. See GPT-4 for more information. /// /// public const string Gpt4_32k = "gpt-4-32k";