| 42번째 줄: |
42번째 줄: |
| | == 사용 == | | == 사용 == |
| | | | |
| − | === 기본 형태 === | + | === 기본 형태(채팅) === |
| | 기본 형태가... 결과물에 따라 천차만별이 된다. https://platform.openai.com/docs/introduction<nowiki/>를 보며 만들어가는 게 좋을듯. | | 기본 형태가... 결과물에 따라 천차만별이 된다. https://platform.openai.com/docs/introduction<nowiki/>를 보며 만들어가는 게 좋을듯. |
| | {| class="wikitable" | | {| class="wikitable" |
| 86번째 줄: |
86번째 줄: |
| | ] | | ] |
| | </syntaxhighlight>위처럼 assistant와 user를 추가해 전달하면 이전 대화를 기억하는 것처럼 작동한다. | | </syntaxhighlight>위처럼 assistant와 user를 추가해 전달하면 이전 대화를 기억하는 것처럼 작동한다. |
| | + | |} |
| | + | |
| | + | === 기본 형태(채팅이 아닌 단답 문장 완성형) === |
| | + | 위와 다른 점만 기입하겠다. |
| | + | {| class="wikitable" |
| | + | !채팅형 |
| | + | !단답형 |
| | + | |- |
| | + | |<syntaxhighlight lang="python"> |
| | + | import openai |
| | + | |
| | + | openai.api_key = "API키 넣을 자리" |
| | + | |
| | + | response = openai.ChatCompletion.create( |
| | + | model="gpt-3.5-turbo" |
| | + | messages=[ |
| | + | {'role':'user', 'content':'작성하고 싶은 프롬프트'} |
| | + | ] |
| | + | ) |
| | + | |
| | + | print(response['choices'][0]['message']['content']) # 응답 중 답변만 추출한다. |
| | + | </syntaxhighlight> |
| | + | | |
| | + | # openai.ChatCompletion.create 대신 openai.Completion.create을 사용한다. |
| | + | # model이 아닌, engine에 모델명을 기입한다. |
| | + | # messages가 아닌 prompt가 텍스트 형태로 input이 된다. |
| | + | <syntaxhighlight lang="python3"> |
| | + | import openai |
| | + | openai.api_key = "API키 넣기." |
| | + | # 조건 |
| | + | model = 'text-davinci-003' # 선택할 수 있게! ex) gpt-3.5-turbo |
| | + | max_tokens = 1700 # 입력에 1000정도 사용해서. 출력은 700 이하가 되게끔. |
| | + | |
| | + | response = openai.Completion.create( |
| | + | engine=model, |
| | + | prompt='api 사용을 시도해보고 있어. 아무 말이나 해봐~', |
| | + | max_tokens=max_tokens |
| | + | ) |
| | + | print(response) |
| | + | print('답변') |
| | + | print(response['choices'][0]['text']) # 응답 중 답변만 추출한다. |
| | + | response = response['choices'][0]['text'] |
| | + | </syntaxhighlight> |
| | |} | | |} |
| | [[분류:ChatGPT]] | | [[분류:ChatGPT]] |