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.
-
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
- Features
- Getting Started
- Project Structure
- API Documentation
- Usage Examples
- Community & Support
- Roadmap
- FAQ
- Contributing
- License
- Python 3.10 or higher
- Docker (optional, for containerized deployment)
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
-
Clone the repository:
git clone https://github.com/yctimlin/OpenAI-Compatible-API-Mimic.git cd OpenAI-Compatible-API-Mimic -
Install dependencies:
pip install -r requirements.txt
Or use our setup script:
./setup.sh
Start the API server:
python main.pyThe API will be available at http://localhost:8000 with interactive documentation at http://localhost:8000/docs.
-
Build the Docker image:
docker build -t openai-api-mimic . -
Run the container:
docker run -p 8000:8000 --env-file .env openai-api-mimic
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
Once the server is running, you can access the interactive API documentation at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
-
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
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)}")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)- GitHub Discussions - Ask questions and share ideas
- GitHub Issues - Report bugs or request features
- Discord Server - Join our community
- 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
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.
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.
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.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add some amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this project helpful, please consider giving it a star β
Built with β€οΈ for the AI developer community