Skip to main content

llm-api

A FastAPI-based server plugin for the llm CLI that exposes LLM models through API interfaces compatible with popular LLM clients. This allows you to use local or remote LLM models with any client that expects standard LLM API formats.

Installation

As an LLM Plugin

Install this plugin to llm:

# Install from PyPI (once published)
llm install llm-api

# Or install from GitHub
llm install https://github.com/danielcorin/llm-api.git

# Or install from local development directory
cd /path/to/llm-api
llm install -e .

Verify installation:

# Check the plugin is installed
llm plugins

# The 'api' command should be available
llm api --help

Development Installation

For development, use uv:

# Clone the repository
git clone https://github.com/danielcorin/llm-api.git
cd llm-api

# Create a virtual environment and install dependencies
uv venv
source .venv/bin/activate
uv sync --dev

# Install as an editable LLM plugin
llm install -e .

Usage

Start the API server:

llm api --port 8000

The server provides OpenAI Chat Completions API endpoints:

  • GET /v1/models - List available models
  • POST /v1/chat/completions - Create chat completions with:
    • Streaming support
    • Tool/function calling (for models with supports_tools=True)
    • Structured output via response_format (for models with supports_schema=True)
    • Conversation history with tool results

Features

Basic Usage

from openai import OpenAI

# Point the client to your local llm-server
client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed"  # API key is not required for local server
)

# Use any model available in your llm CLI
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, how are you?"}
    ]
)

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

Streaming is also supported

stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Tool/Function Calling

Models that support tools (indicated by supports_tools=True) can use OpenAI-compatible function calling:

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed"  # API key is not required for local server
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What's the weather in San Francisco?"}],
    tools=[{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name"}
                },
                "required": ["location"]
            }
        }
    }]
)

Structured Output with Schema

Models that support schema (indicated by supports_schema=True) can generate structured JSON output:

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="not-needed"
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Generate a person's profile"}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                    "email": {"type": "string"}
                },
                "required": ["name", "age", "email"]
            }
        }
    }
)

# The response will contain valid JSON matching the schema
print(response.choices[0].message.content)

Testing

Run the test script to verify the OpenAI-compliant API:

python -m pytest tests/test_openai_api.py

Development

Prerequisites

  • Python 3.9+
  • llm CLI tool installed
  • One or more LLM models configured in llm

Code Quality

Format code:

ruff format .

Lint code:

ruff check --fix .

Running Tests

Run all tests:

pytest

Configuration

The server integrates with the llm CLI tool's configuration. Make sure you follow the setup instructions.

  1. Installed and configured llm with your preferred models
  2. Set up any necessary API keys for cloud-based models
  3. Verified models are available with llm models

Supported API Specifications

Currently Implemented

  • OpenAI Chat Completions API (/v1/chat/completions)
    • Compatible with OpenAI Python/JavaScript SDKs
    • Works tools expecting OpenAI format
    • Full support for streaming, tool calling, and structured output

Help Wanted

License

MIT

Metadata

Release files for llm-api-server 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-api-server 0.1.0
File Size Uploaded
llm_api_server-0.1.0.tar.gz 14.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-api-server 0.1.0
File Interpreter ABI Platform
llm_api_server-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.8 kB

Release files / llm_api_server-0.1.0.tar.gz

Download URL llm_api_server-0.1.0.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ba258ca5f75ee17386b4667ce5e9db7b3b08b4305cbf053315c165bfe352827a
BLAKE2b-256 checksum
How to use checksums
5f36ea994ae6989d75ea667289109bac1cc2a411bcd998910f1ff09ee799b1d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / llm_api_server-0.1.0-py3-none-any.whl

Download URL llm_api_server-0.1.0-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
433ce5c9a390c56a73f839d7409464f3f5462c7245f47a7a8b223d59c583831c
BLAKE2b-256 checksum
How to use checksums
e92a390ea750aba255db90f20593ea2b5c40acd5ba741a754d6ce942435880a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page