Video recording

Abstract

Streamlining ChatGPT code generation workflows for different programming languages.
Paclet page: https://resources.wolframcloud.com/PacletRepository/resources/AntonAntonov/OpenAIMode/
In[]:=
Mathematica code for plotting sin of x over minus one and five
In[]:=
​
​
Plot[Sin[x], {x, -1, 5}]
Out[]=
-1
1
2
3
4
5
-1.0
-0.5
0.5
1.0

Setup

Related OpenAIMode demo
Load paclets
Needs["AntonAntonov`OpenAIMode`"]
Convert notebook style
In[]:=
OpenAIMode[]
Default options
In[]:=
SetOptions[OpenAIInputExecuteToText,OpenAIModel->"text-davinci-003",OpenAITokenLimit->360,OpenAITemperature->0.4];
In[]:=
OpenAIModels[]//Sort

How does it work?

Consider the following flowchart:
Out[]=

Experiments (WL)

In[]:=
SetOptions[OpenAIInputExecuteToText,Epilog->Identity];
In[]:=
Mathematica code for transforming a list of numbers into their squares
Out[]=
​list = {1, 2, 3, 4, 5};​squares = list^2;​Print[squares]​(* Output: {1, 4, 9, 16, 25} *)
In[]:=
SetOptions[OpenAIInputExecuteToText,Epilog->CellPrintWL];
In[]:=
Mathematica code for transforming a list of numbers into their squares
In[]:=
​
​
list = {1, 2, 3, 4, 5};
​
squares = Map[#^2 &, list]
​
(* Output: {1, 4, 9, 16, 25} *)
Out[]=
{1,4,9,16,25}
In[]:=
SetOptions[OpenAIInputExecuteToText,Epilog->CellPrintAndRunWL];
In[]:=
Plot the first 100 prime numbers with Mathematica
In[]:=
​
​
ListPlot[Prime[Range[100]]]
Out[]=
20
40
60
80
100
100
200
300
400
500
In[]:=
Numerical integration code in Mathematica for the function, one of the square root of X over the domain, two and 10
In[]:=
​
​
NIntegrate[Sqrt[x], {x, 2, 10}]
Out[]=
19.1962

Experiments (Python)

In[]:=
plFlowchart
In[]:=
SetOptions[OpenAIInputExecuteToText,Epilog->CellPrintPython];
In[]:=
Generate Python code for iteration over a list
〉
In[]:=
​
​
#Example list
my_list = [1, 2, 3, 4, 5]
​
#Iteration over a list
for item in my_list:
print(item)
1
2
3
4
5
In[]:=
Generate Python code for replacing the elements of a list with their squares
〉
In[]:=
​
​
# Initial list
list1 = [2, 4, 5, 6]

# Using list comprehension
list2 = [x**2 for x in list1]

# Printing list2
print(list2)
​
# Output: [4, 16, 25, 36]
[4, 16, 25, 36]

Experiments (Julia)

In[]:=
SetOptions[OpenAIInputExecuteToText,Epilog->CellPrintJulia];
In[]:=
Generate Julia code for iteration over a list
〉
In[]:=
​
​
# Iterate over a list of numbers
list = [1, 2, 3, 4, 5]
​
for num in list
println(num)
end
〉
In[]:=
list
Out[]=
{1,2,3,4,5}
In[]:=
Julia code that changes a list of numbers into a list of strings
〉
​
​
list_of_numbers = [1, 2, 3, 4]
​
list_of_strings = [str(num) for num in list_of_numbers]
​
print(list_of_strings) # ['1', '2', '3', '4']

Experiments (R)

In[]:=
SetOptions[OpenAIInputExecuteToText,Epilog->CellPrintR];
In[]:=
Generate R code for iteration over a list
〉
{
​
​
# Set up the list
list <- c(1, 2, 3, 4, 5)
​
# Iterate over the list
for (i in list) {
print(i)
}
}
In[]:=
R code that changes a list of numbers into a list of strings
〉
{
​
​
list_of_numbers = [1,2,3,4,5]
​
list_of_strings = [str(num) for num in list_of_numbers]
​
print(list_of_strings) # ['1', '2', '3', '4', '5']
}

More examples of code narration

Acknowledgments

Kuba Podkalicki provided insightful and actionable help for the implementation of notebook stylesheets and interactive features. (Of RakuMode and DSLMode, which were followed while implementing OpenAIMode.)

CITE THIS NOTEBOOK

Code generation demo using paclet OpenAIMode​
by Anton Antonov​
Wolfram Community, STAFF PICKS, April 10 2023
​https://community.wolfram.com/groups/-/m/t/2893906