Crea prompt per generare codice

Questo documento mostra le strategie per la creazione di prompt che funzionano con i modelli supportati per generare codice utilizzando una descrizione in linguaggio naturale.

Casi d'uso

Ecco alcuni casi d'uso comuni per la generazione del codice:

  • Test delle unità: progetta un prompt per richiedere un test delle unità per una funzione.

  • Scrivi una funzione: passa un problema al modello per ottenere una funzione che lo risolve.

  • Crea una classe: utilizza un prompt per descrivere lo scopo di una classe e ottieni un codice che definisce la classe restituita.

Modelli supportati

I seguenti modelli supportano le attività di generazione del codice:

  • Gemini 1.5 Flash
  • Gemini 1.5 Pro
  • Gemini 1.0 Pro

Esempi di prompt di generazione del codice

Utilizza i seguenti esempi per imparare a progettare prompt di generazione di codice.

genera uno script Docker

Puoi utilizzare il modello di generazione del codice per generare uno script Docker. Il seguente prompt genera uno script Docker per creare una macchina Linux con librerie specifiche installate:

Generate a Docker script to create a simple linux machine that has python 3.10 installed with following libraries: pandas, tensorflow, numpy
  
```
# Create a new Dockerfile
FROM ubuntu:20.04

# Update the package list
RUN apt-get update

# Install Python 3.10
RUN apt-get install -y python3.10

# Install Python libraries
RUN pip install pandas tensorflow numpy

# Create a new user
RUN useradd -m user

# Set the default user
USER user

# Start the Python shell
CMD ["python"]
```

To build the Docker image, run the following command:

```
docker build -t my-python-image .
```

To run the Docker image, run the following command:

```
docker run -it --rm my-python-image
```

This starts a new container based on the `my-python-image` image. You can then use the Python shell to interact with the container. For example, you can install additional Python libraries or run Python scripts.
  

Generare una funzione

Puoi utilizzare il modello di generazione del codice per generare una funzione. Il seguente prompt genera una funzione che inserisce un anno e determina se si tratta o meno di un anno bisestile:

Write a function that checks if a year is a leap year.
  
```python
def is_leap_year(year):
"""
def is_leap_year(year):
  """
  Check if a year is a leap year.

  Args:
    year: The year to check.

  Returns:
    True if the year is a leap year, False otherwise.
  """

  if year % 4 != 0:
    return False
  elif year % 100 == 0 and year % 400 != 0:
    return False
  else:
    return True
```
  

Genera un test delle unità

Puoi utilizzare il modello di generazione del codice per generare un test delle unità.

Il seguente prompt genera un test delle unità per una funzione che determina se un anno è un anno bisestile o meno.

Write a unit test for this function:
def is_leap_year(year):
  """
  Check if a year is a leap year.

  Args:
    year: The year to check.

  Returns:
    True if the year is a leap year, False otherwise.
  """

  if year % 4 != 0:
    return False
  elif year % 100 == 0 and year % 400 != 0:
    return False
  else:
    return True
  
```python
import unittest

class IsLeapYearTest(unittest.TestCase):

    def test_is_leap_year_true(self):
        self.assertTrue(is_leap_year(2000))
        self.assertTrue(is_leap_year(2004))
        self.assertTrue(is_leap_year(2012))

    def test_is_leap_year_false(self):
        self.assertFalse(is_leap_year(1900))
        self.assertFalse(is_leap_year(1999))
        self.assertFalse(is_leap_year(2100))

    def test_is_leap_year_invalid_input(self):
        with self.assertRaises(TypeError):
            is_leap_year("2000")

if __name__ == '__main__':
    unittest.main()
```
  

Passaggi successivi