|
1 | | -# provider-conversions |
| 1 | +# @inference-net/provider-conversions |
| 2 | + |
| 3 | +Convert between LLM provider API formats (OpenAI, Anthropic, Google GenAI) and the Vercel AI SDK format. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @inference-net/provider-conversions |
| 9 | +``` |
| 10 | + |
| 11 | +### Peer Dependencies |
| 12 | + |
| 13 | +You'll need to install the provider SDKs you plan to use: |
| 14 | + |
| 15 | +```bash |
| 16 | +# For OpenAI conversions |
| 17 | +npm install openai |
| 18 | + |
| 19 | +# For Anthropic conversions |
| 20 | +npm install @anthropic-ai/sdk |
| 21 | + |
| 22 | +# For Google GenAI conversions |
| 23 | +npm install @google/genai |
| 24 | + |
| 25 | +# For AI SDK types (required) |
| 26 | +npm install ai @ai-sdk/provider |
| 27 | +``` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### OpenAI Chat Completions |
| 32 | + |
| 33 | +```typescript |
| 34 | +import { OpenAIChatCompletion } from "@inference-net/provider-conversions"; |
| 35 | + |
| 36 | +// Convert OpenAI request → AI SDK format |
| 37 | +const aiRequest = OpenAIChatCompletion.openaiToAiSDK(openaiRequest); |
| 38 | + |
| 39 | +// Convert AI SDK response → OpenAI format |
| 40 | +const openaiResponse = OpenAIChatCompletion.openaiFromAiSDK(aiResponse, { |
| 41 | + requestId: "req_123", |
| 42 | + model: "gpt-4", |
| 43 | + created: Date.now(), |
| 44 | +}); |
| 45 | + |
| 46 | +// Convert AI SDK stream chunks → OpenAI format |
| 47 | +const context = OpenAIChatCompletion.createChunkConversionContext({ |
| 48 | + requestId: "req_123", |
| 49 | + model: "gpt-4", |
| 50 | + created: Date.now(), |
| 51 | +}); |
| 52 | + |
| 53 | +for await (const chunk of aiStream) { |
| 54 | + const result = OpenAIChatCompletion.openaiChunkFromAiSDK(chunk, context); |
| 55 | + if (result.type === "chunk") { |
| 56 | + // Send result.chunk to client |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Anthropic Messages |
| 62 | + |
| 63 | +```typescript |
| 64 | +import { AnthropicMessages } from "@inference-net/provider-conversions"; |
| 65 | + |
| 66 | +// Convert Anthropic request → AI SDK format |
| 67 | +const aiRequest = AnthropicMessages.anthropicToAiSDK(anthropicRequest); |
| 68 | + |
| 69 | +// Convert AI SDK response → Anthropic format |
| 70 | +const anthropicResponse = AnthropicMessages.anthropicFromAiSDK(aiResponse, { |
| 71 | + responseId: "msg_123", |
| 72 | + model: "claude-3-opus-20240229", |
| 73 | +}); |
| 74 | + |
| 75 | +// Convert AI SDK stream chunks → Anthropic format |
| 76 | +const context = AnthropicMessages.createChunkConversionContext({ |
| 77 | + responseId: "msg_123", |
| 78 | + model: "claude-3-opus-20240229", |
| 79 | +}); |
| 80 | + |
| 81 | +for await (const chunk of aiStream) { |
| 82 | + const result = AnthropicMessages.anthropicChunkFromAiSDK(chunk, context); |
| 83 | + if (result.type === "events") { |
| 84 | + for (const event of result.events) { |
| 85 | + // Send SSE event to client |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Google GenAI |
| 92 | + |
| 93 | +```typescript |
| 94 | +import { GoogleGenAI } from "@inference-net/provider-conversions"; |
| 95 | + |
| 96 | +// Convert Google GenAI request → AI SDK format |
| 97 | +const aiRequest = GoogleGenAI.googleGenaiToAiSDK(googleRequest); |
| 98 | + |
| 99 | +// Convert AI SDK response → Google GenAI format |
| 100 | +const googleResponse = GoogleGenAI.googleGenaiFromAiSDK(aiResponse, { |
| 101 | + responseId: "resp_123", |
| 102 | + modelVersion: "gemini-1.5-pro", |
| 103 | +}); |
| 104 | + |
| 105 | +// Convert AI SDK stream chunks → Google GenAI format |
| 106 | +const context = GoogleGenAI.createChunkConversionContext({ |
| 107 | + responseId: "resp_123", |
| 108 | + modelVersion: "gemini-1.5-pro", |
| 109 | +}); |
| 110 | + |
| 111 | +for await (const chunk of aiStream) { |
| 112 | + const result = GoogleGenAI.googleChunkFromAiSDK(chunk, context); |
| 113 | + if (result.type === "response") { |
| 114 | + // Send result.response to client |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Tool/Function Calling |
| 120 | + |
| 121 | +All providers support tool/function calling conversions: |
| 122 | + |
| 123 | +```typescript |
| 124 | +// OpenAI tools → AI SDK tools |
| 125 | +const openaiRequest = { |
| 126 | + model: "gpt-4", |
| 127 | + messages: [{ role: "user", content: "What's the weather?" }], |
| 128 | + tools: [{ |
| 129 | + type: "function", |
| 130 | + function: { |
| 131 | + name: "get_weather", |
| 132 | + description: "Get current weather", |
| 133 | + parameters: { |
| 134 | + type: "object", |
| 135 | + properties: { |
| 136 | + location: { type: "string" } |
| 137 | + }, |
| 138 | + required: ["location"] |
| 139 | + } |
| 140 | + } |
| 141 | + }] |
| 142 | +}; |
| 143 | + |
| 144 | +const aiRequest = OpenAIChatCompletion.openaiToAiSDK(openaiRequest); |
| 145 | +// aiRequest.tools contains the converted tool definitions |
| 146 | +``` |
| 147 | + |
| 148 | +## Type Exports |
| 149 | + |
| 150 | +Access AI SDK types for building your own conversions: |
| 151 | + |
| 152 | +```typescript |
| 153 | +import { AISDKTypes } from "@inference-net/provider-conversions"; |
| 154 | + |
| 155 | +type Message = AISDKTypes.AiSDKMessage; |
| 156 | +type Response = AISDKTypes.AiSDKResponse; |
| 157 | +type Chunk = AISDKTypes.AiSDKChunk; |
| 158 | +``` |
| 159 | + |
| 160 | +## API Reference |
| 161 | + |
| 162 | +| Provider | Request Conversion | Response Conversion | Stream Conversion | |
| 163 | +|----------|-------------------|--------------------|--------------------| |
| 164 | +| OpenAI | `openaiToAiSDK()` | `openaiFromAiSDK()` | `openaiChunkFromAiSDK()` | |
| 165 | +| Anthropic | `anthropicToAiSDK()` | `anthropicFromAiSDK()` | `anthropicChunkFromAiSDK()` | |
| 166 | +| Google GenAI | `googleGenaiToAiSDK()` | `googleGenaiFromAiSDK()` | `googleChunkFromAiSDK()` | |
| 167 | + |
| 168 | +## License |
| 169 | + |
| 170 | +MIT |
0 commit comments