OpenAI API Exercises
Fill in the blanks to test your knowledge.
1
Complete the OpenAI client initialization
import OpenAI from "openai";
const client = new ();
2
Call the chat completions endpoint
const completion = await client.chat..create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }]
});
3
Access the generated message content
const text = completion.choices[0]..content;
4
Enable JSON mode response format
const res = await client.chat.completions.create({
model: "gpt-4o",
response_format: { type: " " },
messages: [{ role: "user", content: "Return JSON with name and age" }]
});
5
Enable response streaming
const stream = await client.chat.completions.create({
model: "gpt-4o",
messages,
: true
});
6
Generate an embedding vector for text
const result = await client..create({
model: "text-embedding-3-small",
input: "Hello world"
});
7
Access the embedding vector from the result
const vector = result.data[0].;
8
Name the DALL-E 3 model ID for image generation
const image = await client.images.generate({
model: "",
prompt: "A sunset over mountains"
});