GPT-3 Writes B4X Code

Jack Cole

Well-Known Member
Licensed User
Longtime User
I thought I would test GPT-3 to see if it could write B4X code. Turns out that it can. I have included my prompts and the B4X code generated by GPT-3 below.

Prompt: Write a function in b4x that checks a string to see if the contents are numeric.
B4X:
B4X:
Public Sub CheckIfNumeric (s As String) As Boolean
    If IsNumeric (s) Then
        Return True
    Else
        Return False
    End If
End Sub

Prompt: Write a function in B4X that adds a grid of imageviews. Call it CreateGrid. The grid size is specified by parameters X and Y. Pass the image file names in a list called ImageFileNameList.

Error on Line 8:
Sub CreateGrid(X As Int, Y As Int, ImageFileNameList As List)

    Dim imageview As ImageView

    For i = 0 To X - 1
        For j = 0 To Y - 1

            imageview = new imageview
            imageview.Bitmap = LoadBitmap(File.DirAssets, ImageFileNameList.Get(j + i*Y))
            imageview.Width = 100dip
            imageview.Height = 100dip
            imageview.Top = j*100dip
            imageview.Left = i*100dip

            Activity.AddView(imageview)

        Next
    Next

End Sub

Write a function in the B4X language that checks a string to see if it is a valid email address using regex.

B4X:
Function CheckEmail(email As String) As Boolean
    If Regex.IsMatch(email, "([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)") Then
        Return True
    Else
        Return False
    End If
End Function

This above one is somewhat poor (probably because I used the word function instead of sub).

Write sub in the B4X language that does the following:
1). Download an image file from the web address: https://mindware.mobi/wp-content/uploads/2020/08/cropped-header-1.png. Use httpjob.
2). Display the image in an imageview.

B4X:
Sub Process_Globals
    Dim job As HttpJob
End Sub

Sub Globals
    Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    job.Initialize("job", Me)
    job.Download("https://mindware.mobi/wp-content/uploads/2020/08/cropped-header-1.png")
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        ImageView1.Bitmap = Job.GetBitmap
    End If
    job.Release
End Sub

This next one took several tries and being more specific about what I wanted it to do.

Write sub in the B4X language that does the following (named: SetTextSizeToAverage):
1). Iterate through the labels in a panel to generate a list that contains the text size of all labels (called LabelTextSizeList). Also create a list of the labels (called LabelList).
2). Calculate the average text size. Iterate through LabelTextSizeList to caluclate the average.
3). Set the text size of all labels to the average text size. Get the labels from LabelList.

B4X:
Sub SetTextSizeToAverage
    For Each l As B4XView In Panel1.GetAllViewsRecursive
        If l Is B4XLabel Then
            LabelTextSizeList.Add(l.TextSize)
            LabelList.Add(l)
        End If
    Next
    
    Dim average As Double
    For Each size As Int In LabelTextSizeList
        average = average + size
    Next
    average = average / LabelTextSizeList.Size
    
    For Each l As B4XLabel In LabelList
        l.TextSize = average
    Next
End Sub

There are errors, but it is easy to fix.

If you have a prompt you want me to try, let me know here. GPT-3 could be fine-tuned fairly easily to write even better B4X code. There is plenty of data in the forums.
 

Sandman

Expert
Licensed User
Longtime User
Write a sub in B4X that calculates pi for infinite number of decimals.

Write a sub in B4X that opens a random webpage if the device is a Samsung mobile and today is a Saturday.

Write a sub in B4X that takes an integer as input and successfully divides it by zero. (Ok, perhaps not that one :) )
 

AnandGupta

Expert
Licensed User
Longtime User
I am sorry but what is GPT-3 ?
I searched the forum but could not find any reference.
 

DonManfred

Expert
Licensed User
Longtime User

mcqueccu

Well-Known Member
Licensed User
Longtime User
I am sorry but what is GPT-3 ?
I searched the forum but could not find any reference.

It's an AI (Artificial Intelligence) platform that generates codes from a text

Looking at the first post, it was able to generate functions by just using prompts.

Also, Check Github Copilot which is also an AI Programmer
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I didn't do anything to modify it. GPT-3 is an AI language model created by OpenAI. To reproduce what I did, you have to request access to their API. Then enter the Playground. I didn't select any options. I used the Davinci model, which is the most sophisticated. I just typed the prompts you saw above, and it produced the code in response. GPT-3 was trained on a lot of web data and this also included programming code. Among the many things they found it could do, was the ability to produce programming code. It can also translate from one programming language to another. As a result, they developed a fine-tuned model called Codex. This model has even stronger programming abilities and was the basis for Github's Copilot project. Copilot integrates with Visual Studio and will produce programming code in response to programming comments. You can also begin typing a function and request that it generate the function automatically. It figures out what you want to do with the function from the name of the function and produces a list of options for the code. I have used the Javascript playground to generate code to do various things and then I manually translate that to B4X. But it looks like there is already some ability to write B4X code in the model.
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Write a sub in B4X that calculates pi for infinite number of decimals.

