Skip to content

Commit

Permalink
Bugfix and documentation update #100
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Aug 11, 2023
1 parent 2299cc6 commit 587ebd7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/ChatGptNet/IChatGptClient/AddFunctionResponseAsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The Task corresponding to the asynchronous operation.

| exception | condition |
| --- | --- |
| ArgumentNullException | *functionName* or *content* are `null`. |
| InvalidOperationException | The conversation history is empty. |

## See Also
Expand Down
22 changes: 10 additions & 12 deletions src/ChatGptNet/ChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,17 @@ public async Task<Guid> LoadConversationAsync(Guid conversationId, IEnumerable<C
// Ensures that conversationId isn't empty.
conversationId = (conversationId == Guid.Empty) ? Guid.NewGuid() : conversationId;

if (replaceHistory)
// If messages must replace history, just use the current list, discarding all the previously cached content.
if (!replaceHistory)
{
// If messages must replace history, just use the current list, discarding all the previously cached content.
// If messages.Count() > ChatGptOptions.MessageLimit, the UpdateCacheAsync method takes care of taking only the last messages.
await UpdateCacheAsync(conversationId, messages, cancellationToken);
}
else
{
// Retrieves the current history and adds new messages.
// Otherwise, retrieves the current history and adds the messages.
var conversationHistory = await cache.GetAsync(conversationId, cancellationToken) ?? Enumerable.Empty<ChatGptMessage>();
conversationHistory = conversationHistory.Union(messages);

// If messages.Count() > ChatGptOptions.MessageLimit, the UpdateCacheAsync method takes care of taking only the last messages.
await UpdateCacheAsync(conversationId, conversationHistory, cancellationToken);
messages = conversationHistory.Union(messages);
}

// If messages.Count() > ChatGptOptions.MessageLimit, the UpdateCacheAsync method takes care of taking only the last messages.
await UpdateCacheAsync(conversationId, messages.ToList(), cancellationToken);

return conversationId;
}

Expand Down Expand Up @@ -265,6 +260,9 @@ public async Task AddInteractionAsync(Guid conversationId, string question, stri

public async Task AddFunctionResponseAsync(Guid conversationId, string functionName, string content, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(functionName);
ArgumentNullException.ThrowIfNull(content);

var messages = await cache.GetAsync(conversationId, cancellationToken);
if (!messages?.Any() ?? true)
{
Expand Down
1 change: 1 addition & 0 deletions src/ChatGptNet/IChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Task<Guid> LoadConversationAsync(IEnumerable<ChatGptMessage> messages, Cancellat
/// <param name="content">The content of the function response.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The <see cref="Task"/> corresponding to the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException"><paramref name="functionName"/> or <paramref name="content"/> are <see langword="null"/>.</exception>
/// <exception cref="InvalidOperationException">The conversation history is empty.</exception>
/// <seealso cref="AskAsync(Guid, string, ChatGptParameters?, string?, bool, CancellationToken)"/>
/// <seealso cref="AskStreamAsync(Guid, string, ChatGptParameters?, string?, bool, CancellationToken)"/>
Expand Down

0 comments on commit 587ebd7

Please sign in to comment.