-
Notifications
You must be signed in to change notification settings - Fork 807
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
OpenAI chat completion is not working as expected since it's chunked data (IDFGH-14549) (AEGHB-968) #466
Comments
The second call to get the actual content_length calls esp_http_client_get_chunk_length which returns the chunk_length which should be set by this function but this function is never being called as I don't see this log message anywhere.
|
Okay, so more reading testing. I don't think the code here for this is correct, there shouldn't be a content-length when there is chunked data so I'm not seeing how this would ever work. If I hack it like this if (esp_http_client_is_chunked_response(client))
{
esp_http_client_get_chunk_length(client, &content_length);
}
ESP_LOGD(TAG, "content_length=%d", content_length);
// OPENAI_ERROR_CHECK_GOTO(content_length > 0, "HTTP client fetch headers failed!", end);
content_length = 200000; // Guess on a size?
result = (char *)malloc(content_length + 1);
int read = esp_http_client_read_response(client, result, content_length);
// if (read != content_length)
// {
// ESP_LOGE(TAG, "HTTP_ERROR: read=%d, length=%d", read, content_length);
// free(result);
// result = NULL;
// }
// else
// {
result[read] = 0;
ESP_LOGD(TAG, "result: %s, size: %d", result, strlen(result));
// } Everything works as expected because it can read the chunked data correctly if it gets past this initialization. So I guess the question is is this code incorrect or am I not understanding something? Ideally, esp_http_client_read_response would dynamically adjust the buffer to accommodate the chunked data since we don't know the size from the start but it doesn't appear to do that either. |
Answers checklist.
General issue report
This is what I'm seeing in the logs:
When I add additional debug logs in openai.c I see that the payload is correct and the response from opanai is also correct. I've narrowed down to this block but I'm not super familiar with how http and chunks work.
From the logs above I can tell esp_http_client_fetch_headers returns 0 here (which I think it should) because the server returns a Transfer-Encoding:chunked header. Because it's chunked the condition is met and we call esp_http_client_get_chunk_length to get the actual content_length but this also returns 0 as you can see in the second log for content_length.
Should this return an actual value from the header or is this calculated somehow?
Since this returns 0 the OPENAI_ERROR_CHECK_GOTO triggers and we don't actually process the response.
Entire example that fails
The text was updated successfully, but these errors were encountered: