openai-to-azure-openai-cli
v1.0.0
Published
CLI tool to convert OpenAI code (curl, Python, JavaScript/TypeScript) to Azure OpenAI compatible code
Maintainers
Readme
OpenAI to Azure OpenAI Converter CLI
A command-line tool that converts OpenAI code (curl, Python, JavaScript/TypeScript, LangChain) to Azure OpenAI compatible code.
Installation
# Install globally from npm
npm install -g openai-to-azure-openai-cli
# Or use directly with npx
npx openai-to-azure-openai-cli --helpFrom Source
# Clone the repository
git clone https://github.com/davidhou17/openai-to-azure-openai-cli.git
cd openai-to-azure-openai-cli
# Install dependencies
npm install
# Build the project
npm run build
# Link globally (optional)
npm linkUsage
Basic Usage
# From stdin
echo 'your code here' | openai-to-azure
# From file
openai-to-azure -f input.py
# From string argument
openai-to-azure 'curl https://api.openai.com/v1/chat/completions ...'Options
Usage: openai-to-azure [options] [input]
Arguments:
input Input code string or file path
Options:
-V, --version output the version number
-f, --file <path> Read input from a file
-l, --language <lang> Specify the language (curl, python, javascript, typescript, langchain)
--no-comments Disable adding environment variable comments to output
-o, --output <path> Write output to a file (default: stdout)
-q, --quiet Suppress warnings and info messages
-h, --help display help for command
Commands:
detect [options] [input] Detect the language of the input codeRequired Environment Variables
After conversion, your Azure OpenAI code will use these environment variables:
| Variable | Description |
|----------|-------------|
| AZURE_OPENAI_ENDPOINT | Your Azure OpenAI endpoint (e.g., https://your-resource.openai.azure.com) |
| AZURE_OPENAI_API_KEY | Your Azure OpenAI API key |
| OPENAI_API_VERSION | API version (e.g., 2024-02-15-preview) |
| AZURE_OPENAI_DEPLOYMENT | Your deployment name |
Examples
cURL Conversion
Input:
curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello!"}]}'Output:
curl ${AZURE_OPENAI_ENDPOINT}/openai/deployments/${AZURE_OPENAI_DEPLOYMENT}/chat/completions?api-version=${OPENAI_API_VERSION:-2024-02-15-preview} \
-H "api-key: $AZURE_OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello!"}]}'Python Conversion
Input:
from openai import OpenAI
client = OpenAI(api_key="sk-xxx")
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)Output:
from openai import AzureOpenAI
import os
client = AzureOpenAI(
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("OPENAI_API_VERSION", "2024-02-15-preview")
)
response = client.chat.completions.create(
model=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
messages=[{"role": "user", "content": "Hello!"}]
)JavaScript/TypeScript Conversion
Input:
import OpenAI from 'openai';
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
});Output:
import { AzureOpenAI } from 'openai';
const client = new AzureOpenAI({
apiKey: process.env.AZURE_OPENAI_API_KEY,
endpoint: process.env.AZURE_OPENAI_ENDPOINT,
apiVersion: process.env.OPENAI_API_VERSION || '2024-02-15-preview',
});
const response = await client.chat.completions.create({
model: process.env.AZURE_OPENAI_DEPLOYMENT,
messages: [{ role: 'user', content: 'Hello!' }],
});LangChain Conversion
Input:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4",
temperature=0,
max_tokens=None,
)
messages = [("system", "You are a helpful assistant."), ("human", "Hello!")]
response = llm.invoke(messages)Output:
import os
from langchain_openai import AzureChatOpenAI
llm = AzureChatOpenAI(
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT"),
api_version=os.getenv("OPENAI_API_VERSION", "2024-02-15-preview"),
)
messages = [("system", "You are a helpful assistant."), ("human", "Hello!")]
response = llm.invoke(messages)Supported Conversions
OpenAI SDK
- Python:
OpenAI→AzureOpenAI - JavaScript/TypeScript:
OpenAI→AzureOpenAI
LangChain
- Python:
ChatOpenAI→AzureChatOpenAI
curl Endpoints
/v1/chat/completions→ Chat completions/v1/completions→ Text completions/v1/embeddings→ Embeddings/v1/images/generations→ Image generation (DALL-E)/v1/audio/transcriptions→ Audio transcription (Whisper)/v1/audio/translations→ Audio translation (Whisper)
Development
Running Tests
# Run all tests
npm test
# Run tests in watch mode
npm run test:watchBuilding
npm run buildLicense
MIT
