Groq Fortran coding agent

The Groq API (Groq is distinct from grok) gives you access to multiple LLMs, run on the fast GroqCloud. You need an API key., but there is a free tier that allows for considerable usage. I created a Python agent that given a coding prompt, automates the process of sending gfortran error messages back to the LLM until the code compiles. For a configuration file

model: qwen-2.5-coder-32b
max_attempts: 10
max_time: 1000
prompt_file: prompt_cauchy.txt
source_file: cauchy.f90
run_executable: yes
print_code: no
print_compiler_error_messages: no
compiler: gfortran
compiler_options: -O0 -Wall -Werror=unused-parameter -Werror=unused-variable -Werror=unused-function -Wno-maybe-uninitialized -Wno-surprising -fbounds-check -static -g

with the file prompt_cauchy.txt containing

Do a Fortran simulation to find the optimal trimmed mean estimator of
the location of the Cauchy distribution, trying trimming proportions
of 0%, 10%, 20%, 30%, 40%, and 45%. Declare real variables as
real(kind=dp) with dp a module constant, and put procedures in a
module. Have the simulation use 100 samples of 1000 observations each.
Only output Fortran code. Do not give commentary.

sample output was

Attempt 1 failed (error details suppressed, generation time: 3.164 seconds, LOC=67)
Attempt 2 failed (error details suppressed, generation time: 14.672 seconds, LOC=71)
Code compiled successfully after 3 attempts (generation time: 18.061 seconds, LOC=75)!
Running executable: .\cauchy.exe

Output:
  Trim proportion:   0.0000000000000000      Mean trimmed mean:  -2.7425188462552290
 Trim proportion:  0.10000000000000001      Mean trimmed mean:   1.6141409422076695E-002
 Trim proportion:  0.20000000000000001      Mean trimmed mean:   1.4961654913832745E-002
 Trim proportion:  0.29999999999999999      Mean trimmed mean:   1.2341060294325419E-002
 Trim proportion:  0.40000000000000002      Mean trimmed mean:   1.1169627560190555E-002
 Trim proportion:  0.45000000000000001      Mean trimmed mean:   1.0791625569573506E-002

Total generation time: 35.897 seconds across 3 attempts

Compilation command: gfortran -O0 -Wall -Werror=unused-parameter -Werror=unused-variable -Werror=unused-function -Wno-maybe-uninitialized -Wno-surprising -fbounds-check -static -g -o cauchy cauchy.f90

The available models are listed here. The project, which has a single Python source file, is at

Running the same script many times with the same model, the number of attempts needed for a program that compiles varies a lot. Sometimes the executable gives wrong results or has a run-time error. The script could modified so that specified object or library files are compiled along with the generated source file to create an executable.

2 Likes

I uploaded to GitHub analogous Python scripts to generate C++ and Python code. For the problem of estimating the mode of Cauchy variates using the trimmed mean, the C++ agent is often able to do it on the first attempt, because C++ has built-in functions to generate Cauchy variates and to sort arrays. The Python agent was told to use NumPy and pandas – it chose to use SciPy also.