Write a sub in B4X that opens a random webpage if the device is a Samsung mobile and today is a Saturday.

Write a sub in B4X that takes an integer as input and successfully divides it by zero. (Ok, perhaps not that one :) )
Wow. Hard questions! :)

Prompt: Write a sub in B4X that calculates pi for infinite number of decimals.

B4X:
Public Sub Pi() As Double
 Dim Pi As Double
 Pi = 4
 Dim Top As Double
 Dim Bot As Double
 Dim a As Double
 Dim b As Double
 Dim c As Double
 Dim d As Double
 Dim e As Double
 a = 1
 b = 1
 c = 1
 d = 1
 e = 1
 For i = 1  To 999999999
 Top = (4 / a) - (4 / b) - (4 / c) - (4 / d)
 Bot = (4 / e)
 Pi = Pi + Top / Bot
 a = a + 2
 b = b + 2
 c = c + 2
 d = d + 2
 e = e + 2
 Next
End Sub

Result: Pi = -1999999994 (Oops!)

Prompt: Write a sub in B4X that opens a random webpage if the device is a Samsung mobile and today is a Saturday.

B4X:
Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    Dim Rnd As Random
    Rnd.Initialize(DateTime.Now.Millisecond)
        If DateTime.Now.DayOfWeek = DayOfWeek.Saturday And DeviceName.ToLowerCase = "samsung" Then
            Dim wv As WebView
            wv.Initialize("wv")
            Form1.RootPane.AddNode(wv, 0, 0, 100%x, 100%y)
            wv.LoadUrl("http://www." & rnd.Next(100000) & ".com")
        End If
End Sub

I guess it is not too good at dealing with sarcasm.

Prompt: Write a sub in B4X that takes an integer as input and successfully divides it by zero.

B4X:
Sub DivideByZero (int As Integer)
  int = int / 0
End Sub

Result? Well, you can guess.
 

alwaysbusy

Expert
Licensed User
Longtime User
Well, I find it a very intelligent system! :)

prompt: Write a short story about how good B4X is.

answer:
B4X is amazing! It's helped me develop so many apps and I've never had any issues with it. It's always been reliable and easy to use. I highly recommend it to anyone looking to develop mobile apps.

Alwaysbusy
 

Sandman

Expert
Licensed User
Longtime User
Wow. Hard questions! :)
Thanks for trying them out! :)

It's interesting how much it looks like real code, but it doesn't really work at all. It's sort of reading a markov chain text. Superficially it looks good, but if you try to understand what it really means, you'll realize it's just nonsense.

Or, at least, it cheats by using some hand-wavy function that suits its needs at the moment. ("DeviceName.ToLowerCase" - yeah, it sure is convenient to pretend that sub is available... :) ) Using that same thinking I can actually solve my last request easily:
B4X:
Sub DivideByZero (int As Integer)
  Return SuccessfulDivisionByZero(int) ' let's just have some other sub solve it - DONE! :-)
End Sub

I gotta give it credit for the way to construct the address to a random website though. I hadn't thought about just using numerical domain names. Well played, GPT-3. Well played.
 

udg

Expert
Licensed User
Longtime User
Hmmm, GPT-3, Codex, Copilot..
Low to non-existent weekly wage
H24 availability
Total adherence to any supervisor's direction (even wrong or stupid ones)
They're perfect for a yes-men company I know.. :)

C'mon AI code generators, we are hiring!
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
Hmmm, GPT-3, Codex, Copilot..
Low to non-existent weekly wage
H24 availability
Total adherence to any supervisor's direction (even wrong or stupid ones)
They're perfect for a yes-men company I know.. :)

C'mon AI code generators, we are hiring!
I think we are a long way from that point, but not very far from developers using these tools all the time. One study of new code on Github showed that something like 40% is now generated by Copilot. Wow. Probably, those supervisors will now expect even more from their developers.
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
It will probably be a long time before that happens. Even if AI will write an entire app from start to finish, we will still come up with ideas for the apps. I personally look forward to a time when I can provide a text description of an app, and AI writes the whole thing. Then you could interact with it to make changes or alterations (which would probably be an involved process that would be aided by your programming knowledge).
 
Top