# OpenAI-Compatible API with Ollama Backend A FastAPI-based service that provides an OpenAI-compatible API endpoint, powered by PydanticAI and Ollama for LLM inference. ## Architecture ``` ┌─────────────┐ ┌──────────────────┐ ┌─────────────┐ │ Client │─────▶│ FastAPI Server │─────▶│ Ollama │ │ │◀─────│ (Stream Coord.) │◀─────│ Container │ └─────────────┘ └──────────────────┘ └─────────────┘ │ ▼ ┌──────────┐ │Pydantic │ │ AI │ └──────────┘ ``` ### Components - **FastAPI**: High-performance web framework providing the API layer - **Stream Coordinator**: Manages streaming responses in OpenAI-compatible format - **PydanticAI**: Agent framework handling LLM integration and structured outputs - **Ollama**: External LLM backend (networked, managed separately) - **SSE-Starlette**: Server-Sent Events for streaming responses ## Features - OpenAI-compatible API endpoints - Streaming responses with Server-Sent Events - PydanticAI integration for robust LLM interactions - Networked Ollama support - Type-safe request/response handling with Pydantic - Async/await throughout for optimal performance ## Requirements - Python 3.12+ (Python 3.12.11 recommended for security) - Network access to an existing Ollama instance (managed externally) ## Installation ### 1. Clone the repository ```bash git clone cd tatlock ``` ### 2. Create a virtual environment ```bash python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate ``` ### 3. Install dependencies ```bash pip install -r requirements.txt ``` ### 4. Configure environment variables Create a `.env` file in the project root: ```env # Ollama Configuration (point to your existing Ollama instance) OLLAMA_HOST=http://your-ollama-host:11434 OLLAMA_MODEL=llama2 # API Configuration API_HOST=0.0.0.0 API_PORT=8000 API_RELOAD=true # Logging LOG_LEVEL=info ``` **Note**: Update `OLLAMA_HOST` to point to your existing Ollama instance. Ensure the Ollama service is accessible from your network and has the required models installed. ## Usage ### Start the development server ```bash uvicorn main:app --reload ``` The API will be available at `http://localhost:8000` ### API Endpoints #### Chat Completions (OpenAI-compatible) ```bash curl http://localhost:8000/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ "model": "llama2", "messages": [ {"role": "user", "content": "Hello, how are you?"} ], "stream": true }' ``` #### List Models ```bash curl http://localhost:8000/v1/models ``` ### Interactive API Documentation - Swagger UI: `http://localhost:8000/docs` - ReDoc: `http://localhost:8000/redoc` ## Security ### Version Locking Strategy This project uses minor version locking (`>=X.Y,