SwiftRelay

← All sprint notes

First Lap: Build a Python Chatbot in Your Terminal

First lapsEN

Your first Python chatbot starts with one reply

A chatbot does not need a website, a database, or a complicated framework. For your first run, build a terminal program that asks for a message, sends it to a language model, and prints the answer.

You will write the Python code that handles input and output. The model runs remotely and generates the response. This means you are building an application around an existing model, not training a new one.

The first version below handles one message at a time. That small scope makes it easier to understand each moving part before adding an ongoing conversation.

Prepare Python, a key, and a model identifier

You need Python installed, a terminal, and an API key. An API key is a secret credential that authorizes your program to use a service. Treat it like a password: do not publish it in screenshots, repositories, or shared code.

SwiftRelay is a practical option for this exercise because it works with the OpenAI-compatible Python SDK and bills per token. Tokens are small chunks of text used to measure input and output. Sign up for SwiftRelay to get a small gift credit for your first experiments.

In your account, choose an available model, such as Gemini 2.5 Flash, and copy its exact API model identifier. A display name and an API identifier are not necessarily identical.

Install the Python package by running this command in your terminal:

python -m pip install openai

Next, save your key in an environment variable. This keeps it outside your Python file. Replace the example value with your real key.

  • On macOS or Linux: export SWIFTRELAY_API_KEY="your_api_key"
  • In Windows PowerShell: $env:SWIFTRELAY_API_KEY="your_api_key"

Run the script from that same terminal session so it can read the variable.

Write the six-line chatbot script

Create a file named chatbot.py. Copy the following six lines into it, placing each paragraph on its own line. Replace YOUR_MODEL_ID with the exact identifier you copied.

import os

from openai import OpenAI

client = OpenAI(api_key=os.environ["SWIFTRELAY_API_KEY"], base_url="https://api.fastapirelay.com/v1")

user_message = input("You: ")

response = client.chat.completions.create(model="YOUR_MODEL_ID", messages=[{"role": "user", "content": user_message}])

print("Bot:", response.choices[0].message.content)

The first two lines load the tools your script needs. The third creates a client: an object that sends requests using your key and the specified address. For the OpenAI SDK, SwiftRelay's base address includes /v1.

The fourth line waits for your message. The fifth sends it to the selected model. The final line extracts the answer text from the response and prints it.

Run a question through the terminal

Save the file and run python chatbot.py. If your computer uses python3 instead, use that command consistently for installation and execution.

At the prompt, try: “Explain a Python variable using a kitchen example.” You should see a reply after the request completes. The program then exits. Run it again to ask another question.

This version has no memory. Each run sends only the message you just typed, so a follow-up such as “Give me another example” will not include the earlier exchange.

Check errors before adding conversation memory

  • Missing key: Check that the environment variable exists in the terminal running your script.
  • Authentication failure: Confirm that the key is valid and copied without extra spaces.
  • Unknown model: Check the exact API identifier and your account's access to it.
  • Billing error: Check your available credit and the error details.

Once this works, your next lap is a conversation loop. Keep a list of messages, append each user message and assistant reply, and send that history with each request. Remember that longer histories use more tokens. Start small, avoid sending sensitive information, and expand only after you understand the basic request.

Keep reading

Back for another lap?Log in