@inference-gateway/sdk 0.3.0 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/README.md +183 -98
- package/dist/src/client.d.ts +42 -8
- package/dist/src/client.js +175 -80
- package/dist/src/types/index.d.ts +104 -23
- package/dist/src/types/index.js +2 -1
- package/dist/tests/client.test.js +249 -140
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.3.3](https://github.com/inference-gateway/typescript-sdk/compare/v0.3.2...v0.3.3) (2025-03-31)
|
|
6
|
+
|
|
7
|
+
### 🐛 Bug Fixes
|
|
8
|
+
|
|
9
|
+
* Update release configuration to include version bumping of the package.json ([3667690](https://github.com/inference-gateway/typescript-sdk/commit/36676909a923bc29d398ad814f0518fcc12080aa))
|
|
10
|
+
|
|
11
|
+
## [0.3.2](https://github.com/inference-gateway/typescript-sdk/compare/v0.3.1...v0.3.2) (2025-03-31)
|
|
12
|
+
|
|
13
|
+
### 👷 CI
|
|
14
|
+
|
|
15
|
+
* Add npm ci step to install project dependencies in release workflow ([84791b1](https://github.com/inference-gateway/typescript-sdk/commit/84791b1e4c319f91798c456c783ded6e22da8f81))
|
|
16
|
+
|
|
17
|
+
## [0.3.1](https://github.com/inference-gateway/typescript-sdk/compare/v0.3.0...v0.3.1) (2025-03-31)
|
|
18
|
+
|
|
19
|
+
### ♻️ Improvements
|
|
20
|
+
|
|
21
|
+
* Make the SDK OpenAI compatible ([#2](https://github.com/inference-gateway/typescript-sdk/issues/2)) ([31657b3](https://github.com/inference-gateway/typescript-sdk/commit/31657b358f34ccc39acc5994248a95127f1ea46a))
|
|
22
|
+
|
|
23
|
+
### 👷 CI
|
|
24
|
+
|
|
25
|
+
* Update GitHub Actions release workflow to use GitHub App token and improve release handling ([14835e8](https://github.com/inference-gateway/typescript-sdk/commit/14835e8f9289314f34e711c02faf865ad9af6d66))
|
|
26
|
+
* Update release configuration for semantic-release plugins and rules to be consistent with other repos ([20bd3f8](https://github.com/inference-gateway/typescript-sdk/commit/20bd3f82c68d0b1ee1d07b4fa75eb67524db4fb8))
|
|
27
|
+
|
|
1
28
|
## [0.3.0](https://github.com/inference-gateway/typescript-sdk/compare/v0.2.0...v0.3.0) (2025-02-02)
|
|
2
29
|
|
|
3
30
|
### ✨ Features
|
package/README.md
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
# Inference Gateway
|
|
1
|
+
# Inference Gateway TypeScript SDK
|
|
2
2
|
|
|
3
|
-
An SDK written in
|
|
3
|
+
An SDK written in TypeScript for the [Inference Gateway](https://github.com/edenreich/inference-gateway).
|
|
4
4
|
|
|
5
|
-
- [Inference Gateway
|
|
5
|
+
- [Inference Gateway TypeScript SDK](#inference-gateway-typescript-sdk)
|
|
6
6
|
- [Installation](#installation)
|
|
7
7
|
- [Usage](#usage)
|
|
8
8
|
- [Creating a Client](#creating-a-client)
|
|
9
|
-
- [Listing
|
|
10
|
-
- [
|
|
11
|
-
- [
|
|
12
|
-
- [
|
|
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)
|
|
13
14
|
- [Health Check](#health-check)
|
|
15
|
+
- [Creating a Client with Custom Options](#creating-a-client-with-custom-options)
|
|
14
16
|
- [Contributing](#contributing)
|
|
15
17
|
- [License](#license)
|
|
16
18
|
|
|
@@ -22,152 +24,215 @@ Run `npm i @inference-gateway/sdk`.
|
|
|
22
24
|
|
|
23
25
|
### Creating a Client
|
|
24
26
|
|
|
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
|
+
|
|
25
65
|
```typescript
|
|
26
66
|
import {
|
|
27
67
|
InferenceGatewayClient,
|
|
28
|
-
|
|
68
|
+
MessageRole,
|
|
29
69
|
Provider,
|
|
30
70
|
} from '@inference-gateway/sdk';
|
|
31
71
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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',
|
|
49
80
|
messages: [
|
|
50
81
|
{
|
|
51
82
|
role: MessageRole.System,
|
|
52
|
-
content: 'You are a helpful
|
|
83
|
+
content: 'You are a helpful assistant',
|
|
53
84
|
},
|
|
54
85
|
{
|
|
55
86
|
role: MessageRole.User,
|
|
56
87
|
content: 'Tell me a joke',
|
|
57
88
|
},
|
|
58
89
|
],
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
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
|
|
73
93
|
|
|
74
|
-
|
|
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);
|
|
83
95
|
} catch (error) {
|
|
84
96
|
console.error('Error:', error);
|
|
85
97
|
}
|
|
86
98
|
```
|
|
87
99
|
|
|
88
|
-
###
|
|
100
|
+
### Streaming Chat Completions
|
|
89
101
|
|
|
90
|
-
To
|
|
102
|
+
To stream content from a model:
|
|
91
103
|
|
|
92
104
|
```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
|
+
|
|
93
115
|
try {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
+
);
|
|
99
135
|
} catch (error) {
|
|
100
136
|
console.error('Error:', error);
|
|
101
137
|
}
|
|
102
138
|
```
|
|
103
139
|
|
|
104
|
-
###
|
|
140
|
+
### Tool Calls
|
|
105
141
|
|
|
106
|
-
To
|
|
142
|
+
To use tool calls with models that support them:
|
|
107
143
|
|
|
108
144
|
```typescript
|
|
109
145
|
import {
|
|
110
146
|
InferenceGatewayClient,
|
|
111
|
-
Message,
|
|
112
147
|
MessageRole,
|
|
113
148
|
Provider,
|
|
114
149
|
} from '@inference-gateway/sdk';
|
|
115
150
|
|
|
116
|
-
const client = new InferenceGatewayClient(
|
|
151
|
+
const client = new InferenceGatewayClient({
|
|
152
|
+
baseURL: 'http://localhost:8080/v1',
|
|
153
|
+
});
|
|
117
154
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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);
|
|
129
188
|
},
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
189
|
+
onContent: (content) => process.stdout.write(content),
|
|
190
|
+
onFinish: () => console.log('\nStream completed'),
|
|
191
|
+
},
|
|
192
|
+
Provider.OpenAI
|
|
193
|
+
);
|
|
135
194
|
} catch (error) {
|
|
136
195
|
console.error('Error:', error);
|
|
137
196
|
}
|
|
138
197
|
```
|
|
139
198
|
|
|
140
|
-
###
|
|
199
|
+
### Proxying Requests
|
|
141
200
|
|
|
142
|
-
To
|
|
201
|
+
To proxy requests directly to a provider:
|
|
143
202
|
|
|
144
203
|
```typescript
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
+
}
|
|
164
223
|
```
|
|
165
224
|
|
|
166
225
|
### Health Check
|
|
167
226
|
|
|
168
|
-
To check if the Inference Gateway is running
|
|
227
|
+
To check if the Inference Gateway is running:
|
|
169
228
|
|
|
170
229
|
```typescript
|
|
230
|
+
import { InferenceGatewayClient } from '@inference-gateway/sdk';
|
|
231
|
+
|
|
232
|
+
const client = new InferenceGatewayClient({
|
|
233
|
+
baseURL: 'http://localhost:8080/v1',
|
|
234
|
+
});
|
|
235
|
+
|
|
171
236
|
try {
|
|
172
237
|
const isHealthy = await client.healthCheck();
|
|
173
238
|
console.log('API is healthy:', isHealthy);
|
|
@@ -176,6 +241,26 @@ try {
|
|
|
176
241
|
}
|
|
177
242
|
```
|
|
178
243
|
|
|
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
|
+
|
|
179
264
|
## Contributing
|
|
180
265
|
|
|
181
266
|
Please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) file for information about how to get involved. We welcome issues, questions, and pull requests.
|
package/dist/src/client.d.ts
CHANGED
|
@@ -1,12 +1,46 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatCompletionRequest, ChatCompletionResponse, ChatCompletionStreamCallbacks, ListModelsResponse, Provider } from './types';
|
|
2
|
+
export interface ClientOptions {
|
|
3
|
+
baseURL?: string;
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
defaultHeaders?: Record<string, string>;
|
|
6
|
+
defaultQuery?: Record<string, string>;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
fetch?: typeof globalThis.fetch;
|
|
9
|
+
}
|
|
2
10
|
export declare class InferenceGatewayClient {
|
|
3
|
-
private
|
|
4
|
-
private
|
|
5
|
-
|
|
11
|
+
private baseURL;
|
|
12
|
+
private apiKey?;
|
|
13
|
+
private defaultHeaders;
|
|
14
|
+
private defaultQuery;
|
|
15
|
+
private timeout;
|
|
16
|
+
private fetchFn;
|
|
17
|
+
constructor(options?: ClientOptions);
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new instance of the client with the given options merged with the existing options.
|
|
20
|
+
*/
|
|
21
|
+
withOptions(options: ClientOptions): InferenceGatewayClient;
|
|
22
|
+
/**
|
|
23
|
+
* Makes a request to the API.
|
|
24
|
+
*/
|
|
6
25
|
private request;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Lists the currently available models.
|
|
28
|
+
*/
|
|
29
|
+
listModels(provider?: Provider): Promise<ListModelsResponse>;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a chat completion.
|
|
32
|
+
*/
|
|
33
|
+
createChatCompletion(request: ChatCompletionRequest, provider?: Provider): Promise<ChatCompletionResponse>;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a streaming chat completion.
|
|
36
|
+
*/
|
|
37
|
+
streamChatCompletion(request: ChatCompletionRequest, callbacks: ChatCompletionStreamCallbacks, provider?: Provider): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Proxy a request to a specific provider.
|
|
40
|
+
*/
|
|
41
|
+
proxy<T = unknown>(provider: Provider, path: string, options?: RequestInit): Promise<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Health check endpoint.
|
|
44
|
+
*/
|
|
11
45
|
healthCheck(): Promise<boolean>;
|
|
12
46
|
}
|