-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathChatHistory.cs
221 lines (191 loc) · 9.87 KB
/
ChatHistory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
#pragma warning disable CA1033 // Interface methods should be callable by child types
#pragma warning disable CA1710 // Identifiers should have correct suffix
namespace Microsoft.SemanticKernel.ChatCompletion;
/// <summary>
/// Provides a history of chat messages from a chat conversation.
/// </summary>
public class ChatHistory : IList<ChatMessageContent>, IReadOnlyList<ChatMessageContent>
{
/// <summary>The messages.</summary>
private readonly List<ChatMessageContent> _messages;
/// <summary>Initializes an empty history.</summary>
/// <summary>
/// Creates a new instance of the <see cref="ChatHistory"/> class
/// </summary>
public ChatHistory()
{
this._messages = new();
}
/// <summary>
/// Creates a new instance of the <see cref="ChatHistory"/> class with a system message
/// </summary>
/// <param name="systemMessage">The system message to add to the history.</param>
public ChatHistory(string systemMessage)
{
Verify.NotNullOrWhiteSpace(systemMessage);
this._messages = new();
this.AddSystemMessage(systemMessage);
}
/// <summary>Initializes the history will all of the specified messages.</summary>
/// <param name="messages">The messages to copy into the history.</param>
/// <exception cref="ArgumentNullException"><paramref name="messages"/> is null.</exception>
public ChatHistory(IEnumerable<ChatMessageContent> messages)
{
Verify.NotNull(messages);
this._messages = new(messages);
}
/// <summary>Gets the number of messages in the history.</summary>
public int Count => this._messages.Count;
/// <summary>
/// Add a message to the chat history
/// </summary>
/// <param name="chatMessageContent">Chat message content</param>
public void AddMessage(ChatMessageContent chatMessageContent)
{
this.Add(chatMessageContent);
}
/// <summary>
/// <param name="authorRole">Role of the message author</param>
/// <param name="content">Message content</param>
/// <param name="encoding">Encoding of the message content</param>
/// <param name="metadata">Dictionary for any additional metadata</param>
/// </summary>
public void AddMessage(AuthorRole authorRole, string content, Encoding? encoding = null, IReadOnlyDictionary<string, object?>? metadata = null) =>
this.Add(new ChatMessageContent(authorRole, content, null, null, encoding, metadata));
/// <summary>
/// <param name="authorRole">Role of the message author</param>
/// <param name="items">Instance of <see cref="ChatMessageContentItemCollection"/> with content items</param>
/// <param name="encoding">Encoding of the message content</param>
/// <param name="metadata">Dictionary for any additional metadata</param>
/// </summary>
public void AddMessage(AuthorRole authorRole, ChatMessageContentItemCollection items, Encoding? encoding = null, IReadOnlyDictionary<string, object?>? metadata = null) =>
this.Add(new ChatMessageContent(authorRole, items, null, null, encoding, metadata));
/// <summary>
/// Add a user message to the chat history
/// </summary>
/// <param name="content">Message content</param>
public void AddUserMessage(string content) =>
this.AddMessage(AuthorRole.User, content);
/// <summary>
/// Add a user message to the chat history
/// </summary>
/// <param name="items">Instance of <see cref="ChatMessageContentItemCollection"/> with content items</param>
public void AddUserMessage(ChatMessageContentItemCollection items) =>
this.AddMessage(AuthorRole.User, items);
/// <summary>
/// Add an assistant message to the chat history
/// </summary>
/// <param name="content">Message content</param>
public void AddAssistantMessage(string content) =>
this.AddMessage(AuthorRole.Assistant, content);
/// <summary>
/// Add a system message to the chat history
/// </summary>
/// <param name="content">Message content</param>
public void AddSystemMessage(string content) =>
this.AddMessage(AuthorRole.System, content);
/// <summary>Adds a message to the history.</summary>
/// <param name="item">The message to add.</param>
/// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
public void Add(ChatMessageContent item)
{
Verify.NotNull(item);
this._messages.Add(item);
}
/// <summary>Adds the messages to the history.</summary>
/// <param name="items">The collection whose messages should be added to the history.</param>
/// <exception cref="ArgumentNullException"><paramref name="items"/> is null.</exception>
public void AddRange(IEnumerable<ChatMessageContent> items)
{
Verify.NotNull(items);
this._messages.AddRange(items);
}
/// <summary>Inserts a message into the history at the specified index.</summary>
/// <param name="index">The index at which the item should be inserted.</param>
/// <param name="item">The message to insert.</param>
/// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
public void Insert(int index, ChatMessageContent item)
{
Verify.NotNull(item);
this._messages.Insert(index, item);
}
/// <summary>
/// Copies all of the messages in the history to an array, starting at the specified destination array index.
/// </summary>
/// <param name="array">The destination array into which the messages should be copied.</param>
/// <param name="arrayIndex">The zero-based index into <paramref name="array"/> at which copying should begin.</param>
/// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
/// <exception cref="ArgumentException">The number of messages in the history is greater than the available space from <paramref name="arrayIndex"/> to the end of <paramref name="array"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception>
public void CopyTo(ChatMessageContent[] array, int arrayIndex) => this._messages.CopyTo(array, arrayIndex);
/// <summary>Removes all messages from the history.</summary>
public void Clear() => this._messages.Clear();
/// <summary>Gets or sets the message at the specified index in the history.</summary>
/// <param name="index">The index of the message to get or set.</param>
/// <returns>The message at the specified index.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="index"/> was not valid for this history.</exception>
public ChatMessageContent this[int index]
{
get => this._messages[index];
set
{
Verify.NotNull(value);
this._messages[index] = value;
}
}
/// <summary>Determines whether a message is in the history.</summary>
/// <param name="item">The message to locate.</param>
/// <returns>true if the message is found in the history; otherwise, false.</returns>
/// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
public bool Contains(ChatMessageContent item)
{
Verify.NotNull(item);
return this._messages.Contains(item);
}
/// <summary>Searches for the specified message and returns the index of the first occurrence.</summary>
/// <param name="item">The message to locate.</param>
/// <returns>The index of the first found occurrence of the specified message; -1 if the message could not be found.</returns>
/// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
public int IndexOf(ChatMessageContent item)
{
Verify.NotNull(item);
return this._messages.IndexOf(item);
}
/// <summary>Removes the message at the specified index from the history.</summary>
/// <param name="index">The index of the message to remove.</param>
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="index"/> was not valid for this history.</exception>
public void RemoveAt(int index) => this._messages.RemoveAt(index);
/// <summary>Removes the first occurrence of the specified message from the history.</summary>
/// <param name="item">The message to remove from the history.</param>
/// <returns>true if the item was successfully removed; false if it wasn't located in the history.</returns>
/// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
public bool Remove(ChatMessageContent item)
{
Verify.NotNull(item);
return this._messages.Remove(item);
}
/// <summary>
/// Removes a range of messages from the history.
/// </summary>
/// <param name="index">The index of the range of elements to remove.</param>
/// <param name="count">The number of elements to remove.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than 0.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than 0.</exception>
/// <exception cref="ArgumentException"><paramref name="count"/> and <paramref name="count"/> do not denote a valid range of messages.</exception>
public void RemoveRange(int index, int count)
{
this._messages.RemoveRange(index, count);
}
/// <inheritdoc/>
bool ICollection<ChatMessageContent>.IsReadOnly => false;
/// <inheritdoc/>
IEnumerator<ChatMessageContent> IEnumerable<ChatMessageContent>.GetEnumerator() => this._messages.GetEnumerator();
/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => this._messages.GetEnumerator();
}