Android Question Dealing with CHAT-GPT (GPT-3)

GMan

Well-Known Member
Licensed User
Longtime User
I am using the samples from this thread:
https://www.b4x.com/android/forum/t...into-android-applications.145654/#post-923913

but having problem to initiate the procedure.

Like in the sample i got an api-key and integrated it.

So far so good.
Then an error occurs "text-davinci-30" was't allowed any longer.

I changed it to this:
Dim m As Map m.Initialize CreateMap("n": 1, "stop": "None", "model": "gpt-3.5-turbo", "prompt": query_string, "max_tokens": 350, "temperature": 0.5):

But now an error occured in the log:
"message": "you must provide a model parameter",:

But i cant figure out what the correct model parameter could be ?

Any suggestions ?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Meanwhile i registred and payed a membership and it will work "so far".

Now i'm getting a error that the Server has Problems to manage my request.
B4X:
    "message": "The server had an error while processing your request. Sorry about that!",
 
Upvote 0

blong

Active Member
Licensed User
Longtime User
Sample of ChatGPT:
Sub TranslateText_to_eng(thaiText As String)
    ' Create the JSON payload
    Dim messages As List
    messages.Initialize
   
    ' Add the system message
    Dim systemMessage As Map
    systemMessage.Initialize
    systemMessage.Put("role", "system")
    systemMessage.Put("content", "You are a professional translator. Translate text from Thai to English. Only return the English translation, nothing else.")
    messages.Add(systemMessage)
   
    Dim userMessage As Map
    userMessage.Initialize
    userMessage.Put("role", "user")
    userMessage.Put("content", thaiText )
    messages.Add(userMessage)

    Dim json As Map
    json.Initialize
    json.Put("model", "gpt-4o-mini")
    json.Put("messages", messages)
    json.Put("max_tokens", 300)
    json.Put("temperature", 0.0)

    Dim jsonString As String = CreateJSON(json)

    ' Initialize HttpJob
    Dim http As HttpJob
    http.Initialize("TranslateText", Me)
    http.PostString(ChatGPT_API_URL, jsonString)
    http.GetRequest.SetContentType("application/json")
    http.GetRequest.SetHeader("Authorization", "Bearer " & ChatGPT_API_Key)
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Try
            Dim result As String = Job.GetString
                     
            ' Log the entire response for debugging
            Log("API Response: " & result)
           
            ProcessResponse(result)
        Catch
            ToastMessageShow("Error processing the response.", False)
        End Try
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, False)
    End If
    Job.Release
End Sub

Sub ProcessResponse(responseString As String)
    Try
        Dim parser As JSONParser
        parser.Initialize(responseString)

        ' Parse the root object
        Dim root As Map = parser.NextObject

        ' Get the choices list
        Dim choices As List = root.Get("choices")

        ' Get the first choice
        Dim firstChoice As Map = choices.Get(0)

        ' Get the message map from the first choice
        Dim message As Map = firstChoice.Get("message")

        ' Extract the content field from the message
        Dim translation As String = message.Get("content")

        ' Display the translation in the second text box
        If(translate_dirn="en>th") Then
            outmsg.Text = translation.Trim          
        End If
        If(translate_dirn="th>en") Then
            txtmsg.Text = translation.Trim
        End If

        ' Optionally log the translation for debugging
        Log("Translation: " & translation)
    Catch
        ToastMessageShow("Error processing the response.", False)
        Log("Response Parsing Error: " & LastException.Message)
    End Try
End Sub
Sub CreateJSON(map As Map) As String
    Dim json As JSONGenerator
    json.Initialize(map)
    Return json.ToString
End Sub

Above a code snippet that may help that I use to "talk" to ChatGPT for language translation. :)

I would also note that perhaps the MODEL you use depends on your subscription
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Did you look here?
 
Upvote 0
Top