[OpenAI] API를 이용하여 텍스트 요약기 만들기 1

반디·2023년 8월 29일
0

개발기

목록 보기
13/16

패키지 설치

sudo apt install python3-pip
pip install openai

Chat completions API

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 {system,user,assistant}\in \{system, user, assistant \}

  • system: assistant의 행동을 설정하는 역할로, 답변의 성격을 조절하거나 특정 지시사항을 전달
    (위 예시에서는 "helpful" assistant로 assistant의 성격을 지정)
  • user: assistant에게 input을 입력하는 대상
  • assistant: output을 주는 대상

이 외에도, request를 할 때, max_tokens이나 temperature라는 변수를 추가로 지정해줄 수 있다.

  • max_tokens: 답변 생성 시, token의 최대 갯수 (답변의 최대 길이, default = 16)
  • temperature (default = 1): 0에서 2사이의 값으로 답변의 랜덤성과 연관. 높은 값을 지정할수록 output이 더 랜덤한 형태.

코드

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.

추가 작업

  • 한글로 input, output 처리 (response encoding 주의!)
  • web 형태로 구현하기
  • 세부 목적에 맞게 fine-tuning 시도

참고문헌

profile
꾸준히!

0개의 댓글