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: 84
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:
Top