GPT-3 Writes B4X Code

Sandman

Expert
Licensed User
Longtime User
What if we are replaced by AI code writers in the future
That will probably work just as well as self-driving cars do.

Imagine having the software in the self-driving cars written by AI. I'll keep my bike, thank you very much. And I'll avoid roads with cars.

It will probably be a long time before that happens.
Agreed. Look at the bright side: We'll likely be dead by then.
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
So, here's another way to get B4X code from Codex (the model trained on generating programming code from Github).

What I want is code that does the following: Generate a B4X sub that gets a subset of items from a list.

Can I be tricky to get Codex that is designed to generate Python code to access its knowledge about B4X and generate the sub?

Prompt:
1. Create a string variable called b4x_code.
2. Assign a value to the string that contains B4X code.
3. The B4X code is a sub to get a subset of items from a list.

GPT-3 Generating B4X Code within a Python String:
b4x_code = """
Sub GetSubset(list As List, start As Int, end As Int) As List
    Dim result As List
    result.Initialize
    For i = start To end
        result.Add(list.Get(i))
    Next
    Return result
End Sub
"""

"""
1. Create a string variable called b4a_code.
2. Assign a value to the string that contains B4A code.
3. The B4A code is a sub to get a subset of items from a list.
"""

b4a_code = """
Sub GetSubset(list As List, start As Int, end As Int) As List
    Dim result As List
    result.Initialize
    For i = start To end
        result.Add(list.Get(i))
    Next
    Return result
End Sub
"""

"""
1. Create a string variable called b4j_code.
2. Assign a value to the string that contains B4J code.
3. The B4J code is a sub to get a subset of items from a list.
"""

b4j_code = """
Sub GetSubset(list As List, start As Int, end As Int) As List
    Dim result As List
    result

Not bad! Other than using "end" as a parameter name. It is interesting that it tried giving B4A and B4J variations.
 

aeric

Expert
Licensed User
Longtime User
I watched some videos about GPT-3 on YouTube but I don't know it can writes B4X code.
 

aeric

Expert
Licensed User
Longtime User
Write an AI library with B4X
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I'm learning more about how to use this more effectively to generate B4X code. Here is my most recent one. It performs a regression analysis. The code is actually completely correct! I provided it with two examples of B4X code and then asked for the following.

B4X:
'code generated by CODEX
'"""
'1. Write a function in the B4X programming language that performs a regression analysis.
'2. It should take the parameters of YList And XList.
'"""

'performs a regression analysis
Sub RegressionAnalysis(YList As List, XList As List)
    Dim x_bar As Double = 0
    Dim y_bar As Double = 0
    Dim x_y_bar As Double = 0
    Dim x_square_bar As Double = 0
    Dim n As Int = YList.Size
    For i = 0 To n - 1
        x_bar = x_bar + XList.Get(i)
        y_bar = y_bar + YList.Get(i)
        x_y_bar = x_y_bar + XList.Get(i) * YList.Get(i)
        x_square_bar = x_square_bar + XList.Get(i) * XList.Get(i)
    Next
    x_bar = x_bar / n
    y_bar = y_bar / n
    x_y_bar = x_y_bar / n
    x_square_bar = x_square_bar / n
    Dim beta_1 As Double = (x_y_bar - x_bar * y_bar) / (x_square_bar - x_bar * x_bar)
    Dim beta_0 As Double = y_bar - beta_1 * x_bar
    Log("beta_0: " & beta_0)
    Log("beta_1: " & beta_1)
End Sub
 

Sandman

Expert
Licensed User
Longtime User
1. Write a function in the B4X programming language that performs a regression analysis.
2. It should take the parameters of YList And XList.

Add a step 3:

3. Comment each line with an explanation what it does
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…