반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

Get Started - Quick Start Tutorial

2023. 1. 3. 12:24 | Posted by 솔웅


반응형

OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language.

In this quickstart tutorial, you’ll build a simple sample application. Along the way, you’ll learn key concepts and techniques that are fundamental to using the API for any task, including:

 

OpenAI는 텍스트를 이해하고 생성하는 데 매우 뛰어난 최첨단 언어 모델을 훈련했습니다. 당사의 API는 이러한 모델에 대한 액세스를 제공하며 이는 언어 처리와 관련된 거의 모든 작업을 해결하는 데 사용할 수 있습니다.
이 빠른 시작 자습서에서는 간단한 샘플 애플리케이션을 빌드합니다. 그 과정에서 다음을 포함하여 모든 작업에 API를 사용하는 데 기본이 되는 주요 개념과 기술을 배우게 됩니다.

  • Content generation
  • Summarization
  • Classification, categorization, and sentiment analysis
  • Data extraction
  • Translation
  • Many more!

콘텐츠 생성
요약
분류, 범주화 및 감정 분석
데이터 추출
번역
더 많이!

 

Introduction

The completions endpoint is the core of our API and provides a simple interface that’s extremely flexible and powerful. You input some text as a prompt, and the API will return a text completion that attempts to match whatever instructions or context you gave it.

 

완료 엔드포인트는 API의 핵심이며 매우 유연하고 강력한 간단한 인터페이스를 제공합니다. 일부 텍스트를 프롬프트로 입력하면 API는 사용자가 제공한 지침이나 컨텍스트와 일치하도록 시도하는 텍스트 완성을 반환합니다.

Prompt
Write a tagline for an ice cream shop.
 
Completion
We serve up smiles with every scoop!

You can think of this as a very advanced autocomplete — the model processes your text prompt and tries to predict what’s most likely to come next.

 

프롬프트
아이스크림 가게의 슬로건을 작성하세요.

완성
우리는 모든 특종으로 미소를 제공합니다!


이것을 매우 진보된 자동 완성으로 생각할 수 있습니다. 모델은 텍스트 프롬프트를 처리하고 다음에 올 가능성이 가장 높은 것을 예측하려고 시도합니다.

 

Start with an instruction

Imagine you want to create a pet name generator. Coming up with names from scratch is hard!

First, you’ll need a prompt that makes it clear what you want. Let’s start with an instruction. Submit this prompt to generate your first completion.

‍애완동물 이름 생성기를 만들고 싶다고 상상해 보십시오. 처음부터 이름을 생각해내는 것은 어렵습니다!
먼저 원하는 것을 명확하게 하는 프롬프트가 필요합니다. 지침부터 시작하겠습니다. 이 프롬프트를 제출하여 첫 번째 완료를 생성하십시오.
 
Suggest one name for a horse.

Not bad! Now, try making your instruction more specific.

‍나쁘지 않습니다! 이제 지시사항을 보다 구체적으로 작성해 보십시오.
 
Suggest one name for a black horse.

As you can see, adding a simple adjective to our prompt changes the resulting completion. Designing your prompt is essentially how you “program” the model.

 

보시다시피 프롬프트에 간단한 형용사를 추가하면 결과 완성이 변경됩니다. 프롬프트 디자인은 본질적으로 모델을 "프로그래밍"하는 방법입니다.

Add some examples

Crafting good instructions is important for getting good results, but sometimes they aren’t enough. Let’s try making your instruction even more complex.

‍좋은 지침을 만드는 것은 좋은 결과를 얻는 데 중요하지만 때로는 그것만으로는 충분하지 않습니다. 당신의 지시를 더욱 복잡하게 만들어 봅시다.
 
Suggest three names for a horse that is a superhero.

This completion isn't quite what we want. These names are pretty generic, and it seems like the model didn't pick up on the horse part of our instruction. Let’s see if we can get it to come up with some more relevant suggestions.

이 완성은 우리가 원하는 것이 아닙니다. 이 이름은 매우 일반적이며 모델이 우리 지침의 말 부분을 선택하지 않은 것 같습니다. 좀 더 관련성 높은 제안을 제시할 수 있는지 봅시다.

 

In many cases, it’s helpful to both show and tell the model what you want. Adding examples to your prompt can help communicate patterns or nuances. Try submitting this prompt which includes a couple examples.

 
많은 경우 모델에게 원하는 것을 보여주고 말하는 것이 도움이 됩니다. 프롬프트에 예시를 추가하면 패턴이나 뉘앙스를 전달하는 데 도움이 될 수 있습니다. 몇 가지 예가 포함된 이 메시지를 제출해 보세요.

Suggest three names for an animal that is a superhero.

 

슈퍼히어로인 동물의 이름을 세 개 제안하세요.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: Horse
Names:

 

Nice! Adding examples of the output we’d expect for a given input helped the model provide the types of names we were looking for.

 

멋집니다! 주어진 입력에 대해 예상되는 출력의 예를 추가하면 모델이 우리가 찾고 있는 이름 유형을 제공하는 데 도움이 되었습니다.

