Menu

Working with APIs

Introduction

Integrating AI into your applications typically involves interacting with APIs provided by companies like OpenAI, Anthropic, or Google. You'll learn how to authenticate, send prompts, and parse responses programmatically.

Making API Requests

import os
from openai import OpenAI

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful coding assistant."},
        {"role": "user", "content": "Explain recursion in Python in one sentence."}
    ]
)

print(response.choices[0].message.content)

In this example, we define roles (system, user) to contextually steer the LLM's behavior.

Understanding Tokens

LLMs process text in chunks called tokens. A token can be a word, part of a word, or just a single character. APIs charge based on token usage (both input and output tokens), so optimizing your prompts can save money and improve performance.

Assignment

  1. Follow the OpenAI Quickstart Guide to write a simple script to send a prompt to an LLM API and print the result.
  2. Use OpenAI's Tiktokenizer tool to see how sentences are broken down into tokens.

Support me!

I am a software engineer giving back to the community - my name is Musila Peter. Join me in empowering learners around the globe by supporting SaneGenius!