Android Question Server Side Event

Soheyl

Member
Licensed User
Longtime User
Hello everyone,

I am able to connect to the ChatGPT API without any issues when the `Stream` argument is set to `False` and I use the HttpJob. However, when I tried connecting by enabling the `Stream` argument, I encountered problems with the OkHttpRequest.

I found a sample MPEG Stream by Erel and attempted to modify it to suit my needs. The attached project contains the modified MPEG. When I try to connect using the URL below:
B4X:
https://www.b4x.com/print.php

I am able to establish a connection successfully. However, when I change the URL to the ChatGPT URL:
B4X:
https://api.openai.com/v1/edits

I receive an HTTP Error 404.
(Of course, it's not because of the endpoint.)

Note:

There is no need to have an API Key if I only receive an HTTP 403 error, as it indicates that the request is fine.

Thank you for your advice.
 

Attachments

  • SSE-B4J.zip
    3.9 KB · Views: 144

Soheyl

Member
Licensed User
Longtime User
Thanks, teddybear.

However, when we call this URL using CURL on the command line, we receive a more informative error message, such as "API key is not valid," which is understandable.

Additionally, when we use the HttpJob object, everything works perfectly fine. The issue only arises when we use OkHttpRequest.

Here's an example cURL command. You can copy and paste this piece of code into the command line (cmd) and execute it, it should work:

(Source)
B4X:
curl https://api.openai.com/v1/models -H "Authorization: Bearer OPENAI_API_KEY_OPTIONAL"

Regarding the attached project mentioned in the first post, I believe the issue resides in this specific portion of the code. Below is another simple example along with the URL that we should send to either HttpJob or OkHttpRequest:

B4X:
    Dim     url        As String = "https://api.openai.com/v1/models"
    Private hc        As OkHttpClient     'It should be in Process_Global
    
    Dim req         As OkHttpRequest
        req.InitializePatch2(url, "TEST".ToString.GetBytes("UTF8"))
        req.SetHeader("Authorization", "Bearer " & GetEnvironmentVariable("OPENAI_API_KEY", ""))

        req.SetHeader("OpenAI-Organization", "")
        req.SetContentType("application/json")
        req.SetContentEncoding("UTF8")
        req.Timeout = 90000

    hc.Execute(req, 0)

I'm not sure where my foolish mistake lies, but I have been struggling with this particular issue for more than two weeks now. It has been quite frustrating and challenging to resolve.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
curl https://api.openai.com/v1/models -H "Authorization: Bearer OPENAI_API_KEY_OPTIONAL"

B4X:
    Dim     url        As String = "https://api.openai.com/v1/models"
    Private hc        As OkHttpClient     'It should be in Process_Global
 
    Dim req         As OkHttpRequest
        req.InitializePatch2(url, "TEST".ToString.GetBytes("UTF8"))
        req.SetHeader("Authorization", "Bearer " & GetEnvironmentVariable("OPENAI_API_KEY", ""))

        req.SetHeader("OpenAI-Organization", "")
        req.SetContentType("application/json")
        req.SetContentEncoding("UTF8")
        req.Timeout = 90000

    hc.Execute(req, 0)
In curl you are using Get Method for request

In OkHttpRequest, why are you using Patch Method for request?
 
Upvote 0

Soheyl

Member
Licensed User
Longtime User
It is not matter, I have tested both scenarios with OkHttpRequest. In the last post, you can see the URL variable is the same as the one used in the cURL command.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
It is not matter, I have tested both scenarios with OkHttpRequest. In the last post, you can see the URL variable is the same as the one used in the cURL command.
It is not about URL, I mean why you are using Patch for http request instead of Post method..
Try
B4X:
    Dim req         As OkHttpRequest
        req.InitializePost2(url, "TEST".ToString.GetBytes("UTF8"))
 
Upvote 0

Soheyl

Member
Licensed User
Longtime User
Because I'm foolish. ?

I used POST instead of GET for two weeks. :confused:
Thank you for notice.
?
 
Upvote 0
Top