Adjust your settings

Prompt design isn’t the only tool you have at your disposal. You can also control completions by adjusting your settings. One of the most important settings is called temperature.

You may have noticed that if you submitted the same prompt multiple times in the examples above, the model would always return identical or very similar completions. This is because your temperature was set to 0.

Try re-submitting the same prompt a few times with temperature set to 1.

 

프롬프트 설계가 마음대로 사용할 수 있는 유일한 도구는 아닙니다. 설정을 조정하여 완료를 제어할 수도 있습니다. 가장 중요한 설정 중 하나는 temperature (온도)라고 불립니다. 
위의 예에서 동일한 프롬프트를 여러 번 제출한 경우 모델이 항상 동일하거나 매우 유사한 완성을 반환한다는 것을 알 수 있습니다. 온도가 0으로 설정되어 있기 때문입니다.
온도를 1로 설정하고 동일한 프롬프트를 몇 번 다시 제출해 보십시오.

 

Suggest three names for an animal that is a superhero.

 

슈퍼히어로인 동물의 이름을 세 개 제안하세요.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: Horse
Names:

 

See what happened? When temperature is above 0, submitting the same prompt results in different completions each time.

Remember that the model predicts which text is most likely to follow the text preceding it. Temperature is a value between 0 and 1 that essentially lets you control how confident the model should be when making these predictions. Lowering temperature means it will take fewer risks, and completions will be more accurate and deterministic. Increasing temperature will result in more diverse completions.

 

무슨 일이 있었났나요? 온도가 0보다 높을 때 동일한 프롬프트를 제출하면 매번 다른 완료 결과가 나타납니다.
모델은 앞의 텍스트를 따를 가능성이 가장 높은 텍스트를 예측한다는 점을 기억하십시오. 온도는 기본적으로 이러한 예측을 할 때 모델의 신뢰도를 제어할 수 있는 0과 1 사이의 값입니다. 온도를 낮추면 위험이 줄어들고 완료가 더 정확하고 결정적입니다. 온도를 높이면 더욱 다양한 완성도를 얻을 수 있습니다.

DEEP DIVE
Understanding tokens and probabilities

For your pet name generator, you probably want to be able to generate a lot of name ideas. A moderate temperature of 0.6 should work well.

 

심층 분석
토큰 및 확률 이해


애완 동물 이름 생성기의 경우 많은 이름 아이디어를 생성할 수 있기를 원할 것입니다. 0.6의 적당한 온도가 잘 작동합니다.

 

Build your application

PYTHON (FLASK)

Now that you’ve found a good prompt and settings, you’re ready to build your pet name generator! We’ve written some code to get you started — follow the instructions below to download the code and run the app.

 

이제 적절한 프롬프트와 설정을 찾았으므로 애완동물 이름 생성기를 만들 준비가 되었습니다! 시작할 수 있도록 몇 가지 코드를 작성했습니다. 아래 지침에 따라 코드를 다운로드하고 앱을 실행하세요.

Setup

If you don’t have Python installed, install it from here. Then download the code by cloning this repository.

 

Python이 설치되어 있지 않은 경우 여기에서 설치하십시오. 그런 다음 이 리포지토리를 복제하여 코드를 다운로드합니다.

git clone https://github.com/openai/openai-quickstart-python.git

If you prefer not to use git, you can alternatively download the code using this zip file.

git을 사용하지 않으려면 이 zip 파일을 사용하여 코드를 다운로드할 수 있습니다.

Add your API key

Navigate into the project directory and make a copy of the example environment variables file.

 

프로젝트 디렉터리로 이동하여 예제 환경 변수 파일의 복사본을 만듭니다.

cd openai-quickstart-python
cp .env.example .env

Copy your secret API key and set it as the OPENAI_API_KEY in your newly created .env file. If you haven't created a secret key yet, you can do so below.

 

비밀 API 키를 복사하고 새로 만든 .env 파일에서 OPENAI_API_KEY로 설정합니다. 아직 비밀 키를 만들지 않았다면 아래에서 만들 수 있습니다.

SECRET KEYCREATEDLAST USED
sk-...hTSN
Sep 24, 2022 Never

Create new secret key‍

새로운 secret key 을 생성한다.

Run the app

Run the following commands in the project directory to install the dependencies and run the app. When running the commands, you may need to type python3/pip3 instead of python/pip depending on your setup.

 

프로젝트 디렉터리에서 다음 명령을 실행하여 종속성을 설치하고 앱을 실행합니다. 명령을 실행할 때 설정에 따라 python/pip 대신 python3/pip3을 입력해야 할 수도 있습니다.

 

1
2
3
4
python -m venv venv
. venv/bin/activate
pip install -r requirements.txt
flask run

Open http://localhost:5000 in your browser and you should see the pet name generator!

 

브라우저에서 http://localhost:5000을 열면 애완동물 이름 생성기가 표시됩니다!

 

