srcnalt / OpenAI-Unity

An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.
MIT License
672 stars 150 forks source link

JsonSerializationException Error #14

Closed fanyqe closed 1 year ago

fanyqe commented 1 year ago

JsonSerializationException: Could not find member 'id' on object of type 'CreateCompletionResponse'. Path 'id', line 1, position 6. Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:2433) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:502) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:309) Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:202) Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/JsonSerializer.cs:904) Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/JsonSerializer.cs:883) Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at /root/repo/Src/Newtonsoft.Json/JsonConvert.cs:831) Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at /root/repo/Src/Newtonsoft.Json/JsonConvert.cs:787) OpenAI.OpenAIApi.DispatchRequest[T] (System.String path, System.Net.Http.HttpMethod method, System.Byte[] payload) (at Library/PackageCache/com.srcnalt.openai-unity@36ddc4ce71/Runtime/OpenAIApi.cs:70) OpenAI.OpenAIApi.CreateCompletion (OpenAI.CreateCompletionRequest request) (at Library/PackageCache/com.srcnalt.openai-unity@36ddc4ce71/Runtime/OpenAIApi.cs:148) OpenAI.ChatGPT.SendReply () (at Assets/Samples/OpenAI Unity/0.1.4/ChatGPT/ChatGPT.cs:34) System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) (at <1f66344f2f89470293d8b67d71308c07>:0) UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0) UnityEngine.UnitySynchronizationContext.Exec () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0) UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0)

srcnalt commented 1 year ago

Hi @fanyqe can you provide more info, this error message alone does not help me understand the issue. A part of the code you are running, the prompt, unity version, platform, etc.

berkingokyolcu commented 1 year ago

Same problem here. I did not add any code to your 0.1.6 version code. I just imported it. Prompt: Hi, how are you. Unity version: Unity 2021.3.11f1 This error message appeared at 0.1.5 version too. But when i delete .openai folder from users, it resolved. But this time this does not solve my problem. If you can, can you share code without json pathing?

And i'll share ChatGPT's answer: he error message indicates that the Newtonsoft.Json serializer is unable to find the 'id' member in the 'CreateCompletionResponse' object while deserializing the JSON response received from the OpenAI API.

This could happen due to one of the following reasons:

The JSON response from the API may not contain the 'id' member, which could be caused by an issue with the API response or an outdated version of the Newtonsoft.Json library. The 'CreateCompletionResponse' class may not have a public 'id' property with a getter and a setter. To fix the issue, you can try the following steps:

Ensure that you are using the latest version of the Newtonsoft.Json library. Check the API response to ensure that it contains the 'id' member. If it does not, you can try updating the API request to include the 'id' member in the response. Verify that the 'CreateCompletionResponse' class has a public 'id' property with a getter and a setter. If it does not, you can add the property to the class to resolve the issue.

srcnalt commented 1 year ago

Hi @berkingokyolcu the package uses the latest Newtonsoft.JSON for unity as a dependency I do not think that is the issue.

But when i delete .openai folder from users, it resolved.

Can you post the content of your auth.json file, without exposing your API Key? Its structure might be wrong.

Also with 0.1.6 you can now pass API key as a param to OpenAIApi ctor which then ignores reading from auth.json

berkingokyolcu commented 1 year ago

I am using 0.1.0 version and it works fine with my auth.json. Its structure is correct i assume. { "api_key" : "", "organization" : "" } When i use 0.1.5, i got same error. Then i write my API key as param at ChatGPT.cs i got same error. Then i deleted .openai folder that fixed that error. After that i got 2 more errors. One of them was saying api key tokens are ended. But i was know that if i place it as .openai folder and auth.json it works. I tried open new mail and got fresh api key. This time i got Api key is wrong error. I stopped there.

This time i am using 0.1.6 . With .openai folder, i got this error. I writed my API key as param at ChatGPT.cs and deleted .openai folder. Still remained. I did write it to ChatCPT.cs because i watched your video. After that i deleted from ChatGPT.cs and go OpenAIApi.cs and public OPENAIApi(string apiKey= null,.......), i changed null as my API key. This time i got "You exceeded your current quota, please check your plan and billing details." Like i said, i can use my api key at version 0.1.0 as json file. It works but again i created brand new account and got api key. Again this time says wrong api key. I can't solve this problem.

srcnalt commented 1 year ago

API key being wrong and quota being reached is OpenAI account side things, I can't really help with that. There might still be OpenAI API responses that I have not encountered and might need to alter response structures to handle them. Unfortunately OpenAI does not have documentation for possible error response types.

Leroks commented 1 year ago

I have the same error, I was not getting it last week. My error is:

"An error occurred while creating completion: Could not find member 'id' on object of type 'CreateCompletionResponse'. Path 'id', line 1, position 6."

Also I am giving my API key from script not from auth.json and still getting this error.

srcnalt commented 1 year ago

@Leroks can you debug from the DispatchRequest method of the package to see what is the string value you are receiving in request.downloadHandler.text?

