B4J Question chatGPT

ivanomonti

Expert
Licensed User
Longtime User
Hello everyone, I'm not very knowledgeable about socket or HTML issues, but I'm developing an app that queries ChatGPT. However, when I open and close the connection, I lose the context of my request on ChatGPT, which doesn't happen when using the chat.openai.com page. Could you please give me a solution?

Thank you.
 

epiCode

Active Member
Licensed User
When making your first query to the API, you should include a unique identifier for the conversation in the session_id parameter. This identifier can be any string that you choose, such as a UUID or a user ID.

Then, for subsequent queries, you should use the same session_id value as before. This will allow the API to link the new query to the previous conversation and use it as context for generating the response.
 
Upvote 0

magicmars

Member
Licensed User
As mentionned in this video, you can use the parameter "assistant" to remind about the discussion :

In the official API there are 3 important roles :

- "system" (use to define GPT answering context),
- "user" (User query)
- "assistant" (remind conversation)


You have to combine all of 3 parameters in a api query :

This is very well explained here (it for python, but it's the same for CURL) :


Hope this help.
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User
I'm having difficulty figuring out where to insert these parameters using HTTP. Thank you.

B4X:
        Dim m As Map = CreateMap("n":1,"stop":"None","model": "text-davinci-003", "prompt": Text,"max_tokens":350,"temperature":0.2)
        Dim js As JSONGenerator
        js.Initialize(m)
        Dim req As HttpJob
        req.Initialize("",Me)
        req.PostString("https://api.openai.com/v1/completions",js.ToString)
        req.GetRequest.SetHeader("Authorization", GPT3_AUTH)
        req.GetRequest.SetHeader("OpenAI-Organization", GPT3_ORG)
        req.GetRequest.SetContentType("application/json")
        Wait For (req) JobDone(req As HttpJob)
 
Last edited:
Upvote 0

epiCode

Active Member
Licensed User
Try this:
B4X:
        Dim m As Map = CreateMap("n":1,"stop":"None","model": "text-davinci-003", "session_id": "5d050000-4ce3-2184-8c55-eb7a59ee0000", "prompt": Text,"max_tokens":350,"temperature":0.2)
 
Upvote 0

ivanomonti

Expert
Licensed User
Longtime User

code:
        'Dim m As Map = CreateMap("n":1,"stop":"None","model": "text-davinci-003", "prompt": Text.Trim,"max_tokens":350,"temperature":0.2)
        Dim m As Map = CreateMap("n":1,"stop":"None","model": "text-davinci-003", "session_id": "5d050000-4ce3-2184-8c55-eb7a59ee0000", "prompt": Text,"max_tokens":350,"temperature":0.2)
        Dim js As JSONGenerator
        js.Initialize(m)
        Dim req As HttpJob
        req.Initialize("",Me)
        req.PostString("https://api.openai.com/v1/completions",js.ToString)
        req.GetRequest.SetHeader("Authorization", GPT3_AUTH)
        req.GetRequest.SetHeader("OpenAI-Organization", GPT3_ORG)
        req.GetRequest.SetContentType("application/json")
        Wait For (req) JobDone(req As HttpJob)

Error:
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
(Http client initialized with accept all option.)
ResponseError. Reason: , Response: {
  "error": {
    "message": "Unrecognized request argument supplied: session_id",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…