Sub topic 1
Running a Model Locally Behind an HTTP Server
Prerequisites
Assume you know basic Python, HTTP requests, and JSON. You also need a local model server, such as Ollama, with at least one model downloaded. Start the server before running client code.
A local inference server loads a language model on your computer and exposes it through HTTP. Your application sends a request to a local address, such as http://localhost:11434, instead of sending data to a hosted provider.
What OpenAI-Compatible Means
An OpenAI-compatible server follows familiar endpoint paths, request fields, and response shapes from the OpenAI API. For example, it may provide /v1/models and /v1/chat/completions.
Compatibility describes the interface, not identical behavior. A local server may support only some endpoints or parameters. Model names, authentication, streaming, embeddings, and tool calling can differ.
Pointing the Client at Your Server
The Python client uses base_url to choose the server. Local servers often ignore authentication, but the client still requires an API key value.
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="local",
)
print(client.models.list())The /v1 suffix matters because many compatible servers place their endpoints below that path. Check your server's documentation if the request returns 404 Not Found.
Checking Which Models the Server Offers
List models before sending a completion. This avoids guessing the exact model identifier.
for model in client.models.list():
print(model.id)You can also inspect the endpoint directly:
curl http://localhost:11434/v1/modelsUse one of the returned IDs in later requests. A downloaded model's display name may differ from the identifier accepted by the API.
Exercise
Why should you list models before constructing a chat request?
Show answer
The server may require an exact model ID, and listing models reveals which IDs are available.