코드 생성 프롬프트 만들기

gemini-1.0-procode-bison 모델은 자연어 설명을 사용해서 코드 생성을 지원합니다. 이 주제에서는 코드 생성을 위해 지원되는 모델로 작동하는 프롬프트 생성 전략을 보여줍니다.

사용 사례

코드 생성에 대한 몇 가지 공통적인 사용 사례는 다음과 같습니다.

  • 단위 테스트 - 프롬프트를 설계하여 함수의 단위 테스트를 요청합니다.

  • 함수 작성: 문제를 모델에 전달하여 해당 문제를 해결하는 함수를 가져옵니다.

  • 클래스 만들기: 프롬프트를 사용하여 클래스 목적을 설명하고 반환된 클래스를 정의하는 코드를 사용합니다.

지원되는 모델

다음 모델은 코드 생성 태스크를 지원합니다.

  • gemini-1.0-pro
  • code-bison

코드 생성 프롬프트 예시

다음 예시를 사용하여 코드 생성 프롬프트를 설계하는 방법을 알아보세요.

Docker 스크립트 생성

코드 생성 모델을 사용하여 Docker 스크립트를 생성할 수 있습니다. 다음 프롬프트는 특정 라이브러리가 설치된 Linux 머신을 만들기 위한 Docker 스크립트를 생성합니다.


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.
  

함수 생성

코드 생성 모델을 사용하여 함수를 생성할 수 있습니다. 다음 프롬프트는 연도를 입력하고 윤년인지 여부를 확인하는 함수를 생성합니다.


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
```
  

단위 테스트 생성

코드 생성 모델을 사용하여 단위 테스트를 생성할 수 있습니다.

다음 프롬프트는 윤년인지 여부를 확인하는 함수의 단위 테스트를 생성합니다.


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()
```
  

다음 단계