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: 252
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
 

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: 106
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
Dim su As StringUtils '<--- This line'
Prompt = su.EncodeUrl(Prompt, "UTF8")

โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘โ†‘
|||||||||||||||||| lo remplace por esto |||||||||||||||||||||
โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“โ†“

Prompt = Prompt.Replace(""&CRLF&"",".")
 

asales

Expert
Licensed User
Longtime User
Hi @Blueforcer.
I'm testing with your code, but I get an error in this line when I try to compile:
B4X:
oai.ImageAspectRatio(oai.IMAGE_16_9)
It's not possible to access private member: imageaspectratio
 

JGParamo

Active Member
Licensed User
Hi Blueforcer, I did tried using DALL-E 3 with my api key but could not show the result in the imageview and "Error: Billing hard limit has been reached." Any sample B4A code to do it? Thanks.

B4X:
Dim oai As OpenAI
Dim imagetext As String
imagetext = "A blue elephant in a Greenhouse"
oai.Initialize(Me, "OpenAI", "sk-c===================k")
' Interact with DALL-E 3
oai.generateImage(imagetext)


Sub OpenAI_ImageResponse(image As B4XBitmap)
ImageView.SetBitmap(image)
'clvMessages.sv.SetBitmap(ImageView)
clvMessages.AsView.SetBitmap(image)
End Sub
 
Last edited:

JGParamo

Active Member
Licensed User

Tim Chapman

Active Member
Licensed User
Longtime User
Does this work with B4J?
When I try to run this I get the following error message:

Compiling generated Java code. Error
Cannot find: C:\Program Files\Anywhere Software\B4J\libraries\okhttputils2.jar

I tried to make the other parts of the code here work but could not. So I commented those lines out. Can anyone give me the details to make that code work as well?

B4J Code:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    Dim PagesManager As B4XPagesManager
    PagesManager.Initialize(MainForm)
End Sub


Dim oai As OpenAI
oai.Initialize(Me, "OpenAI", "My API Key here.")

'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)
'    Dim MediaPlayer    As MediaPlayer
'    MediaPlayer.Load(folder, filename)
'    MediaPlayer.Play
'End Sub

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


'Template version: B4J-1.0
#Region Delegates
Sub MainForm_FocusChanged (HasFocus As Boolean)
    B4XPages.Delegate.MainForm_FocusChanged(HasFocus)
End Sub

Sub MainForm_Resize (Width As Double, Height As Double)
    B4XPages.Delegate.MainForm_Resize(Width, Height)
End Sub

Sub MainForm_Closed
    B4XPages.Delegate.MainForm_Closed
End Sub

Sub MainForm_CloseRequest (EventData As Event)
    B4XPages.Delegate.MainForm_CloseRequest(EventData)
End Sub

Public Sub MainForm_IconifiedChanged (Iconified As Boolean)
    B4XPages.Delegate.MainForm_IconifiedChanged(Iconified)
End Sub
#End Region
 
Last edited:

JGParamo

Active Member
Licensed User
Does this work with B4J?
When I try to run this I get the following error message:

Compiling generated Java code. Error
Cannot find: C:\Program Files\Anywhere Software\B4J\libraries\okhttputils2.jar

I did mine with B4J and it worked.

Regarding okhttputils2.jar not being found, within the B4J internal library folder as a work-around I did copy jOkHttpUtils2.jar and rename it as okhttputils2.jar. The cause of the error could probably be a bug. Erel may help us on this.
 
Last edited:

JGParamo

Active Member
Licensed User
I tried to make the other parts of the code here work but could not. So I commented those lines out. Can anyone give me the details to make that code work as well?

B4A Code:
' 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")

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

In B4A, I have generated an image using the code snippet above. Commented out method ImageAspectRatio as there could be bug in it (not working).
 
Last edited:

Tim Chapman

Active Member
Licensed User
Longtime User
In B4J, ImageView.SetBitmap(Image) gives an error in B4J. It says SetBitmap is an unknown member.
 

Tim Chapman

Active Member
Licensed User
Longtime User
I did mine with B4J and it worked.

Regarding okhttputils2.jar not being found, within the B4J internal library folder as a work-around I did copy jOkHttpUtils2.jar and rename it as okhttputils2.jar. The cause of the error could probably be a bug. Erel may help us on this.
Please zip and upload your entire code. I am new to B4J and am doing something wrong. What libraries did you use as well?
 

Tim Chapman

Active Member
Licensed User
Longtime User
I did mine with B4J and it worked.

Regarding okhttputils2.jar not being found, within the B4J internal library folder as a work-around I did copy jOkHttpUtils2.jar and rename it as okhttputils2.jar. The cause of the error could probably be a bug. Erel may help us on this.
I have renamed jOKHttpUtils2.jar to OkHttpUtils2.jar. It is in the B4J\Libraries folder. The libraries manager won't show this library but the code complies without error only if I select the jOkHttpUtils2 library in the libraries manager. What am I missing here?
 
Top