Skip to content

Commit 31657b3

Browse files
authored
refactor: Make the SDK OpenAI compatible (#2)
* refactor: expand type definitions for chat completion and tool calls Signed-off-by: Eden Reich <eden.reich@gmail.com> * build: add GitHub CLI installation to Dockerfile Signed-off-by: Eden Reich <eden.reich@gmail.com> * chore: add deepseek keyword to package.json Signed-off-by: Eden Reich <eden.reich@gmail.com> --------- Signed-off-by: Eden Reich <eden.reich@gmail.com>
1 parent 14835e8 commit 31657b3

File tree

7 files changed

+1598
-925
lines changed

7 files changed

+1598
-925
lines changed

.devcontainer/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ ENV ZSH_CUSTOM=/home/node/.oh-my-zsh/custom \
66
RUN apt-get update && \
77
# Install Task
88
curl -s https://taskfile.dev/install.sh | sh -s -- -b /usr/local/bin ${TASK_VERSION} && \
9+
# Install GitHub CLI
10+
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && \
11+
chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && \
12+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null && \
13+
apt-get update && \
14+
apt-get install -y gh && \
15+
# Cleanup
916
apt-get clean && \
1017
rm -rf /var/lib/apt/lists/*
1118

README.md

Lines changed: 183 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
# Inference Gateway Typescript SDK
1+
# Inference Gateway TypeScript SDK
22

3-
An SDK written in Typescript for the [Inference Gateway](https://github.com/edenreich/inference-gateway).
3+
An SDK written in TypeScript for the [Inference Gateway](https://github.com/edenreich/inference-gateway).
44

5-
- [Inference Gateway Typescript SDK](#inference-gateway-typescript-sdk)
5+
- [Inference Gateway TypeScript SDK](#inference-gateway-typescript-sdk)
66
- [Installation](#installation)
77
- [Usage](#usage)
88
- [Creating a Client](#creating-a-client)
9-
- [Listing All Models](#listing-all-models)
10-
- [List Models by Provider](#list-models-by-provider)
11-
- [Generating Content](#generating-content)
12-
- [Streaming Content](#streaming-content)
9+
- [Listing Models](#listing-models)
10+
- [Creating Chat Completions](#creating-chat-completions)
11+
- [Streaming Chat Completions](#streaming-chat-completions)
12+
- [Tool Calls](#tool-calls)
13+
- [Proxying Requests](#proxying-requests)
1314
- [Health Check](#health-check)
15+
- [Creating a Client with Custom Options](#creating-a-client-with-custom-options)
1416
- [Contributing](#contributing)
1517
- [License](#license)
1618

@@ -22,152 +24,215 @@ Run `npm i @inference-gateway/sdk`.
2224

2325
### Creating a Client
2426

27+
```typescript
28+
import { InferenceGatewayClient } from '@inference-gateway/sdk';
29+
30+
// Create a client with default options
31+
const client = new InferenceGatewayClient({
32+
baseURL: 'http://localhost:8080/v1',
33+
apiKey: 'your-api-key', // Optional
34+
});
35+
```
36+
37+
### Listing Models
38+
39+
To list all available models:
40+
41+
```typescript
42+
import { InferenceGatewayClient, Provider } from '@inference-gateway/sdk';
43+
44+
const client = new InferenceGatewayClient({
45+
baseURL: 'http://localhost:8080/v1',
46+
});
47+
48+
try {
49+
// List all models
50+
const models = await client.listModels();
51+
console.log('All models:', models);
52+
53+
// List models from a specific provider
54+
const openaiModels = await client.listModels(Provider.OpenAI);
55+
console.log('OpenAI models:', openaiModels);
56+
} catch (error) {
57+
console.error('Error:', error);
58+
}
59+
```
60+
61+
### Creating Chat Completions
62+
63+
To generate content using a model:
64+
2565
```typescript
2666
import {
2767
InferenceGatewayClient,
28-
Message,
68+
MessageRole,
2969
Provider,
3070
} from '@inference-gateway/sdk';
3171

32-
async function main() {
33-
const client = new InferenceGatewayClient('http://localhost:8080');
34-
35-
try {
36-
// List available models
37-
const models = await client.listModels();
38-
models.forEach((providerModels) => {
39-
console.log(`Provider: ${providerModels.provider}`);
40-
providerModels.models.forEach((model) => {
41-
console.log(`Model: ${model.name}`);
42-
});
43-
});
44-
45-
// Generate content
46-
const response = await client.generateContent({
47-
provider: Provider.Ollama,
48-
model: 'llama2',
72+
const client = new InferenceGatewayClient({
73+
baseURL: 'http://localhost:8080/v1',
74+
});
75+
76+
try {
77+
const response = await client.createChatCompletion(
78+
{
79+
model: 'gpt-4o',
4980
messages: [
5081
{
5182
role: MessageRole.System,
52-
content: 'You are a helpful llama',
83+
content: 'You are a helpful assistant',
5384
},
5485
{
5586
role: MessageRole.User,
5687
content: 'Tell me a joke',
5788
},
5889
],
59-
});
60-
61-
console.log('Response:', response);
62-
} catch (error) {
63-
console.error('Error:', error);
64-
}
65-
}
66-
67-
main();
68-
```
69-
70-
### Listing All Models
71-
72-
To list all available models from all providers, use the `listModels` method:
90+
},
91+
Provider.OpenAI
92+
); // Provider is optional
7393

74-
```typescript
75-
try {
76-
const models = await client.listModels();
77-
models.forEach((providerModels) => {
78-
console.log(`Provider: ${providerModels.provider}`);
79-
providerModels.models.forEach((model) => {
80-
console.log(`Model: ${model.name}`);
81-
});
82-
});
94+
console.log('Response:', response.choices[0].message.content);
8395
} catch (error) {
8496
console.error('Error:', error);
8597
}
8698
```
8799

88-
### List Models by Provider
100+
### Streaming Chat Completions
89101

90-
To list all available models from a specific provider, use the `listModelsByProvider` method:
102+
To stream content from a model:
91103

92104
```typescript
105+
import {
106+
InferenceGatewayClient,
107+
MessageRole,
108+
Provider,
109+
} from '@inference-gateway/sdk';
110+
111+
const client = new InferenceGatewayClient({
112+
baseURL: 'http://localhost:8080/v1',
113+
});
114+
93115
try {
94-
const providerModels = await client.listModelsByProvider(Provider.OpenAI);
95-
console.log(`Provider: ${providerModels.provider}`);
96-
providerModels.models.forEach((model) => {
97-
console.log(`Model: ${model.name}`);
98-
});
116+
await client.streamChatCompletion(
117+
{
118+
model: 'llama-3.3-70b-versatile',
119+
messages: [
120+
{
121+
role: MessageRole.User,
122+
content: 'Tell me a story',
123+
},
124+
],
125+
},
126+
{
127+
onOpen: () => console.log('Stream opened'),
128+
onContent: (content) => process.stdout.write(content),
129+
onChunk: (chunk) => console.log('Received chunk:', chunk.id),
130+
onFinish: () => console.log('\nStream completed'),
131+
onError: (error) => console.error('Stream error:', error),
132+
},
133+
Provider.Groq // Provider is optional
134+
);
99135
} catch (error) {
100136
console.error('Error:', error);
101137
}
102138
```
103139

104-
### Generating Content
140+
### Tool Calls
105141

106-
To generate content using a model, use the `generateContent` method:
142+
To use tool calls with models that support them:
107143

108144
```typescript
109145
import {
110146
InferenceGatewayClient,
111-
Message,
112147
MessageRole,
113148
Provider,
114149
} from '@inference-gateway/sdk';
115150

116-
const client = new InferenceGatewayClient('http://localhost:8080');
151+
const client = new InferenceGatewayClient({
152+
baseURL: 'http://localhost:8080/v1',
153+
});
117154

118-
const response = await client.generateContent({
119-
provider: Provider.Ollama,
120-
model: 'llama2',
121-
messages: [
122-
{
123-
role: MessageRole.System,
124-
content: 'You are a helpful llama',
125-
},
126-
{
127-
role: MessageRole.User,
128-
content: 'Tell me a joke',
155+
try {
156+
await client.streamChatCompletion(
157+
{
158+
model: 'gpt-4o',
159+
messages: [
160+
{
161+
role: MessageRole.User,
162+
content: 'What's the weather in San Francisco?',
163+
},
164+
],
165+
tools: [
166+
{
167+
type: 'function',
168+
function: {
169+
name: 'get_weather',
170+
parameters: {
171+
type: 'object',
172+
properties: {
173+
location: {
174+
type: 'string',
175+
description: 'The city and state, e.g. San Francisco, CA',
176+
},
177+
},
178+
required: ['location'],
179+
},
180+
},
181+
},
182+
],
183+
},
184+
{
185+
onTool: (toolCall) => {
186+
console.log('Tool call:', toolCall.function.name);
187+
console.log('Arguments:', toolCall.function.arguments);
129188
},
130-
],
131-
});
132-
133-
console.log('Provider:', response.provider);
134-
console.log('Response:', response.response);
189+
onContent: (content) => process.stdout.write(content),
190+
onFinish: () => console.log('\nStream completed'),
191+
},
192+
Provider.OpenAI
193+
);
135194
} catch (error) {
136195
console.error('Error:', error);
137196
}
138197
```
139198
140-
### Streaming Content
199+
### Proxying Requests
141200
142-
To stream content using a model, use the `streamContent` method:
201+
To proxy requests directly to a provider:
143202
144203
```typescript
145-
const client = new InferenceGatewayClient('http://localhost:8080');
146-
147-
await client.generateContentStream(
148-
{
149-
provider: Provider.Groq,
150-
model: 'deepseek-r1-distill-llama-70b',
151-
messages: [
152-
{
153-
role: MessageRole.User,
154-
content: 'Tell me a story',
155-
},
156-
],
157-
},
158-
{
159-
onMessageStart: (role) => console.log('Message started:', role),
160-
onContentDelta: (content) => process.stdout.write(content),
161-
onStreamEnd: () => console.log('\nStream completed'),
162-
}
163-
);
204+
import { InferenceGatewayClient, Provider } from '@inference-gateway/sdk';
205+
206+
const client = new InferenceGatewayClient({
207+
baseURL: 'http://localhost:8080/v1',
208+
});
209+
210+
try {
211+
const response = await client.proxy(Provider.OpenAI, 'embeddings', {
212+
method: 'POST',
213+
body: JSON.stringify({
214+
model: 'text-embedding-ada-002',
215+
input: 'Hello world',
216+
}),
217+
});
218+
219+
console.log('Embeddings:', response);
220+
} catch (error) {
221+
console.error('Error:', error);
222+
}
164223
```
165224
166225
### Health Check
167226
168-
To check if the Inference Gateway is running, use the `healthCheck` method:
227+
To check if the Inference Gateway is running:
169228
170229
```typescript
230+
import { InferenceGatewayClient } from '@inference-gateway/sdk';
231+
232+
const client = new InferenceGatewayClient({
233+
baseURL: 'http://localhost:8080/v1',
234+
});
235+
171236
try {
172237
const isHealthy = await client.healthCheck();
173238
console.log('API is healthy:', isHealthy);
@@ -176,6 +241,26 @@ try {
176241
}
177242
```
178243
244+
### Creating a Client with Custom Options
245+
246+
You can create a new client with custom options using the `withOptions` method:
247+
248+
```typescript
249+
import { InferenceGatewayClient } from '@inference-gateway/sdk';
250+
251+
const client = new InferenceGatewayClient({
252+
baseURL: 'http://localhost:8080/v1',
253+
});
254+
255+
// Create a new client with custom headers
256+
const clientWithHeaders = client.withOptions({
257+
defaultHeaders: {
258+
'X-Custom-Header': 'value',
259+
},
260+
timeout: 60000, // 60 seconds
261+
});
262+
```
263+
179264
## Contributing
180265
181266
Please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file for information about how to get involved. We welcome issues, questions, and pull requests.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy