Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatCompletion Response is sometimes empty #59

Open
Morgan-6Freedom opened this issue May 26, 2023 · 1 comment
Open

ChatCompletion Response is sometimes empty #59

Morgan-6Freedom opened this issue May 26, 2023 · 1 comment

Comments

@Morgan-6Freedom
Copy link

Hello.
I use ChatCompletion with Stream=true

I have an issue. Sometimes the responses of CreateChatCompletionAsync is empty.
image

image

I have created a system that wait 3sec and try again but often the response is always empty.

I tried with Insmonia/Postman with streaming mode and the responses is never empty.

What can I do ?

@SebastianBlandon
Copy link

SebastianBlandon commented Oct 27, 2023

I also had that error, I managed to modify the source code to solve it, I changed the stop logic of the cycle that received the streams, I attach the modified code fragment of the OpenAIApi.cs that is inside Runtime so that you can add it and test it. I have already generated the pr to be added to the source code.

Script :

/// <summary>
///     Dispatches an HTTP request to the specified path with the specified method and optional payload.
/// </summary>
/// <param name="path">The path to send the request to.</param>
/// <param name="method">The HTTP method to use for the request.</param>
/// <param name="onResponse">A callback function to be called when a response is updated.</param>
/// <param name="onComplete">A callback function to be called when the request is complete.</param>
/// <param name="token">A cancellation token to cancel the request.</param>
/// <param name="payload">An optional byte array of json payload to include in the request.</param>

private async void DispatchRequest<T>(string path, string method, Action<List<T>> onResponse, Action onComplete, CancellationTokenSource token, byte[] payload = null) where T: IResponse
{
    using (var request = UnityWebRequest.Put(path, payload))
    {
        request.method = method;
        request.SetHeaders(Configuration, ContentType.ApplicationJson);
        request.SendWebRequest();
        bool isDone = false;
        do
        {
            List<T> dataList = new List<T>();
            string[] lines = request.downloadHandler.text.Split('\n').Where(line => line != "").ToArray();
            foreach (string line in lines)
            {
                var value = line.Replace("data: ", "");
                if (value.Contains("stop")) 
                {
                    isDone = true;
                    break;
                }
                var data = JsonConvert.DeserializeObject<T>(value, jsonSerializerSettings);
                if (data?.Error != null)
                {
                    ApiError error = data.Error;
                    Debug.LogError($"Error Message: {error.Message}\nError Type: {error.Type}\n");
                }
                else
                {
                    dataList.Add(data);
                }
            }
            onResponse?.Invoke(dataList);
            await Task.Yield();
        }
        while (!isDone);
        onComplete?.Invoke();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants