An issue with Open AI API and nullable float variable(float?)

Hello! I am trying to use the OpenAI API in Unity with this library: GitHub - RageAgainstThePixel/com.openai.unity: A Non-Official OpenAI Rest Client for Unity (UPM) I want to perform speech-to-text transcription with it. I’ve successfully managed to transcribe speech to text, but when I try to set the temperature parameter in the request, I encounter an error related to the float variable not being valid. The issue is that the request function in the library requires a nullable float, but even if I use a nullable float or just a float, I still encounter the same error.

RestException: [400] https://api.openai.com/v1/audio/transcriptions Failed!
[Headers]
Date: Mon, 11 Sep 2023 09:31:46 GMT
Content-Type: application/json
Content-Length: 217
Connection: keep-alive
openai-organization: user-bm4bibgkqd6xzmawfuvsecxz
openai-processing-ms: 3
openai-version: 2020-10-01
strict-transport-security: max-age=15724800; includeSubDomains
x-ratelimit-limit-requests: 50
x-ratelimit-remaining-requests: 49
x-ratelimit-reset-requests: 1.2s
x-request-id: 20150ea72b83ae81c2615486f7f305c4
CF-Cache-Status: DYNAMIC
Server: cloudflare
CF-RAY: 804ee64daee2624f-OTP
alt-svc: h3=":443"; ma=86400
[Data] 217 bytes
[Body]
{
  "error": {
    "message": "1 validation error for Request\nbody -> temperature\n  value is not a valid float (type=type_error.float)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}
[Errors]
HTTP/1.1 400 Bad Request


Utilities.WebRequestRest.Rest.Validate (Utilities.WebRequestRest.Response response, System.Boolean debug, System.String methodName) (at Library/PackageCache/com.utilities.rest@2.1.6/Runtime/Rest.cs:1071)
OpenAI.Audio.AudioEndpoint.CreateTranscriptionAsync (OpenAI.Audio.AudioTranscriptionRequest request, System.Threading.CancellationToken cancellationToken) (at Library/PackageCache/com.openai.unity@5.0.8/Runtime/Audio/AudioEndpoint.cs:72)
WhisperScript.TranscriptAudio (UnityEngine.AudioClip audioClip) (at Assets/Scripts/WhisperScript.cs:28)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <47fc8c70fa834cbf8141d7c1a7589125>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <91dc399f41a440558c716315c7e834ab>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <91dc399f41a440558c716315c7e834ab>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <91dc399f41a440558c716315c7e834ab>:0)

Here is the function where I make the request.

using UnityEngine;
using OpenAI;
using OpenAI.Audio;

public class WhisperScript : MonoBehaviour
{
    private OpenAIClient api;
    private string result;
    private CanvasScript canvas;
    private string audioPath = "Assets/Audio/audioClip.ogg";
    float? temperatureValue = 0.1f;
  
    private void Awake() {
        api = new OpenAIClient(new OpenAIAuthentication());
        canvas = FindAnyObjectByType<CanvasScript>();
    }
    public async void TranscriptAudio(){
        AudioTranscriptionRequest transcriptionRequest = new AudioTranscriptionRequest(
            audioPath,
            model: "whisper-1",
            responseFormat: AudioResponseFormat.Json,
            temperature: temperatureValue,
            language: "en"
        );

        result = await api.AudioEndpoint.CreateTranscriptionAsync(transcriptionRequest);
        canvas.UpdateTranscriptedText(result);
    }
}

Here is the request function in the library;

public AudioTranscriptionRequest(
            string audioPath,
            string model = null,
            string prompt = null,
            AudioResponseFormat responseFormat = AudioResponseFormat.Json,
            float? temperature = null,
            string language = null)
            : this(File.OpenRead(audioPath), Path.GetFileName(audioPath), model, prompt, responseFormat, temperature, language)
        {
        }

I even tried modifying the library files by moving them to the ‘packages’ folder. I changed all the nullable floats to floats, but I still encountered the same error.

By the way, I reported this issue on GitHub, but I haven’t received any response yet.

I really need this to work so I can continue with my project. Thanks in advance.

I remember temperature being a thing in a DougDoug stream on twitch, as he does heavy use for multiple uses with AI. And I think the temperature became a standard 1.0f as max, so you attempting 0.1f should have no issues in that regard. I remember in that particular vid, not even adding the temperature worked(as whatever the default was would override).

But to be fair, I don’t think it was the same app. Try adding just a float itself(instead of a float variable) within the call, and also try not adding the temperature at all. See if either of those help.

Still contact the maker of the app(if something helps), because he may just have a simple mistake within the hard code somewhere.

To debug any remote API / network call:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

Yes, the temperature parameter can be set between 0 and 1, with the default value being 0. If I don’t set the temperature in the request, transcription works fine, but there is a hallucination problem where random sentences are added at the end of the transcribed text. I read somewhere that lowering the temperature reduces this hallucination problem.

The owner of the library I mentioned said that you can use a regular float, but I encountered the same error when I tried that as well.

I had no idea that I could debug a network call. I’m a newbie when it comes to this stuff.
Thank you for the links; I’ll try it as soon as possible!

All network stuff should always begin with curl / Postman / swagger until you have 100% success.

If you can’t make that work, you certainly won’t make anything else work!

1 Like

Bug was fixed a while back, but I did add a way to enable debug logs for all endpoints using:

var client = new OpenAIClient();
client.EnableDebug = true;