GPT를 이용하여 QnA 챗봇 만들기(Python)

BK·2023년 1월 18일
0

목적 : Python에서 GPT 엔진을 이용하여 QnA 챗봇 만들기

1. OpenAI 사이트로 이동

2. 회원가입

  • 간단하게 Google 계정으로 회원가입

3. API-Key 발급

4. PyCharm 실행 후 새로운 프로젝트 생성

5. Terminal을 열어 pip install

  • openai 인스톨
    pip install openai

6. 코드 작성

import openai

openai.api_key = <여기에 아까 발급받은 API-Key 붙여넣기>

prompt = "I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, " \
         "I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, " \
         "I will respond with \"Unknown\".\n\n" \
         "Q: What is human life expectancy in the United States?\n" \
         "A: Human life expectancy in the United States is 78 years.\n\n" \
         "Q: Who was president of the United States in 1955?\n" \
         "A: Dwight D. Eisenhower was president of the United States in 1955.\n\n" \
         "Q: Which party did he belong to?\n" \
         "A: He belonged to the Republican Party.\n\n" \
         "Q: What is the square root of banana?\n" \
         "A: Unknown\n\n" \
         "Q: How does a telescope work?\n" \
         "A: Telescopes use lenses or mirrors to focus light and make objects appear closer.\n\n" \
         "Q: Where were the 1992 Olympics held?\n" \
         "A: The 1992 Olympics were held in Barcelona, Spain.\n\n" \
         "Q: How many squigs are in a bonk?\n" \
         "A: Unknown\n\n"

while True:
    question = input("Q:")
    if question == '종료' or question == 'exit':
        break

    response = (openai.Completion()).create(
        engine="text-davinci-003",
        prompt=prompt + question,
        temperature=0,
        max_tokens=100,
        top_p=1,
        frequency_penalty=0.0,
        presence_penalty=0.0,
        best_of=1,
    )

    print(response.choices[0].text.strip())

7. 코드 실행

  • 시작 화면
  • 질문사항 입력 후 종료 화면

참조 : GPT Sample로 제공하는 소스 이용.
https://beta.openai.com/examples/default-qa

profile
k-힙합을 사랑하는 개발자

0개의 댓글