1
- # Inference Gateway Typescript SDK
1
+ # Inference Gateway TypeScript SDK
2
2
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 ) .
4
4
5
- - [ Inference Gateway Typescript SDK] ( #inference-gateway-typescript-sdk )
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 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 )
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
- Message ,
68
+ MessageRole ,
29
69
Provider ,
30
70
} from ' @inference-gateway/sdk' ;
31
71
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' ,
49
80
messages: [
50
81
{
51
82
role: MessageRole .System ,
52
- content: ' You are a helpful llama ' ,
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
- 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
73
93
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 );
83
95
} catch (error ) {
84
96
console .error (' Error:' , error );
85
97
}
86
98
```
87
99
88
- ### List Models by Provider
100
+ ### Streaming Chat Completions
89
101
90
- To list all available models from a specific provider, use the ` listModelsByProvider ` method :
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
- 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 (' \n Stream 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
- ### Generating Content
140
+ ### Tool Calls
105
141
106
- To generate content using a model, use the ` generateContent ` method :
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 (' http://localhost:8080' );
151
+ const client = new InferenceGatewayClient ({
152
+ baseURL: ' http://localhost:8080/v1' ,
153
+ });
117
154
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 );
129
188
},
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 ( ' \n Stream completed ' ),
191
+ },
192
+ Provider . OpenAI
193
+ );
135
194
} catch (error ) {
136
195
console.error(' Error:' , error);
137
196
}
138
197
` ` `
139
198
140
- ### Streaming Content
199
+ ### Proxying Requests
141
200
142
- To stream content using a model, use the ` streamContent ` method :
201
+ To proxy requests directly to a provider :
143
202
144
203
` ` ` 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 ( ' \n Stream 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
+ }
164
223
` ` `
165
224
166
225
### Health Check
167
226
168
- To check if the Inference Gateway is running, use the ` healthCheck ` method :
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.
0 commit comments