how to use OpenAI to generate text completions and chat-style responses with GPT-3,chatGPT or GPT-4 through API


OpenAI is a research organization that aims to build safe and beneficial artificial intelligence systems. One of their most popular products is the GPT (Generative Pre-trained Transformer) language model, which can be used to generate human-like text based on a given prompt. OpenAI provides a Python API that allows developers to easily access and use their language models. In this blog post, we’ll explore how to use OpenAI to generate text completions and chat-style responses using GPT-3 and GPT-4.

install openai package

pip install openai

One thing to notice, if you want to use chatGPT API, make sure the openai version is greater than 0.27.0.

Generating Text Completions with GPT-3

To generate text completions using GPT-3, we can use the openai.Completion.create method provided by the OpenAI Python API. Here’s an example:

import openai
openai.api_key = "YOUR_API_KEY"

prompt = "The quick brown fox"

response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
temperature=0.5,
max_tokens=20
)

result = response['choices'][0]['text'].strip('\n').strip()
print(result)

In this example, we’re using text-davinci-003 as the engine to generate text completions based on the prompt “The quick brown fox”. We’re using a temperature of 0.5 and a max_tokens of 20 to control the length and diversity of the generated text. The resulting text completion is printed to the console.

Generating Chat-style Responses with GPT-4

To generate chat-style responses using GPT-4, we can use the openai.ChatCompletion.create method provided by the OpenAI Python API. Here’s an example:

import openai
openai.api_key = "YOUR_API_KEY"

messages=[
{"role": "system", "content": "You are a good assistant."},
{"role": "user", "content": "I'm looking for a new laptop. Can you recommend one?"},
{"role": "assistant", "content": "Sure, what's your budget?"},
{"role": "user", "content": "Not too much."},
]

response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
temperature=0.7,
max_tokens=50
)

result = response['choices'][0]['message']['content'].strip('\n').strip()
print(result)

Generating Chat-style Responses with chatGPT

It’s the same API to call as GPT-4, just switch the model to be “gpt-3.5-turbo”

import openai
openai.api_key = "YOUR_API_KEY"

messages=[
{"role": "system", "content": "You are a good assistant."},
{"role": "user", "content": "I'm looking for a new laptop. Can you recommend one?"},
{"role": "assistant", "content": "Sure, what's your budget?"},
{"role": "user", "content": "Not too much."},
]

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0.7,
max_tokens=50
)

result = response['choices'][0]['message']['content'].strip('\n').strip()
print(result)

Conclusion

OpenAI provides powerful language models that can be used to generate human-like text and chat-style responses. In this blog post, we explored how to use the OpenAI Python API to generate text completions using GPT-3 and chat-style responses using GPT-4. By leveraging these language models, developers can create innovative applications that can interact with users in natural and engaging ways.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC