claude2openai
v1.0.5
Published
A proxy service to convert between Claude and OpenAI API formats
Readme
Claude to OpenAI API Adapter
A TypeScript-based proxy service that converts Claude API requests to OpenAI API format and vice versa. This allows applications built for the Claude API to work with OpenAI models, and potentially other LLM APIs in the future.
Features
- Convert Claude API requests to OpenAI API format
- Convert OpenAI API responses back to Claude format
- Support for both Claude Completions API and Claude Messages API
- Support for both streaming and non-streaming completions
- Optional support for function calls/tools (configurable)
- Customizable OpenAI API URL, API key, and model
- Extensible architecture for adding support for other API formats (e.g., Gemini)
- Configurable via environment variables
Installation
- Clone the repository:
git clone https://github.com/yourusername/claude2openai.git
cd claude2openai- Install dependencies:
npm install- Build the project:
npm run buildConfiguration
The service can be configured using environment variables:
PORT: The port to run the server on (default: 3000)OPENAI_API_KEY: Your OpenAI API keyCLAUDE_API_KEY: Your Claude API key (for future use)OPENAI_API_URL: The OpenAI API URL (default: https://api.openai.com/v1)CLAUDE_API_URL: The Claude API URL (default: https://api.anthropic.com/v1)OPENAI_MODEL: The OpenAI model to use (default: gpt-4-turbo)ENABLE_TOOLS: Enable function calls/tools support (default: false)LOG_LEVEL: The log level (default: info)
Create a .env file in the root directory with your configuration:
PORT=3000
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_API_URL=https://api.openai.com/v1
OPENAI_MODEL=gpt-4-turbo
ENABLE_TOOLS=false
LOG_LEVEL=infoUsage
- Start the server in development mode:
npm run devOr in production mode:
npm start- Install claude code
npm install -g @anthropic-ai/claude-code
export ANTHROPIC_AUTH_TOKEN=sk-litellm-static-key
export ANTHROPIC_BASE_URL=http://127.0.0.1:3000
claude- Send Claude API requests to the proxy:
Using Claude Completions API
curl -X POST http://localhost:3000/v1/complete \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-opus-20240229",
"max_tokens_to_sample": 1000,
"temperature": 0.7,
"messages": [
{
"role": "user",
"content": "Hello, Claude!"
}
]
}'Using Claude Messages API
curl -X POST http://localhost:3000/v1/messages \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-3-opus-20240229",
"max_tokens": 1000,
"temperature": 0.7,
"messages": [
{
"role": "user",
"content": "Hello, Claude!"
}
]
}'The proxy will convert the Claude request to OpenAI format, send it to the OpenAI API, and return the response in Claude format.
API Endpoints
POST /v1/complete: Handle Claude Completions API requestsPOST /v1/complete_stream: Handle Claude Completions API streaming requestsPOST /v1/messages: Handle Claude Messages API requests (supports both streaming and non-streaming)GET /health: Health check endpoint
Project Structure
src/
├── adapters/ # API format adapters
│ ├── base.adapter.ts # Base adapter class
│ ├── claude-to-openai.adapter.ts # Claude to OpenAI adapter
│ └── adapter-registry.ts # Registry for managing adapters
├── config/ # Configuration
│ └── config.ts # Application configuration
├── routes/ # API routes
│ └── claude-to-openai.route.ts # Claude to OpenAI route
├── types/ # Type definitions
│ └── index.ts # API type definitions
├── utils/ # Utility functions
│ ├── http-client.ts # HTTP client for API requests
│ └── logger.ts # Logger utility
└── index.ts # Main application entry pointModel Mapping
By default, the adapter will use the model specified in the OPENAI_MODEL environment variable. If not specified, it will map Claude models to OpenAI models as follows:
claude-3-opus-20240229→gpt-4-turboclaude-3-sonnet-20240229→gpt-4claude-3-haiku-20240307→gpt-3.5-turboclaude-2.1→gpt-4claude-2.0→gpt-4claude-instant-1.2→gpt-3.5-turbo
Custom OpenAI API Endpoints
You can configure the adapter to use a custom OpenAI API endpoint by setting the OPENAI_API_URL environment variable. This is useful if you want to use a self-hosted OpenAI-compatible API or a different provider that supports the OpenAI API format.
Example:
OPENAI_API_URL=http://localhost:8000/v1Function Calls / Tools Support
The adapter includes optional support for Claude's tools feature (equivalent to OpenAI's function calls). This feature is disabled by default due to potential compatibility issues with some OpenAI-compatible backends.
To enable tools support:
- Set the
ENABLE_TOOLSenvironment variable totrue - Send Claude API requests with the
toolsparameter
Example request with tools:
curl -X POST http://localhost:3000/v1/messages \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-3-opus-20240229",
"max_tokens": 1000,
"temperature": 0.7,
"messages": [
{
"role": "user",
"content": "What is the weather in San Francisco?"
}
],
"tools": [
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use"
}
},
"required": ["location"]
}
}
]
}'Future Enhancements
- Add support for Gemini API
- Add authentication and rate limiting
- Add support for more Claude API features
- Add tests
License
ISC
