Skip to content

Commit bb4acaa

Browse files
committed
feat: anthropic & google
1 parent 9e6c1e3 commit bb4acaa

File tree

23 files changed

+6807
-5
lines changed

23 files changed

+6807
-5
lines changed

.github/workflows/ci.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [18, 20, 22]
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Enable Corepack
21+
run: corepack enable
22+
23+
- name: Setup Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
cache: yarn
28+
29+
- name: Install dependencies
30+
run: yarn install --immutable
31+
32+
- name: Run tests
33+
run: yarn test:run
34+
35+
build:
36+
runs-on: ubuntu-latest
37+
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- name: Enable Corepack
42+
run: corepack enable
43+
44+
- name: Setup Node.js
45+
uses: actions/setup-node@v4
46+
with:
47+
node-version: 22
48+
cache: yarn
49+
50+
- name: Install dependencies
51+
run: yarn install --immutable
52+
53+
- name: Build
54+
run: yarn build
55+
56+
- name: Check build output
57+
run: |
58+
test -f dist/inference-net-provider-conversions.js
59+
test -f dist/inference-net-provider-conversions.cjs
60+
test -f dist/index.d.ts

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Use Context, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,170 @@
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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@inference-net/provider-conversions",
33
"version": "0.0.1",
4+
"license": "MIT",
45
"type": "module",
56
"packageManager": "yarn@4.11.0",
67
"main": "./dist/inference-net-provider-conversions.cjs",
@@ -23,6 +24,7 @@
2324
},
2425
"devDependencies": {
2526
"@anthropic-ai/sdk": "^0.71.0",
27+
"@google/genai": "^1.30.0",
2628
"ai": "^5.0.102",
2729
"openai": "^6.9.1",
2830
"typescript": "^5.9.3",

0 commit comments

Comments
 (0)