Generative AI · Intermediate level

Intermediate level · Module 4: Building Applications with LLM APIs · Lesson 1 of 3

One Interface, Many Backends

Learners write a small generate() interface with a scripted practice model, a local Ollama backend and a hosted backend, so that application code never depends on one provider.

In this lesson you will

  • Implement a provider-independent generate() interface
  • Switch backends through configuration

Key concepts

adapter patternscripted practice modelOllama REST APIhosted provider APIconfiguration

The lesson

Application code calls generate(messages, **options) and receives text plus usage information. Small adapter classes translate this for each backend: a scripted practice model for offline work and tests, a local model through Ollama's REST API, and a hosted provider through its documented API.

Provider documentation (for example OpenAI's text generation guide or Anthropic's documentation) shows each API's details; this course uses them as examples, not requirements.

Switching backends should be a configuration change, which keeps projects affordable and portable.

Step by step

  1. Write the generate() interface and a scripted backend.
  2. Add an Ollama backend (if a local model is available).
  3. Add a hosted backend with the key read from an environment variable.
  4. Run the same script on two backends.

Practice activity

Implement the project's model interface with two working backends.

Expected result: The same application code running on two backends.

Check your understanding

  1. Why use a provider-independent interface?

    Show answer

    To avoid lock-in and switch backends by configuration.

  2. What is the scripted practice model used for?

    Show answer

    Offline, free and repeatable development and testing.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.