srcnalt / OpenAI-Unity

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

logprobs missing from datatype ChatChoice #107

Closed chibiskuld closed 9 months ago

chibiskuld commented 9 months ago

Describe the bug We were having issues with your library due to a property supplied from the OpenAI API JSON that was not present in the data structure that it was targeting.

This was causing the JSON parser to fail in creating the datatype and causing an exception.

To get around this, I had to modify your DataTypes.cs file with alterations to ChatChoice. logprobs had to be added.

This is how I had to alter ChatChoice to work around the issue:

    public struct ChatChoice
    {
        public ChatMessage Message { get; set; }
        public ChatMessage Delta { get; set; }
        public int? Index { get; set; }
        public string FinishReason { get; set; }
        public Boolean logprobs {get; set; }
    }

You guys may want to switch to Object/Token parsing pattern for the JSON returned from the API in the future to avoid issues like this. Basically instead of relying on it to dump into a struct, parse for the individual properties instead and handle exceptions for this on changes.

Environment (please complete the following information):

Additional context Add any other context about the problem here.

owainrich commented 9 months ago

@chibiskuld thank you so much for figuring this out! I had my AI stream go down but now up and running again thanks to your fix. Massive kudos to @srcnalt for a brilliant Unity package that has been my goto for loads of projects over the last year. Your work is very much appreciated.

XyrisKennBackup commented 9 months ago

ChatChoice in Visual Basic is locked as an external library - how to make these changes? Path: ..\~Unity Dev[project folder][project name]\Library\PackageCache\com.srcnalt.openai-unity@a165a22303\Runtime\DataTypes.cs

owainrich commented 9 months ago

I'm not quite sure how Visual Basic works but I'm using Visual Studio to edit .cs files without problems. Perhaps try a different IDE?

jalemanyf commented 9 months ago

Hi, I solved this problem following this steps:

1.-The DataTypes.cs file to modify is on this path: Packages\OpenAiUnity\Runtime\DataTypes.cs

2.-If you copy/paste the struct published here, it has a very little mistake:

The last line is public Boolean logprobs {get; set; }. You have to change Boolean => bool

You only have to change this correct struct, save the file and works like a charm.

public struct ChatChoice
{
    public ChatMessage Message { get; set; }
    public ChatMessage Delta { get; set; }
    public int? Index { get; set; }
    public string FinishReason { get; set; }
    public bool logprobs {get; set; }
}

I hope it helps someone.

Josep Alemany

garethwalkom commented 9 months ago

Hi, I solved this problem following this steps:

1.-The DataTypes.cs file to modify is on this path: Packages\OpenAiUnity\Runtime\DataTypes.cs

2.-If you copy/paste the struct published here, it has a very little mistake:

The last line is public Boolean logprobs {get; set; }. You have to change Boolean => bool

You only have to change this correct struct, save the file and works like a charm.

public struct ChatChoice
{
    public ChatMessage Message { get; set; }
    public ChatMessage Delta { get; set; }
    public int? Index { get; set; }
    public string FinishReason { get; set; }
    public bool logprobs {get; set; }
}

I hope it helps someone.

Josep Alemany

I also just came up with the same fix! We must've been working on it at the same time haha.

I confirm that the fix from @jalemanyf works!

To make it flow with the addition from @srcnalt in the struct of Choice, I added the Logprobs between 'Index' and 'FinishReason'.

Atlas3DSS commented 9 months ago

thank you so much for posting this. This solved the error i was facing.

ivangarcia44 commented 9 months ago

This fixed the issue for me as well. Just by adding this line in ChatChoice of that file:

public Boolean logprobs {get; set; }

Thank you!

srcnalt commented 9 months ago

Hi folks, sorry I didnt have much time to maintain the package. Pushed these changes in 0.2.0 and removed missing member handing error level from json serializer settings, which will avoid future api/type change errors however will cause data loss during json deserializing.