As you may know, LEGO Education release Spike app version 3, with support for python, including MicroPython support and new python modules specific to the Spike Prime hub, motors and sensors. I have been trying to create a custom GPT that can write python code for the Spike Prime system and its updated hub, motors and sensors. I have uploaded the API document that provides a detailed description of each new module as a .txt file. I have copied and pasted individual modules, one at a time. The resultant GPT continues to make syntax errors. Any suggestions or is this something that custom GPT’s cannot do.
Hi @LanaiBoy
Can you please share the syntax errors?
Sorry, I tried copying and pasting the script but it was not formatted well, so here is a screen shot of the script, where the prompt: write a python script to use the color sensor in portA of the spike 1 hub to sample the sensor every second and print the color value in terminal/console.
this is the color sensor module I uploaded to the custom GPT:
Color Sensor
The color_sensor
module enables you to write code that reacts to specific colors or the intensity of the reflected light.
To use the Color Sensor module add the following import statement to your project:
import color_sensor
All functions in the module should be called inside the color_sensor
module as a prefix like so:
color_sensor.reflection(port.A)
The Color Sensor can recognize the following colors:
Red
Green
Blue
Magenta
Yellow
Orange
Azure
Black
White
Functions
color
color(port: int) → int
Returns the color value of the detected color. Use the color
module to map the color value to a specific color.
import color_sensor
from hub import port
import color
if color_sensor.color(port.A) is color.RED:
print("Red detected")
Parameters
port: int
A port from the port
submodule in the hub
module
reflection
reflection(port: int) → int
Retrieves the intensity of the reflected light (0-100%).
Parameters
port: int
A port from the port
submodule in the hub
module
rgbi
rgbi(port: int) → tuple[int, int, int, int]
Retrieves the overall color intensity and intensity of red, green and blue.
Returns tuple[red: int, green: int, blue: int, intensity: int]
Parameters
port: int
A port from the port
submodule in the hub
module
Please repaste the text even though it will be badly formatted and then I will see if bookending with ```
will format it correctly.
BTW, my goal in trying to develop this custom GPT, Spike, is to introduce generative AI to middle school students who are not coders but have ideas about what they want the Spike Prime robots to do and can express those ideas in natural language prompts, without have to deal with block coding, scratch coding or python.
from spike import PrimeHub, color_sensor, ColorSensor
from spike.control import wait_for_seconds
# Initialize the hub
hub = PrimeHub()
# Initialize the Color Sensor on port A
sensor = ColorSensor('A')
while True:
# Read the color value from the sensor
color = sensor.get_color()
# Print the color value
print("Detected color:", color)
# Wait for 1 second before repeating
wait_for_seconds(1)
Sorry, I uploaded the wrong script. let me copy and paste the right one.
that is the correct script. I have been trying to get GPT to use ‘from hub’ and not ‘from spike’, which was used in the first release of the Spike app. As you can see, the functions Spike comes up with are not in the API for the color sensor.
Here is the correct script, from the GPT trainer:
import color_sensor
from hub import port
import color
import time
while True:
# Read the color value from the sensor
if color_sensor.color(port.A) == color.RED:
print("Red detected")
# Wait for a short period before checking again
time.sleep(1)
FYI
To format the code correctly just use markdown code blocks
e.g.
```python y = 3 x = 5 print(x+y) ```
Sorry, I am not a programmer. My background is in hardware and semiconductors. Can you explain
The example code you are pasting is for Python. When you paste it here as plain text the Python code is not formatting correctly. To get the Python code to display correctly it must be formatted. The way to get the Python code to format correctly is create it with added HTML tags. Manually creating HTML tags is just a repetitive chore and error prone. So Markdown as a simplified markup language was created. So to quickly format Python code for display in this forum, just add some Markdown, which in this case are Code Blocks which are created using ```
as bookends meaning at the start and end of the Python code. Then to tell the Markdown engine how the code block should be more precisely formatted, python
is added to the first book end.
In short just put ```python
on one line, paste the Python code, on a blank line after the python code add ```
.
If you need more understanding just do a Markdown tutorial, there are many on the Internet.
I will follow your suggestion. In the meantime, it is interesting that the GPT Builder, the left window of the custom GPT workspace, is able to generate a python script free of syntax errors and works as requested in the text prompt. In this instance, I focused on transferring the color sensor API description and GPT Builder successfully generated working scripts. The breakdown is in GPT Builder’s ability to transfer that knowledge to Spike, the custom GPT. That same prompt, entered into Spike after GPT Builder update’s Spike, results in lots of mistakes, syntax errors.