Basic

Your first API call

A chatbot begins with a single question asked to a model. In this lesson you make your first request to an AI language model, send a message, and receive an answer back, the seed of every chat app.

What you will see

Reply to: How are you?

About 3 min of reading

Try It

A chatbot begins with a single question asked to a model. I make the smallest possible API call, read the reply back from structured data, then send a message through the little loop that every chat app runs.

1

Every chat app starts by sending a message to the model's API and printing whatever comes back. I send one message and print the reply to show the simplest request and response.

message = "Hello!" print(f"Sending: {message}") reply = f"Hi! I am a chatbot." if message == "Hello!" else "..." print(reply)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Sending: Hello! Hi! I am a chatbot.
The model never returns plain text, it returns a data object. This first call just prints what that object hands back.
2

The model answers with structured JSON, and the human-readable text lives under a long path. I open that path and print only the message text, ignoring all the metadata around it.

answer = {"choices": [{"message": {"content": "Hello there!"}}]} print(answer["choices"][0]["message"]["content"])

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Hello there!
The real JSON carries tokens, timing, and more. Only extract the text at choices[0].message.content so that is what shows in the chat bubble.
3

A chat app builds a request from the user's message, sends it to the model, and shows the reply. I take the latest message and print what a reply to it would look like to trace that loop.

def chat(messages): last = messages[-1]["content"] return f"Reply to: {last}" messages = [{"role": "user", "content": "How are you?"}] print(chat(messages))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Reply to: How are you?
The only part that grows in a real chat app is how you manage the conversation, not this basic send-and-print pattern.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Your app sends a prompt to the model's API, the model predicts a reply, and your app extracts the text to display.
1 App builds the request with the user's message
↓
2 HTTP POST sends it to the model API
↓
3 The model predicts a response
↓
4 The API returns a JSON payload
↓
5 Your app parses the JSON
↓
6 The text is shown in the chat
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

message = "Hello!" sets nothing on screen
print(f"Sending: {message}") Sending: Hello!
reply = f"Hi! I am a chatbot." if message == "Hello!" else "..." sets nothing on screen
print(reply) Hi! I am a chatbot.
Why this works
What is a language model

A language model is a program trained on huge amounts of text to predict what text should come next.

When you ask it a question, it is not looking the answer up in a database. It is predicting a sensible response based on patterns it learned. That is why it can explain new ideas, write stories, and answer questions it was never explicitly taught.

Two things shape its behaviour:

  • The **prompt*: the text you send it.
  • The **parameters*: settings like creativity and safety that change how it answers.

Examples

Asking the model a question

Most chat apps call an AI **API*: a service that returns answers. Here is the smallest possible call using Node:

const http = require('http');

const data = JSON.stringify({
  message: "Hello!"
});

const req = http.request(
  {
    host: 'api.openai.com',
    path: '/v1/chat/completions',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + process.env.API_KEY
    }
  },
  (res) => {
    let body = '';
    res.on('data', (c) => body += c);
    res.on('end', () => {
      const answer = JSON.parse(body);
      console.log(answer.reply);
    });
  }
);

req.write(data);
req.end();

This sends your message to the model and prints the reply. Every chat app does this, the only part that grows is how you manage the conversation.

Reading the response

The model answers with **JSON*: structured data you can read apart.

const answer = JSON.parse(body);
console.log(answer.choices[0].message.content);

A chat answer lives under choices[0].message.content. That is the human-readable text you would show in the chat bubble. Everything else in the JSON is metadata, tokens used, timing, and so on.

Why it works: the model never sends plain text back. It sends a data object, and you extract the text from where it lives.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Keeping your secret safe
Keeping your secret safe →