Understand the code

Open up app.py in the openai-quickstart-python folder. At the bottom, you’ll see the function that generates the prompt that we were using above. Since users will be entering the type of animal their pet is, it dynamically swaps out the part of the prompt that specifies the animal.setup.

 

openai-quickstart-python 폴더에서 app.py를 엽니다. 하단에는 위에서 사용했던 프롬프트를 생성하는 함수가 표시됩니다. 사용자는 애완 동물의 유형을 입력하므로 animal.setup을 지정하는 프롬프트 부분을 동적으로 교체합니다.

1
2
3
4
python -m venv venv
. venv/bin/activate
pip install -r requirements.txt
flask run

Open http://localhost:5000 in your browser and you should see the pet name generator!

 

브라우저에서 http://localhost:5000을 열면 애완동물 이름 생성기가 표시됩니다!

Understand the code

Open up app.py in the openai-quickstart-python folder. At the bottom, you’ll see the function that generates the prompt that we were using above. Since users will be entering the type of animal their pet is, it dynamically swaps out the part of the prompt that specifies the animal.

 

openai-quickstart-python 폴더에서 app.py를 엽니다. 하단에는 위에서 사용했던 프롬프트를 생성하는 함수가 표시됩니다. 사용자는 애완 동물의 유형을 입력하므로 동물을 지정하는 프롬프트 부분을 동적으로 교체합니다.

 

1
2
3
4
5
6
7
8
9
def generate_prompt(animal):
    return """Suggest three names for an animal that is a superhero.

Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {}
Names:""".format(animal.capitalize())

On line 14 in app.py, you’ll see the code that sends the actual API request. As mentioned above, it uses the completions endpoint with a temperature of 0.6.

 

app.py의 14행에서 실제 API 요청을 보내는 코드를 볼 수 있습니다. 위에서 언급했듯이 온도가 0.6인 완료 끝점을 사용합니다.

1
2
3
4
5
response = openai.Completion.create(
  model="text-davinci-003",
  prompt=generate_prompt(animal),
  temperature=0.6
)

And that’s it! You should now have a full understanding of how your (superhero) pet name generator uses the OpenAI API!

 

이게 다 입니다! 이제 (슈퍼히어로) 애완동물 이름 생성기가 OpenAI API를 사용하는 방법을 완전히 이해하셨을 것입니다!

Closing

These concepts and techniques will go a long way in helping you build your own application. That said, this simple example demonstrates just a sliver of what’s possible! The completions endpoint is flexible enough to solve virtually any language processing task, including content generation, summarization, semantic search, topic tagging, sentiment analysis, and so much more.

One limitation to keep in mind is that, for most models, a single API request can only process up to 2,048 tokens (roughly 1,500 words) between your prompt and completion.

 

이러한 개념과 기술은 자신만의 애플리케이션을 구축하는 데 큰 도움이 될 것입니다. 즉, 이 간단한 예는 가능한 것의 일부에 불과합니다! 완성 엔드포인트는 콘텐츠 생성, 요약, 시맨틱 검색, 주제 태깅, 감정 분석 등 거의 모든 언어 처리 작업을 해결할 수 있을 만큼 유연합니다.
명심해야 할 한 가지 제한 사항은 대부분의 모델에서 단일 API 요청이 프롬프트와 완료 사이에 최대 2,048개의 토큰(약 1,500단어)까지만 처리할 수 있다는 것입니다.

DEEP DIVE
Models and pricing

For more advanced tasks, you might find yourself wishing you could provide more examples or context than you can fit in a single prompt. The fine-tuning API is a great option for more advanced tasks like this. Fine-tuning allows you to provide hundreds or even thousands of examples to customize a model for your specific use case.

 

고급 작업의 경우 단일 프롬프트에 맞출 수 있는 것보다 더 많은 예제나 컨텍스트를 제공할 수 있기를 바랄 수 있습니다. 미세 조정 API는 이와 같은 고급 작업을 위한 훌륭한 옵션입니다. 미세 조정을 통해 수백 또는 수천 개의 예제를 제공하여 특정 사용 사례에 맞게 모델을 사용자 지정할 수 있습니다.

Next steps

To get inspired and learn more about designing prompts for different tasks:

 

영감을 얻고 다양한 작업에 대한 프롬프트 디자인에 대해 자세히 알아보려면 다음 단계를 따르세요.

완료 가이드를 읽어보세요.
예시 프롬프트 라이브러리를 살펴보세요.
놀이터에서 실험을 시작하세요.
빌드를 시작할 때 사용 정책을 염두에 두십시오.

반응형

'Open AI > GET STARTED' 카테고리의 다른 글

Get Started - Data usage policies  (0) 2023.03.06
Get Started - Tutorials  (3) 2023.03.06
Get Started - Usage policies  (0) 2023.01.05
Get Started - Models (Added GPT-4 03/14/2023)  (1) 2023.01.05
Get Started - Libraries  (0) 2023.01.04
Get Started - Introduction  (0) 2023.01.03