Skip to content

Commit cb9ddee

Browse files
authored
Added more details about managing chat history
1 parent 9a5ed0c commit cb9ddee

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

aspnetcore/tutorials/ai-powered-group-chat/ai-powered-group-chat.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,24 @@ await foreach (var completion in chatClient.CompleteChatStreamingAsync(messagesI
5555
}
5656
```
5757

58-
The chat history is managed by `GroupHistoryStore`, which stores messages for context. It includes both user and assistant messages to maintain conversation continuity.
58+
### Maintaining context with history
59+
60+
Every request to [Open AI's Chat Completions API](https://platform.openai.com/docs/guides/chat-completions) is stateless - Open AI doesn't store past interactions. In a chat application, what a user or an assistant has said is important for generating a response that's contextually relevant. We can achieve this by including chat history in every request to the Completions API.
61+
62+
The `GroupHistoryStore` class manages chat history for each group. It stores messages posted by both the users and AI assistants, ensuring that the conversation context is preserved across interactions. This context is crucial for generating coherent AI responses.
63+
64+
```csharp
65+
// Store message generated by AI-assistant in memory
66+
public void UpdateGroupHistoryForAssistant(string groupName, string message)
67+
{
68+
var chatMessages = _store.GetOrAdd(groupName, _ => InitiateChatMessages());
69+
chatMessages.Add(new AssistantChatMessage(message));
70+
}
71+
```
72+
5973
```csharp
60-
public void UpdateGroupHistoryForAssistant(string groupName, string message) { ... }
74+
// Store message generated by users in memory
75+
_history.GetOrAddGroupHistory(groupName, userName, message);
6176
```
6277

6378
### Streaming AI responses
@@ -74,17 +89,6 @@ if (totalCompletion.Length - lastSentTokenLength > 20)
7489
}
7590
```
7691

77-
### Maintaining context with history
78-
79-
The `GroupHistoryStore` class manages the chat history for each group. It stores both user and AI messages, ensuring that the conversation context is preserved across interactions. This context is crucial for generating coherent AI responses.
80-
```csharp
81-
public void UpdateGroupHistoryForAssistant(string groupName, string message)
82-
{
83-
var chatMessages = _store.GetOrAdd(groupName, _ => InitiateChatMessages());
84-
chatMessages.Add(new AssistantChatMessage(message));
85-
}
86-
```
87-
8892
## Explore further
8993

9094
This project opens up exciting possibilities for further enhancement:

0 commit comments

Comments
 (0)