Skip to content

yctimlin/OpenAI-Compatible-API-Mimic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– OpenAI-Compatible API Mimic

Python FastAPI OpenAI Compatible License: MIT Docker GitHub stars

Drop-in replacement for the OpenAI API that works with any backend

A powerful, modular proxy service that mimics the OpenAI API interface, allowing you to transform requests to any OpenAI-compatible backend while maintaining full compatibility with applications that use the OpenAI API. Perfect for AI teams working with custom LLM deployments, private models, or alternative providers.

🌟 Features

  • Complete OpenAI API Compatibility: Ensures compatibility with OpenAI's API version 2024-10-21 and above, including support for:

    • βœ… Chat completions with streaming
    • βœ… Function/tool calling with structured outputs
    • βœ… JSON mode responses
    • βœ… Multimodal content (vision models)
    • βœ… Text-to-speech and speech-to-text
    • βœ… Image generation (DALL-E)
    • βœ… Latest OpenAI models support (GPT-4o, GPT-4-turbo, etc.)
    • βœ… Third-generation embeddings models
    • βœ… Full models discovery API
  • Seamless Framework Integration: Works with any client that supports the OpenAI API:

    • βœ… Official OpenAI SDKs (Python, Node.js, etc.)
    • βœ… LangChain & LangChain.js
    • βœ… LlamaIndex
    • βœ… Semantic Kernel
    • βœ… Any other OpenAI-compatible framework
  • Enterprise-Ready:

    • βœ… Modular, maintainable code architecture
    • βœ… Comprehensive logging
    • βœ… Error handling with automatic token refresh
    • βœ… Easy configuration through environment variables
    • βœ… Docker containerization for simple deployment
    • βœ… Built on FastAPI for high performance

πŸ“‹ Table of Contents

πŸš€ Getting Started

Prerequisites

  • Python 3.10 or higher
  • Docker (optional, for containerized deployment)

Configuration

Create a .env file in the root directory with the following variables:

TOKEN_URL=your_url_to_get_token
CHAT_API_URL=your_url_of_base_api
EMBEDDING_API_URL=your_url_of_base_api
AUTH_CODE=your_authorization_code
VERIFY_SSL=False
PORT=8000
LOG_LEVEL=INFO

Installation

  1. Clone the repository:

    git clone https://github.com/yctimlin/OpenAI-Compatible-API-Mimic.git
    cd OpenAI-Compatible-API-Mimic
  2. Install dependencies:

    pip install -r requirements.txt

    Or use our setup script:

    ./setup.sh

Running Locally

Start the API server:

python main.py

The API will be available at http://localhost:8000 with interactive documentation at http://localhost:8000/docs.

Docker Deployment

  1. Build the Docker image:

    docker build -t openai-api-mimic .
  2. Run the container:

    docker run -p 8000:8000 --env-file .env openai-api-mimic

πŸ“‚ Project Structure

openai-api-mimic/
β”œβ”€β”€ src/                    # Source code directory
β”‚   β”œβ”€β”€ api/                # API route definitions
β”‚   β”‚   β”œβ”€β”€ chat.py         # Chat completions endpoint
β”‚   β”‚   β”œβ”€β”€ embeddings.py   # Embeddings endpoint
β”‚   β”‚   └── models.py       # Models listing endpoint
β”‚   β”œβ”€β”€ config/             # Configuration management
β”‚   β”‚   └── settings.py     # Application settings
β”‚   β”œβ”€β”€ models/             # Pydantic schema definitions
β”‚   β”‚   └── schema.py       # Request/response schemas
β”‚   β”œβ”€β”€ utils/              # Utility functions
β”‚   β”‚   β”œβ”€β”€ api.py          # API interaction utilities
β”‚   β”‚   └── models.py       # Model handling utilities
β”‚   β”œβ”€β”€ app.py              # FastAPI application instance
β”‚   └── __init__.py         # Package initialization
β”œβ”€β”€ main.py                 # Application entry point
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ Dockerfile              # Container definition
β”œβ”€β”€ .env.example            # Example environment variables
└── README.md               # Project documentation

πŸ“š API Documentation

Once the server is running, you can access the interactive API documentation at:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

API Endpoints

  • GET /
    Health check endpoint

  • GET /v1/models
    List all available models

  • GET /v1/models/{model_id}
    Get information about a specific model

  • POST /v1/chat/completions
    Create a chat completion

  • POST /v1/embeddings
    Create embeddings from input text

🧩 Usage Examples

Python with OpenAI SDK

import openai

client = openai.OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="dummy-key"  # API key is not validated but required by the SDK
)

# Chat completion
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, how are you?"}
    ],
    temperature=0.7
)

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

# Streaming example
for chunk in client.chat.completions.create(
    model="gpt-4-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Write a short poem about AI."}
    ],
    stream=True
):
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

# Embeddings
embedding_response = client.embeddings.create(
    model="text-embedding-3-small",
    input="The quick brown fox jumps over the lazy dog"
)

print(f"Embedding dimension: {len(embedding_response.data[0].embedding)}")

LangChain Integration

from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage

llm = ChatOpenAI(
    openai_api_base="http://localhost:8000/v1",
    openai_api_key="dummy-key",
    model="gpt-4o"
)

messages = [
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Explain how APIs work in simple terms.")
]

response = llm.invoke(messages)
print(response.content)

πŸ‘₯ Community & Support

πŸ—ΊοΈ Roadmap

  • Support for Azure OpenAI API compatibility
  • Additional logging and monitoring options
  • Helm chart for Kubernetes deployment
  • Authentication and rate limiting
  • Request/response validation middleware
  • Support for DALL-E 3 and other image generation endpoints

❓ FAQ

How does this differ from the OpenAI API?

This project creates a compatibility layer between your API backend and OpenAI's API format. It allows you to maintain your own backend implementation while providing a standard OpenAI-compatible interface for client applications.

Can I use this with any LLM provider?

Yes, as long as your backend can fulfill the requirements of the API - typically providing chat completions and embeddings. You'll need to implement the appropriate transformations in the backend utilities.

Is this suitable for production use?

Yes, the architecture follows best practices for production environments, including proper error handling, logging, and configuration. However, you should implement appropriate authentication and rate limiting for your specific use case.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add some amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Contributors

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ”— Related Projects


If you find this project helpful, please consider giving it a star ⭐

Built with ❀️ for the AI developer community

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published