Basic Sand-Bob usage: Code generation

Basic Sand-Bob usage: Code generation#

For getting started with Sand-Bob, you first should initialize the LLM server you want to use. In this example, we have ollama locally installed and can use it like this:

from sand_bob import generate_code, config_llms

config_llms(model="gemma3:4b",
            base_url="http://localhost:11434/v1",
            api_key="none"
           )

You can then generate (and execute) code like this:

result = generate_code("print out the numbers between 0 and 9", final_touch=False)

The result can be inspected in an interactive GUI (which will not render on github.com, you need to execute this notebook to see it).

result

Execution Output

0
1
2
3
4
5
6
7
8
9
Numbers between 0 and 9 have been printed.
for i in range(10):
    print(i)
print("Numbers between 0 and 9 have been printed.")

Execution Details

  • Execution reason: Initial execution
  • Final result: Numbers between 0 and 9 have been printed.
  • Build Time: 2.09s
  • Run Time: 8.72s
  • Execution Time: 10.88s
  • Total Time: 45.52s
  • Files:
    • /display_output/notebook_executed.ipynb
  • Feedback:
    Overall the code looks good. The code is concise, readable, and effectively achieves the stated goal. Adding a final print statement to confirm the completion is a nice touch, but not strictly necessary.
    
LLM backend
TaskFunctionModel
Generate codepromptgemma3:4b
Fix codepromptgemma3:4b
Determine dependenciespromptgemma3:4b
Generate code feedbackpromptgemma3:4b
Summarize codepromptgemma3:4b
Notebook conversionpromptgemma3:4b

You can also just visualize the output of the generated code.

result.display_output()
0
1
2
3
4
5
6
7
8
9
Numbers between 0 and 9 have been printed.

And you can print out the generated code itself:

print(result.code)
for i in range(10):
    print(i)
print("Numbers between 0 and 9 have been printed.")

Working with files#

You can also do this involving files and image output, e.g. like this:

result = generate_code("Load 'input_data/image.tif' and show it", 
                       input_host_path="input_data", 
                       dependencies=["scikit-image", "matplotlib"], 
                       n_codefix_attempts=1,
                       n_feedback_iterations=0,
                       final_touch=False)
result.display_output()

image.tif
print(result.code)
import skimage.io
import matplotlib.pyplot as plt
import numpy as np

# Load the image
image = skimage.io.imread('input_data/image.tif')

# Display the image
plt.imshow(image)
plt.axis('off')  # Hide axes
plt.show()

# Print the file name
print("image.tif")
result

Execution Output

image.tif
import skimage.io
import matplotlib.pyplot as plt
import numpy as np

# Load the image
image = skimage.io.imread('input_data/image.tif')

# Display the image
plt.imshow(image)
plt.axis('off')  # Hide axes
plt.show()

# Print the file name
print("image.tif")

Execution Details

  • Execution reason: Initial execution
  • Dependencies: scikit-image, matplotlib
  • Final result: image.tif
  • Build Time: 1.76s
  • Run Time: 30.16s
  • Execution Time: 31.99s
  • Total Time: 38.55s
  • Files:
    • /display_output/notebook_executed.ipynb
LLM backend
TaskFunctionModel
Generate codepromptgemma3:4b
Fix codepromptgemma3:4b
Determine dependenciespromptgemma3:4b
Generate code feedbackpromptgemma3:4b
Summarize codepromptgemma3:4b
Notebook conversionpromptgemma3:4b