Back to course

Connecting Frontend to OpenAI API

Generative AI for Web & Mobile Apps

Connecting to the Brain

Now, let's make that UI work. We will use the openai library to send messages to the gpt-3.5-turbo or gpt-4 models.

The Code Pattern:

javascript const response = await openai.chat.completions.create({ model: "gpt-3.5-turbo", messages: [{ role: "user", content: userInput }], }); console.log(response.choices[0].message.content);

You'll learn how to handle the asynchronous nature of API calls and show a "Loading..." state to your users.