You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// An event called when the chat message history is too long, which should reduce message history length through whatever means is appropriate for your use case. You may want to remove the first entry in the <see cref="List{ChatMessage}"/> in the <see cref="EventArgs"/>
/// Sometimes the total length of your conversation can get too long to fit in the ChatGPT context window. In this case, the <see cref="OnTruncationNeeded"/> event will be called, if supplied. If not supplied and this is <see langword="true"/>, then the first one or more user or assistant messages will be automatically deleted from the beginning of the conversation message history until the API call succeeds. This may take some time as it may need to loop several times to clear enough messages. If this is set to false and no <see cref="OnTruncationNeeded"/> is supplied, then an <see cref="ArgumentOutOfRangeException"/> will be raised when the API returns a context_length_exceeded error.
stringmessage="The context length of this conversation is too long for the OpenAI API to handle. Consider shortening the message history by handling the OnTruncationNeeded event and removing some of the messages in the argument.";
thrownewArgumentOutOfRangeException("OnTruncationNeeded was called but it did not reduce the message history length. "+message,ex);
171
+
}
172
+
}
173
+
elseif(AutoTruncateOnContextLengthExceeded)
174
+
{
175
+
for(inti=0;i<_Messages.Count;i++)
176
+
{
177
+
if(_Messages[i].Role!=ChatMessageRole.System)
178
+
{
179
+
_Messages.RemoveAt(i);
180
+
// the messages have been truncated, so try again
181
+
returnawaitGetResponseFromChatbotAsync();
182
+
}
183
+
}
184
+
}
185
+
else
186
+
{
187
+
thrownewArgumentOutOfRangeException(message,ex);
188
+
}
189
+
}
190
+
else
191
+
{
192
+
throwex;
193
+
}
133
194
}
134
195
returnnull;
196
+
135
197
}
136
198
137
199
/// <summary>
@@ -180,13 +242,109 @@ public async Task StreamResponseFromChatbotAsync(Action<int, string> resultHandl
180
242
/// <returns>An async enumerable with each of the results as they come in. See <see href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#asynchronous-streams"/> for more details on how to consume an async enumerable.</returns>
stringmessage="The context length of this conversation is too long for the OpenAI API to handle. Consider shortening the message history by handling the OnTruncationNeeded event and removing some of the messages in the argument.";
0 commit comments