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: Could not find member 'id' on object of type 'CreateChatCompletionResponse' v0.1.12 #53

Closed ChrisKlingler closed 11 months ago

ChrisKlingler commented 1 year ago

Hey @srcnalt , I'm 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 to work?

LucienMaman commented 1 year ago

Hey @ChrisKlingler, I am facing a similar issue, I commented on a couple of steps I took in this issue: #47 -- it might help to troubleshoot. I still did not figure out, so if any progress on your side, please let me know :)

jackmca commented 1 year ago

I have also encountered the same issue. Would very much like to hear a fix.

jackmca commented 1 year ago

Update: I was able to fix this error, but this caused issue #19 to appear.

The above error was caused (for me), due to Unity stripping Newtonsoft.Json from assembly when building for IL2CPP. To prevent this from occurring, you must edit the link.xml file located in the assets folder to include the line:

<assembly fullname="Newtonsoft.Json" preserve="all"/>

Issue #19 was fixed by updating Unity to 2021.3.15f1

srcnalt commented 1 year ago

Hi folks, I tried to bring prebuild link.xml support here #68 now the package contains a link.xm for preserving OpenAI.Runtime scope, which was the main issue. Newtonsoft didn't recognize the stripped types and returned null for them. I was able to get it working in WebGL build with high code stripping unity 2020+

I'd appreciate if you could also give it a try and see if all works fine.