Skip to content

Commit

Permalink
Add parameter to mantain conversation setup (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Apr 5, 2023
2 parents 9abbe9f + d3939cf commit 553376c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ ChatGPT is aimed to support conversational scenarios: user can talk to ChatGPT w
* *MessageLimit*: specifies how many messages for each conversation must be saved. When this limit is reached, oldest messages are automatically removed.
* *MessageExpiration*: specifies the time interval used to maintain messages in cache, regardless their count.

We can also set ChatGPT parameters for chat completion at startup. Check the [official documentation](https://platform.openai.com/docs/api-reference/chat/create) for the list of available parameters and their meaning.

The configuration can be automatically read from [IConfiguration](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration), using for example a _ChatGPT_ section in the _appsettings.json_ file:

"ChatGPT": {
Expand Down Expand Up @@ -141,7 +143,7 @@ Response streaming works by returning an [IAsyncEnumerable](https://learn.micros

**Changing the assistant's behavior**

ChatGPT supports messages with the *system* role to influence how the assistant should behave. For example, we can tell to ChatGPT something like that:
ChatGPT supports messages with the _system_ role to influence how the assistant should behave. For example, we can tell to ChatGPT something like that:

- You are an helpful assistant
- Answer like Shakespeare
Expand All @@ -161,7 +163,9 @@ The *system* message does not count for messages limit number.

Conversation history is automatically deleted when expiration time (specified by *MessageExpiration* property) is reached. However, if necessary it is possible to immediately clear the history:

await chatGptClient.DeleteConversationAsync(conversationId);
await chatGptClient.DeleteConversationAsync(conversationId, preserveSetup: false);

The _preserveSetup_ argument allows to decide whether mantain also the _system_ message that has been set with the **SetupAsync** method (default: _false_).

**Contribute**

Expand Down
25 changes: 0 additions & 25 deletions samples/ChatGptBlazor.Wasm/ChatGptBlazor.Wasm.sln

This file was deleted.

15 changes: 13 additions & 2 deletions src/ChatGptNet/ChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,20 @@ public Task<IEnumerable<ChatGptMessage>> GetConversationAsync(Guid conversationI
return Task.FromResult(messages);
}

public Task DeleteConversationAsync(Guid conversationId)
public Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false)
{
cache.Remove(conversationId);
if (!preserveSetup)
{
// We don't want to preserve setup message, so just deletes all the cache history.
cache.Remove(conversationId);
}
else if (cache.TryGetValue<List<ChatGptMessage>>(conversationId, out var messages))
{
// Removes all the messages, except system ones.
messages!.RemoveAll(m => m.Role != ChatGptRoles.System);
cache.Set(conversationId, messages, options.MessageExpiration);
}

return Task.CompletedTask;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ChatGptNet/IChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Task<Guid> SetupAsync(string message)
/// Deletes a chat conversation, clearing all the history.
/// </summary>
/// <param name="conversationId">The unique identifier of the conversation.</param>
/// <param name="preserveSetup"><see langword="true"/> to maintain the system message that has been specified with the <see cref="SetupAsync(Guid, string)"/> method.</param>
/// <returns>The <see cref="Task"/> corresponding to the asynchronous operation.</returns>
Task DeleteConversationAsync(Guid conversationId);
Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false);
}

0 comments on commit 553376c

Please sign in to comment.