PawanOsman / ChatGPT.Net

C# library for ChatGPT using official OpenAI API
https://www.nuget.org/packages/ChatGPT.Net
MIT License
379 stars 71 forks source link

Fix possible deserialization failures #32

Closed Caulm closed 1 year ago

Caulm commented 1 year ago

Issue When calling ChatGptUnofficial.AskStream, an exception may be thrown if the completion time exceeds 15 seconds.

Newtonsoft.Json.JsonReaderException: Input string '2023-05-12' is not a valid number. Path '', line 1, position 10.
   at Newtonsoft.Json.JsonTextReader.ParseReadNumber(ReadType readType, Char firstChar, Int32 initialPosition)
   at Newtonsoft.Json.JsonTextReader.ParseValue()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at ChatGPT.Net.ChatGptUnofficial.SendMessage(String message, String messageId, String parentMessageId, String conversationId, String model, Action`1 callback) in E:\Study\Mirai-console\livebot\source\ChatGPT.Net\ChatGPTUnofficial.cs:line 292

Reason The conversation API will return a 'ping event' and a timestamp every 15 seconds. The format is as follows. They cannot be deserialized into JSON, resulting in exceptions.

event: ping
data: 2023-05-12 06:31:06.341774

Modification

  1. Ignore the event messages in response.
  2. Catch exceptions in the deserialization.
PawanOsman commented 1 year ago

Thank you