Article directory
-
-
-
- OpenAI
- Completion
- pet name generator
-
- prompt word engineering prompt enginering
- Build the application
- Results display
-
-
OpenAI
OpenAI
has trained leading language models that are extremely good at understanding and generating text. Our API
provides access to these models, which can be used to handle almost any task involving “language processing”.
Completion
Completion (Completion
) is the core of our API, providing a simple interface that is very flexible and powerful. You enter some text as a prompt (Prompt
), and the API
will return a text that matches any instructions or context you provide as a completion (Completion
>).
-
Enter the prompt word (
Prompt
): Write a slogan for the ice cream shop. -
Return to completion (
Completion
): We deliver a smile with every spoonful!
Pet name generator
Let’s say you want to create a pet name generator. It would be difficult to come up with a name from scratch.
First, you need a prompt word (Prompt
) that clearly states what you want. Let’s try it out and submit the following prompt word (Prompt
) to get a completion (Completion
).
-
Enter the prompt word (
Prompt
): Suggest one name for a horse. -
Return completion (
Completion
): Lightning
Try again to make your instructions more specific.
-
Enter the prompt word (
Prompt
): Suggest one name for a black horse. -
Return completion (
Completion
): Midnight
As you can see, adding a simple adjective to the prompt (Prompt
) changes the completion returned (Completion
). Designing prompts (Prompt
) is essentially “programming” the model.
- Some more examples
Well-designed instructions are important to achieve good results, but sometimes they are not enough. Let’s try a more complex command.
-
Enter the prompt (
Prompt
): Suggest three names for a horse that is a superhero. -
Return completion (
Completion
):
- Super Stallion
- Captain Colt
- Mighty Mustang
prompt enginering
In many situations it is helpful to show and tell the model what you want. Adding examples to your prompts can help convey patterns or nuances. Try submitting this tipwhich includes several examples.
-
Enter the prompt: 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: -
Return completion (
Completion
):
Super Stallion, Mighty Mare, The Magnificent Equine
Super Stallion, Powerful Mare, Magnificent Horse
very good! Adding examples of the output we expect given the input helps the model provide the type of names we are looking for.
- Adjust settings
Prompt design isn’t the only tool you can use. You can also control completions (Completions
) by adjusting settings. One of the most important settings is Temperature (Temperature
).
You may have noticed that if you submit the same prompt multiple times in the example above, the model will always return the same or very similar completion. This is because the temperature (Temperature
) you set is 0
.
Try setting the temperature (Temperature
) to 1
and resubmitting the same prompt word (Prompt
) a few times.
- Enter the prompt word (
Prompt
):
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:
- Return completion (
Completion
):
Super Stallion, Mighty Equine, The Fabulous Thoroughbred
Super Stallion, Powerful Horse, Fabulous Thoroughbred Horse
When the temperature is above 0
, each submission of the same prompt word (Prompt
) will return a different completion (Completion
).
Remember, the model predicts which text is most likely to follow the text that precedes it. Temperature (Temperature
) is a value between 0
and 1
that basically allows you to control how the model behaves when making these predictions. Confidence. Lowering the temperature (Temperature
) means it will take less risk and the finish will be more accurate and certain. Increasing the temperature (Temperature
) will result in a more varied finish.
For a pet name generator, you may want to be able to generate a lot of name ideas. A medium temperature of 0.6
(Temperature
) should have better results.
Build the application
- Install
node.js
Node.js, npm and vue download and installation git clone
project
git clone https://github.com/openai/openai-quickstart-node.git
If you don’t want to use git
, you can also use this zip
file to download the code.
- Add your
API Key
Navigate to the project directory and copy the sample environment variable file.
cd openai-quickstart-node cp .env.example .env
Copy your Secret API Key
and set it to OPENAI_API_KEY
in the newly created .env
file.
When using Javascript
, all API
calls should be made server-side only, because if called on the browser-side, the browser-side code will expose your API Key
.
- Run application
Run the following commands in the project directory to install dependencies and run the app.
npm install npm rundev
- Open http://localhost:3000 in your browser and you should see the pet name generator!
- Error message
error with openai api request: connect etimedout 199.96.58.177:443
Indicates the need to use a new IP
address (you know)
- Error report about
API Key
If it is a private application for API Key
, there is generally no problem.
It may exceed the free quota, etc., just handle it normally.
You must be a member of an organization to use the API. Please contact us through our help center at help.openai.com.
Result display
- horse
- tiger
- goat