Here is an example of how to use the GPT-4o API in Python to generate text:
python
import openai
openai.api_key = "YOUR_API_KEY"
response = openai.Completion.create(
engine="gpt-4o",
prompt="Once upon a time, there was a curious cat named Whiskers who loved to explore the world around her. One day, Whiskers ventured into the enchanted forest and discovered...",
max_tokens=100,
n=1,
stop=None,
temperature=0.7,
)
print(response.choices[0].text)
Key points:
- Import the openai library and set your API key[1][4]
- Use the `openai.Completion.create()` function to generate text[1][4]
- Specify the model as "gpt-4o" to use the GPT-4o engine[1][3]
- Provide a prompt to continue, like the start of a story[1][4]
- Set `max_tokens` to limit the length of the generated text[1][4]
- Adjust `temperature` to control the creativity/randomness (0.0 to 1.0)[3]
- The generated text will be returned in `response.choices.text`[1][4]
This demonstrates the basic usage of the GPT-4o API to extend a given prompt into a longer piece of text. The model will continue the story in a coherent way based on the starting sentence.
You can further customize the prompt, adjust the temperature, and handle the generated output to integrate GPT-4o into your applications. The OpenAI API provides a simple interface to add powerful language generation capabilities.
Citations:[1] https://www.datacamp.com/tutorial/gpt4o-api-openai-tutorial
[2] https://github.com/openai/openai-cookbook/blob/main/examples/gpt4o/introduction_to_gpt4o.ipynb
[3] https://platform.openai.com/docs/guides/text-generation
[4] https://platform.openai.com/docs/quickstart
[5] https://wandb.ai/onlineinference/gpt-python/reports/GPT-4-in-Python-quickstart-using-the-OpenAI-API--VmlldzozODI1MjY4