sudo apt install python3-pip
pip install openai
import openai
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
위처럼 모델을 지정해주고, 챗봇에 입력할 메세지들을 입력해준다. 어떤 input을 넣어서 어떤 output을 얻을 것인지를 지정해주는 파트가 messages 이다.
messages는 message object들의 array 형태이다. 각 message object는 role과 content로 구성되어 있으며, role은 "system", "user", "assistant" 중 하나이다.
즉, message object = role + content, where role
이 외에도, request를 할 때, max_tokens이나 temperature라는 변수를 추가로 지정해줄 수 있다.
gpt-4를 이용하여 정해진 direction에 따라 user의 input을 요약해주는 코드
import openai
import config
openai.api_key = config.SECRET_KEY
INSTRUCTIONS = ["1. Overall summary of discussion", "2. Action items (what needs to be done and who is doing it)", "If applicable, a list of topics that need to be discussed more fully in the next meeting."]
system_instruction = "\n".join(["You will be provided with meeting notes, and your task is to summarize the meeting as follows", *INSTRUCTIONS])
msg = "Bear gets apple and tiger gets garlic. They will meet up on the hill at 10 AM tomorrow. They will discuss about new recipe."
response = openai.ChatCompletion.create(
model="gpt-4", #모델 지정
messages = [{"role":"system", "content": f"{system_instruction}"},
{"role": "user", "content": f"{msg}"}],
temperature=0,
max_tokens=200
)
print(response)
print(response.choices[0]["message"]["content"])
{
"id": "chatcmpl-",
"object": "chat.completion",
"created": 169,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1. Overall Summary of Discussion:\nThe meeting was primarily about assigning tasks to Bear and Tiger. Bear is tasked with getting an apple and Tiger is to get garlic. They have planned to meet up on the hill at 10 AM the following day to discuss a new recipe.\n\n2. Action Items:\n- Bear needs to get an apple.\n- Tiger needs to get garlic.\n- Both Bear and Tiger need to meet up on the hill at 10 AM tomorrow to discuss the new recipe.\n\n3. Topics for Next Meeting:\n- Discussion on the new recipe."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 112,
"total_tokens": 212
}
}
1. Overall Summary of Discussion:
The meeting was primarily about assigning tasks to Bear and Tiger. Bear is tasked with getting an apple and Tiger is to get garlic. They have planned to meet up on the hill at 10 AM the following day to discuss a new recipe.
2. Action Items:
- Bear needs to get an apple.
- Tiger needs to get garlic.
- Both Bear and Tiger need to meet up on the hill at 10 AM tomorrow to discuss the new recipe.
3. Topics for Next Meeting:
- Discussion on the new recipe.
참고문헌