Leroks commented 1 year ago

I debugged and request.downloadHandler.text is: {"id":"cmpl-6n4rXUljrljNPK6OlJfjb2BEAx7zx","object":"text_completion","created":1677154783,"model":"text-davinci-003","choices":[{"text":" The response I am getting here.","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":47,"completion_tokens":31,"total_tokens":78}}

The strange thing is we are deploying this application to WebGL on our servers, it gives no error to me but gives this error to others. And we can see the response from console but not at the text area because of the error.

srcnalt commented 1 year ago

Do you have a different version of Newtonsoft.JSON installed? The string above is correct, it is obviously Newtonsoft.JSON, which might be related to the system locale. Can you spot any time/language difference between your system and the server where the error happens?

mtomruk commented 1 year ago

Hi, I had a similar problem. On the device I tested, the location setting is Turkey. (Oculus Quest 2 device). On my development computer, languge setting is English. And I had no problem on that computer (Windows).

JSON respense in request.downloadHandler.text is formatted with lowercase "id", "index" etc. But the struct "CreateCompletionResponse" was expecting "Id", and the struct "Choice" was expecting "Index".

The reason for the problem was that the upper case of the Turkish letter i is I. So they do not match. I changed "Id" to "id" (struct: CreateCompletionResponse) and "Index" to "index" (struct Choice) in the "DataTypes.cs" file.

After that change my problem is fixed. I did not experience any problem on my development computer as well.

srcnalt commented 1 year ago

@mtomruk I haven't pushed the changes yet but now I have this in jsonSerialization settings: Culture = CultureInfo.InvariantCulture in OpenAIApi.cs which should help with this issue. Could you check if that works?

mtomruk commented 1 year ago

@srcnalt I tired but did not work. Then, I tried something else, now it works.

In CustomNamingStrategy.cs, I changed m.Value[0].ToString().ToLower() to m.Value[0].ToString().ToLowerInvariant()

I think that it's much better than my previous fix :)

srcnalt commented 1 year ago

Thank you, I will add this change in the evening. 🙏

mtomruk commented 1 year ago

I'm glad it helped 😄

srcnalt commented 1 year ago

Should be fixed in v0.1.8

ChrisKlingler commented 1 year ago

Hey @srcnalt, still seeing this issue in v0.1.12 with Unity 2022.2.16 running WebGL builds. Here is the JSON returned by OpenAI Chat Completion:

{"id":"chatcmpl-7Blu4ha7YR834teg22T0XYBdBk77D","object":"chat.completion","created":1683040104,"model":"gpt-3.5-turbo-0301","usage":{"prompt_tokens":70,"completion_tokens":58,"total_tokens":128},"choices":[{"message":{"role":"assistant","content":"Hello, it is a pleasure to be chatting with you today.\n\nI am feeling quite content at the moment, thank you for asking. How about yourself?\n\nDo you have any questions for me? I am happy to share my thoughts and insights on science, philosophy, and life in general."},"finish_reason":"stop","index":0}]}

But the library is throwing this error via Newtonsoft:

app.framework.js.br:10 JsonSerializationException: Could not find member 'id' on object of type 'CreateChatCompletionResponse'. Path 'id', line 1, position 6. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) [0x00000] in <00000000000000000000000000000000>:0 --- End of stack trace from previous location where exception was thrown ---

Anything else I should adjust to get this work?

srcnalt commented 1 year ago

Hi @ChrisKlingler can you try 0.1.14?

DarrenFritts commented 1 year ago

I'm using v0.1.14 and just started getting the following error. It was working perfectly last week and I haven't made any code changes or any changes at all to my project for that matter, but the error just started occurring. I tested a couple of other projects that I had used the v0.1.14 code in, and again, no code or project changes since it was working last week, but all of these projects have the same issue now.

Any assistance would be appreciated.

For reference, I'm using

JsonSerializationException: Could not find member 'warning' on object of type 'CreateChatCompletionResponse'. Path 'warning', line 1, position 11.
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject (System.Object newObject, Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.String id) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at <761cf2a144514d2291a678c334d49e9b>:0)
Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) (at <761cf2a144514d2291a678c334d49e9b>:0)
OpenAI.OpenAIApi.DispatchRequest[T] (System.String path, System.String method, System.Action`1[T] onResponse, System.Action onComplete, System.Threading.CancellationTokenSource token, System.Byte[] payload) (at ./Library/PackageCache/com.srcnalt.openai-unity@569c386fac/Runtime/OpenAIApi.cs:127)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <605bf8b31fcb444b85176da963870aa7>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <17484a9af6b944dea5cd9be4dbb0da2c>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <17484a9af6b944dea5cd9be4dbb0da2c>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <17484a9af6b944dea5cd9be4dbb0da2c>:0)
DarrenFritts commented 1 year ago

Excellent! It looks like the latest v0.1.15 update fixed the issue. Thanks for sharing this, @srcnalt. Your contributions are very much appreciated!