Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

vruru/anthropic-openai-proxy

Repository files navigation

Anthropic-to-OpenAI Translation Proxy (Go)

A Go rewrite of the Node.js proxy that translates between Anthropic Messages API and OpenAI Chat Completions format.

Features

  • Bidirectional Translation: Converts Anthropic requests to OpenAI format and translates responses back
  • Streaming Support: Handles both streaming and non-streaming responses with dual-buffer strategy for tool arguments
  • Tool/Function Calling: Full support for tool calls with JSON validation
  • Multi-modal Content: Supports text and image content blocks
  • Zero External Dependencies: Uses only Go standard library
  • CORS Enabled: Allows all origins for easy integration

Project Structure

anthropic-proxy-go/
├── go.mod # Go module definition
├── main.go # Entry point and server setup
├── config.go # Configuration with environment variables
├── types.go # All data structures (Anthropic & OpenAI)
├── translate.go # Request/response translation logic
├── stream.go # Streaming translation with dual-buffer strategy
├── handler.go # HTTP handler for proxy endpoints
├── middleware.go # CORS and logging middleware
└── Makefile # Build and run commands

Configuration

Environment variables:

Building

# Build binary
make build
# Run directly
make run
# Cross-compile for Linux amd64
make linux
# Clean build artifacts
make clean

Usage

  1. Start the proxy:

    export GATEWAY_KEY="your-api-key"
    ./anthropic-proxy
  2. Send Anthropic-formatted requests to POST /v1/messages:

    curl -X POST http://localhost:18900/v1/messages \
     -H "Content-Type: application/json" \
     -d '{
     "model": "claude-3-5-sonnet-20241022",
     "max_tokens": 1024,
     "messages": [{"role": "user", "content": "Hello!"}]
     }'

Translation Details

Request Translation (Anthropic → OpenAI)

  • System messages: Converted from string or array format to OpenAI system message
  • Messages: Supports text, image, tool_use, and tool_result blocks
  • Tools: Anthropic tool definitions mapped to OpenAI function format
  • Images: Base64 data URLs preserved in multipart content

Response Translation (OpenAI → Anthropic)

  • Content blocks: Text and tool_use blocks constructed from OpenAI response
  • Finish reasons: Mapped appropriately (tool_calls → tool_use, length → max_tokens)
  • Usage stats: Token counts translated to Anthropic format

Streaming Translation

  • Dual-buffer strategy: Maintains both replace and append buffers for tool arguments
  • JSON validation: Selects valid JSON from buffers at flush time
  • Text buffering: Delays text output until [DONE] to handle tool-use responses correctly
  • SSE format: Emits Anthropic-compatible Server-Sent Events

Key Differences from Node.js Version

  1. Type Safety: Strong typing with struct definitions instead of dynamic objects
  2. Error Handling: Explicit error checking throughout
  3. Concurrency: Go's native HTTP server with goroutines per request
  4. Standard Library: No external dependencies (net/http, encoding/json)
  5. Middleware Pattern: Composable middleware chain for request processing

Timeouts

  • Read timeout: 30 seconds
  • Write timeout: 300 seconds (long for streaming)
  • Idle timeout: 120 seconds

API Compatibility

Fully compatible with Anthropic Messages API v1:

  • POST /v1/messages
  • POST /messages (also supported)

Supports all Anthropic features:

  • System messages (string or array)
  • Multi-turn conversations
  • Tool/function calling
  • Multi-modal content (text + images)
  • Streaming responses
  • Temperature, top_p, stop_sequences parameters

About

Anthropic Messages API to OpenAI Chat Completions translation proxy (Go)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

AltStyle によって変換されたページ (->オリジナル) /