B4A Library [B4X] OpenAI - A.I. Text & Image generation

Only tested in B4A so far

This library is designed to facilitate communication with the multimodal OpenAI API, enabling your B4X applications to leverage the capabilities of OpenAI's models for
text generation (GPT-3.5 Turbo and GPT-4),
image generation (DALL-E 3),
text-to-speech functionalities.
Vision API will be added soon!

get your API key here.
Used tokens arent calculated yet.
OpenAI pricing is pay-as-you-go, meaning you only pay for what you use without needing a subscription.
Text generation costs are low. For example, generating text equivalent to the length of "The Hobbit" (around 95,000 words) with GPT-3.5 would cost about $0.25.
Image generation costs are also affordable. Generating a single 1024x1024 image costs around $0.040.

You will see all prices here:

DependsOn: XUI, JSON and OkHttpUtils2

B4X:
Dim oai As OpenAI
oai.Initialize(Me, "OpenAI", "your-api-key")

'In conjunction with ChatGPT, the library will buffer all messages from GPT
'and the user to maintain the context of the chat history. Use .ResetChat to start over.
oai.ChatModel=oai.MODEL_GPT35_TURBO 'or MODEL_GPT4
oai.SystemMessage("Act like a math teacher called Tom, write witty but informative.") 'optional, tell the bot once how to interpret the chat /act
oai.ChatMessage("Hello tom, please explain the determinant method to me!")

' Interact with DALL-E 3
oai.ImageAspectRatio(oai.IMAGE_16_9) 'optional, IMAGE_1_1; IMAGE_16_9, IMAGE_9_16
oai.generateImage("A blue elephant in a Greenhouse")

' Interact withTTS
oai.TTSVoice=oai.TTS_FABLE 'optional, TTS_ALLOY,TTS_ECHO,TTS_FABLE,TTS_ONYX,TTS_NOVA,TTS_SHIMMER
oai.TextToSpeech("Hello, this is a test")

' Implement corresponding event handlers in your activity or class
Sub OpenAI_ChatResponse(response As String)
    Log(response)
End Sub

Sub OpenAI_ImageResponse(image As B4XBitmap)
    ImageView.SetBitmap(image)
End Sub

Sub OpenAI_TTSResponse(folder As String, filename As String)
    MediaPlayer.Load(folder, filename)
    MediaPlayer.Play
End Sub

Sub OpenAI_Error(message As String)
    Log("Error: " & message)
End Sub


Have Fun!
 

Attachments

  • OpenAI.b4xlib
    2.3 KB · Views: 122
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Did anyone get anything?

I had to try with B4J (various reasons) and after having downloaded and corrected the class (it didn't work with the library), I managed to try the chat and the creation of an image. In both cases, though, the response was stuff like:

[Chat]
Error: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.

[Image]
Error: Billing hard limit has been reached


1710703176326.png
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
Did you setup your payment details?
OpenAI API is not free. You will be charged according to used tokens
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
I thought there was a free minimum.
Since that's not the case... goodbye 😄
The costs are really low, generating the amount of words of the Hobbit book (95.000 words) would cost around 0,25$ with GPT3.5.
A image in 1024x1024 around $0.040.

No subscription, only what u're using.
Worth a try
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Greet work @Blueforces.
When he is asked a question he makes mistakes in formulating some questions.
For example, if you ask to create 4 questions about Napoleon, the following error returns:
OpenAI: ChatResponse ready!
Certamente! Ecco quattro domande su Napoleone Bonaparte:
1. "Qual era il ruolo di Napoleone durante la Rivoluzione francese?"
2. "Quali sono state alcune delle principali vittorie militari di Napoleone durante le sue campagne in Europa?"
3. "In che modo Napoleone ha influenzato la storia francese e europea del XIX secolo?"
4. "Qual è stata la fine di Napoleone e quale è il suo lascito storico duraturo?"
Queste domande ti forniranno una panoramica sintetica su alcuni aspetti della vita e delle conquiste di Napoleone Bonaparte. Se hai bisogno di ulteriori informazioni o dettagli, non esitare a chiedere.
OpenAI: Generate TTS...
ResponseError. Reason: , Response: {
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
Error: We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)

and it does this on all questions if you try to have it create a questionnaire.
Any suggestion ?
 

MarcoRome

Expert
Licensed User
Longtime User
I solved it by adding this line (it seems that multiple sentences on multiple lines bother him)

B4X:
Public Sub TextToSpeech(Prompt As String)
    Prompt = Prompt.Replace(CRLF,"") '<--- Add This Line
    Dim j As HttpJob
    j.Initialize("", Me)

but maybe we can do better
 

MarcoRome

Expert
Licensed User
Longtime User
The problem also occurred on " and other characters.
I solved it by adding the following code:

B4X:
Public Sub TextToSpeech(Prompt As String)
    Dim su As StringUtils '<--- This line'
    Prompt =  su.EncodeUrl(Prompt, "UTF8") '<--- This line
    Dim j As HttpJob
    ...

In attached Lib 1.02
 

Attachments

  • OpenAI.b4xlib
    2.3 KB · Views: 18
Top