The completions API generates text from a supplied prompt. It suits single-turn work such as drafting content or answering a question when one response is needed. For a conversation that carries context across turns, use the chat completions API instead. section 3.2 section 3.4.4
Generate business-name ideas
A pet salon could submit a prompt asking for three names that feel professional, friendly, and personal. In the Python client, call client.completions.create with a completion-capable model, the prompt, temperature=0.7, and max_tokens=100; read the result from response.choices[0].text. Check that the chosen model supports completions before deploying the call. section 3.2 section 3.1.2
Control the response and its operating cost with a few request options:
- Set temperature to steer variation, max_tokens to limit generation, and stop to end output at specified sequences. section 3.2
- Use n only when multiple alternatives are useful; additional completions consume tokens. Inspect finish_reason to distinguish a normal stop from a length limit. section 3.2 section 3.3.1
- For Azure OpenAI deployments, account for content-safety filtering of prompts and generated text. section 3.5
Set stream=True when users should see output before the full response is ready. Streaming uses server-sent events, so the client must handle incoming chunks. A chunk’s text contains newly generated output; finish_reason is normally null until the final chunk reports stop or length. section 3.3.1
Listing 3.12 Streaming completion
import os
import sys
from openai import AzureOpenAI
client = AzureOpenAI(
azure_endpoint=os.getenv("AOAI_ENDPOINT"),
api_version="2024-05-01-preview",
api_key=os.getenv("AOAI_KEY"))
prompt_startphrase = "Suggest three names and a tagline
which is at least 3 sentences for a new pet salon business.
The generated name ideas should evoke positive emotions and the
followingkey features: Professional, friendly, Personalized Service." #1
for response in client.completions.create( #2
model="gpt-35-turbo",
prompt=prompt_startphrase,
temperature=0.8,
max_tokens=500,
stream=True, #3
stop=None):
for choice in response.choices: #4
sys.stdout.write(str(choice.text)+"\n")
sys.stdout.flush()
The streaming Python example shows stream=True and a loop that writes each arriving choice to the console. section 3.3.1
