■ OpenAI 클래스의 completions 변수를 사용해 프로그램 소스 코드를 생성하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import os from openai import OpenAI os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>" openAI = OpenAI() # openAI.completions : openai.resources.completions.Completions 클래스 # completion : openai.types.completion.Completion 클래스 completion = openAI.completions.create( model = "gpt-3.5-turbo-instruct", prompt = "# \"Hello, World!\" 표시\ndef printHelloWorld():\n ...\n\n상기 코드를 완성해주세요.", temperature = 1, max_tokens = 256, top_p = 1, frequency_penalty = 0, presence_penalty = 0 ) print(completion) """ Completion( id = 'cmpl-9W8FGDWTfoKfPyaYHJzqrRAx1sojx', choices = [ CompletionChoice ( finish_reason = 'stop', index = 0, logprobs = None, text = '\n\ndef printHelloWorld():\n print("Hello, World!")' ) ], created = 1717445218, model = 'gpt-3.5-turbo-instruct', object = 'text_completion', system_fingerprint = None, usage = CompletionUsage ( completion_tokens = 13, prompt_tokens = 27, total_tokens = 40 ) ) """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
annotated-types==0.7.0 anyio==4.4.0 certifi==2024.6.2 distro==1.9.0 exceptiongroup==1.2.1 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 openai==1.30.5 pydantic==2.7.3 pydantic_core==2.18.4 sniffio==1.3.1 tqdm==4.66.4 typing_extensions==4.12.1 |