Skip to content

Commit

Permalink
Documentation update
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Apr 17, 2023
1 parent 8756272 commit f7322f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion samples/ChatGptConsole/ChatGptConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
31 changes: 14 additions & 17 deletions src/ChatGptNet/ChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,8 @@ public Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = fa
return Task.CompletedTask;
}

public Task<Guid> LoadConversationAsync(Guid conversationId, IEnumerable<ChatGptMessage> messages,
bool replaceHistory = true)
public Task<Guid> LoadConversationAsync(Guid conversationId, IEnumerable<ChatGptMessage> messages, bool replaceHistory = true)
{
// Ensures that messages aren't null.
ArgumentNullException.ThrowIfNull(messages);

// Ensures that conversationId isn't empty.
Expand All @@ -211,17 +209,18 @@ public Task<Guid> LoadConversationAsync(Guid conversationId, IEnumerable<ChatGpt

if (replaceHistory)
{
LoadToCache(conversationId, messages);
// If messages must replace history, just use the current list, discarding all the previously cached content.
// If messages.Count() > ChatGptOptions.MessageLimit, the UpdateCache take care of taking only the last messages.
UpdateCache(conversationId, messages);
}
else
{
var conversationHistory = cache.Get<IList<ChatGptMessage>>(conversationId);
// Retrieves the current history and adds new messages.
var conversationHistory = cache.Get<List<ChatGptMessage>>(conversationId) ?? new List<ChatGptMessage>();
conversationHistory.AddRange(messages);

List<ChatGptMessage> history = conversationHistory is not null ? new(conversationHistory) : new();

history.AddRange(messages);

LoadToCache(conversationId, history);
// If messages total length > ChatGptOptions.MessageLimit, the UpdateCache take care of taking only the last messages.
UpdateCache(conversationId, conversationHistory);
}

return Task.FromResult(conversationId);
Expand Down Expand Up @@ -260,11 +259,10 @@ private ChatGptRequest CreateRequest(IList<ChatGptMessage> messages, bool stream
private void UpdateHistory(Guid conversationId, IList<ChatGptMessage> messages, ChatGptMessage message)
{
messages.Add(message);

LoadToCache(conversationId, messages);
UpdateCache(conversationId, messages);
}

private void LoadToCache(Guid conversationId, IEnumerable<ChatGptMessage> messages)
private void UpdateCache(Guid conversationId, IEnumerable<ChatGptMessage> messages)
{
// If the maximum number of messages has been reached, deletes the oldest ones.
// Note: system message does not count for message limit.
Expand All @@ -275,11 +273,10 @@ private void LoadToCache(Guid conversationId, IEnumerable<ChatGptMessage> messag
conversation = conversation.TakeLast(options.MessageLimit);

// If the first message was of role system, adds it back in.
var gptMessage = messages.First();

if (gptMessage.Role == ChatGptRoles.System)
var firstMessage = messages.First();
if (firstMessage.Role == ChatGptRoles.System)
{
conversation = conversation.Prepend(gptMessage);
conversation = conversation.Prepend(firstMessage);
}

messages = conversation.ToList();
Expand Down
38 changes: 23 additions & 15 deletions src/ChatGptNet/IChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IChatGptClient
/// Setups a new conversation with a system message, that is used to influence assistant behavior.
/// </summary>
/// <param name="message">The system message.</param>
/// <returns>The new Conversation Id.</returns>
/// <returns>The unique identifier of the new conversation.</returns>
/// <remarks>This method creates a new conversation with a system message and a random Conversation Id. Then, call <see cref="AskAsync(Guid, string, ChatGptParameters, string, CancellationToken)"/> with this Id to start the actual conversation.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="message"/> is <see langword="null"/>.</exception>
/// <seealso cref="AskAsync(Guid, string, ChatGptParameters, string, CancellationToken)"/>
Expand Down Expand Up @@ -106,31 +106,39 @@ IAsyncEnumerable<ChatGptResponse> AskStreamAsync(string message, ChatGptParamete
/// <seealso cref="ChatGptMessage"/>
Task<IEnumerable<ChatGptMessage>> GetConversationAsync(Guid conversationId);

/// <summary>
/// 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>
/// <seealso cref="SetupAsync(Guid, string)"/>
Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false);

/// <summary>
/// Loads messages into a new conversation.
/// </summary>
/// <param name="messages">Messages to load into a new conversation</param>
/// <returns>The unique identifier of the new conversation</returns>
/// <param name="messages">Messages to load into a new conversation.</param>
/// <returns>The unique identifier of the new conversation.</returns>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>
/// <remarks>
/// <para>This method creates a new conversation with a random Conversation Id. Then, call <see cref="AskAsync(Guid, string, ChatGptParameters, string, CancellationToken)"/> with this Id to start the actual conversation.</para>
/// <para>The total number of messages never exceeds the message limit defined in <see cref="ChatGptOptions.MessageLimit"/>. If <paramref name="messages"/> contains more, only the latest ones are loaded.</para>
/// </remarks>
/// <seealso cref="ChatGptOptions.MessageLimit"/>
/// <seealso cref="AskStreamAsync(Guid, string, ChatGptParameters?, string?, CancellationToken)"/>
Task<Guid> LoadConversationAsync(IEnumerable<ChatGptMessage> messages)
=> LoadConversationAsync(Guid.NewGuid(), messages);

/// <summary>
/// Loads messages into conversation history.
/// </summary>
/// <param name="conversationId"> The unique identifier of the conversation.</param>
/// <param name="conversationId">The unique identifier of the conversation.</param>
/// <param name="messages">The messages to load into conversation history.</param>
/// <param name="replaceHistory">The flag to clear the preserved conversation history.</param>
/// <returns>The unique identifier of the conversation</returns>
/// <param name="replaceHistory"><see langword="true"/> to replace all the existing messages; <see langword="false"/> to mantain them.</param>
/// <returns>The unique identifier of the conversation.</returns>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is <see langword="null"/>.</exception>
/// <remarks>The total number of messages never exceeds the message limit defined in <see cref="ChatGptOptions.MessageLimit"/>. If <paramref name="messages"/> contains more, only the latest ones are loaded.</remarks>
/// <seealso cref="ChatGptOptions.MessageLimit"/>
Task<Guid> LoadConversationAsync(Guid conversationId, IEnumerable<ChatGptMessage> messages, bool replaceHistory = true);

/// <summary>
/// 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>
/// <seealso cref="SetupAsync(Guid, string)"/>
Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false);
}
2 changes: 1 addition & 1 deletion src/ChatGptNet/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.3",
"version": "1.4",
"publicReleaseRefSpec": [
"^refs/heads/master$" // we release out of master
],
Expand Down

0 comments on commit f7322f4

Please sign in to comment.