Build a small chatbot using a hosted model endpoint and an environment variable for authentication.

Download the runnable notebook

This lesson was converted from the matching notebook in this repository. Outputs are cleared; read the lesson before running code, and never add API keys or private data.

Goal, prerequisites, and setup

Build a minimal multi-turn chatbot and keep its authentication outside the source file. Know Python functions, lists, dictionaries, and environment variables. Create a virtual environment, install the packages used by the notebook, set GITHUB_TOKEN, and verify that the token exists without printing its value.

Run one short conversation first. Then test a follow-up question, a missing-token error, and an empty input. Save the transcript with the token removed and note the model ID, date, and any usage limits.

import json
import os
import urllib.request

github_token = os.getenv("GITHUB_TOKEN")
if not github_token:
    raise ValueError("Set GITHUB_TOKEN before running this notebook.")

endpoint = "https://models.inference.ai.azure.com/chat/completions"
model_id = "anthropic/claude-3.5-sonnet"

Converse with Claude via GitHub

payload = {
    "model": model_id,
    "messages": [
        {
            "role": "user",
            "content": "Write a poem about the beauty of nature."
        }
    ],
}

prefix = "Bearer "
auth_header = prefix + github_token

request = urllib.request.Request(
    endpoint,
    data=json.dumps(payload).encode("utf-8"),
    headers={
        "Authorization": auth_header,
        "Content-Type": "application/json"
    },
    method="POST"
)

with urllib.request.urlopen(request) as response:
    result = json.loads(response.read().decode("utf-8"))

print(result["choices"][0]["message"]["content"])

References and evidence