Skip to content

Commit 1ef6159

Browse files
committed
refactor: Remove redundant request option
When calling streamChatCompletion it's obvious that it's a stream so no need to set it. Also when calling createChatCompletion it's obvious that it's a standard completion - without streaming.
1 parent 370dfaf commit 1ef6159

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/client.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class InferenceGatewayClient {
135135
* Creates a chat completion.
136136
*/
137137
async createChatCompletion(
138-
request: SchemaCreateChatCompletionRequest,
138+
request: Omit<SchemaCreateChatCompletionRequest, 'stream'>,
139139
provider?: Provider
140140
): Promise<SchemaCreateChatCompletionResponse> {
141141
const query: Record<string, string> = {};
@@ -146,17 +146,25 @@ export class InferenceGatewayClient {
146146
'/chat/completions',
147147
{
148148
method: 'POST',
149-
body: JSON.stringify(request),
149+
body: JSON.stringify({ ...request, stream: false }),
150150
},
151151
query
152152
);
153153
}
154154

155155
/**
156156
* Creates a streaming chat completion.
157+
* This method always sets stream=true internally, so there's no need to specify it in the request.
158+
*
159+
* @param request - Chat completion request (must include at least model and messages)
160+
* @param callbacks - Callbacks for handling streaming events
161+
* @param provider - Optional provider to use for this request
157162
*/
158163
async streamChatCompletion(
159-
request: SchemaCreateChatCompletionRequest,
164+
request: Omit<
165+
SchemaCreateChatCompletionRequest,
166+
'stream' | 'stream_options'
167+
>,
160168
callbacks: ChatCompletionStreamCallbacks,
161169
provider?: Provider
162170
): Promise<void> {
@@ -195,6 +203,9 @@ export class InferenceGatewayClient {
195203
body: JSON.stringify({
196204
...request,
197205
stream: true,
206+
stream_options: {
207+
include_usage: true,
208+
},
198209
}),
199210
signal: controller.signal,
200211
});

tests/client.test.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ describe('InferenceGatewayClient', () => {
116116
{ role: MessageRole.system, content: 'You are a helpful assistant' },
117117
{ role: MessageRole.user, content: 'Hello' },
118118
],
119-
stream: false,
120119
};
121120

122121
const mockResponse: SchemaCreateChatCompletionResponse = {
@@ -152,7 +151,7 @@ describe('InferenceGatewayClient', () => {
152151
'http://localhost:8080/v1/chat/completions',
153152
expect.objectContaining({
154153
method: 'POST',
155-
body: JSON.stringify(mockRequest),
154+
body: JSON.stringify({ ...mockRequest, stream: false }),
156155
})
157156
);
158157
});
@@ -161,7 +160,6 @@ describe('InferenceGatewayClient', () => {
161160
const mockRequest = {
162161
model: 'claude-3-opus-20240229',
163162
messages: [{ role: MessageRole.user, content: 'Hello' }],
164-
stream: false,
165163
};
166164

167165
const mockResponse: SchemaCreateChatCompletionResponse = {
@@ -200,7 +198,7 @@ describe('InferenceGatewayClient', () => {
200198
'http://localhost:8080/v1/chat/completions?provider=anthropic',
201199
expect.objectContaining({
202200
method: 'POST',
203-
body: JSON.stringify(mockRequest),
201+
body: JSON.stringify({ ...mockRequest, stream: false }),
204202
})
205203
);
206204
});
@@ -211,7 +209,6 @@ describe('InferenceGatewayClient', () => {
211209
const mockRequest = {
212210
model: 'gpt-4o',
213211
messages: [{ role: MessageRole.user, content: 'Hello' }],
214-
stream: true,
215212
};
216213

217214
const mockStream = new TransformStream();
@@ -258,6 +255,9 @@ describe('InferenceGatewayClient', () => {
258255
body: JSON.stringify({
259256
...mockRequest,
260257
stream: true,
258+
stream_options: {
259+
include_usage: true,
260+
},
261261
}),
262262
})
263263
);
@@ -267,7 +267,6 @@ describe('InferenceGatewayClient', () => {
267267
const mockRequest = {
268268
model: 'gpt-4o',
269269
messages: [{ role: MessageRole.user, content: 'Hello' }],
270-
stream: true,
271270
};
272271
const mockStream = new TransformStream();
273272
const writer = mockStream.writable.getWriter();
@@ -318,6 +317,9 @@ describe('InferenceGatewayClient', () => {
318317
body: JSON.stringify({
319318
...mockRequest,
320319
stream: true,
320+
stream_options: {
321+
include_usage: true,
322+
},
321323
}),
322324
})
323325
);
@@ -341,7 +343,6 @@ describe('InferenceGatewayClient', () => {
341343
},
342344
},
343345
],
344-
stream: true,
345346
};
346347

347348
const mockStream = new TransformStream();
@@ -390,13 +391,25 @@ describe('InferenceGatewayClient', () => {
390391
},
391392
});
392393
expect(callbacks.onFinish).toHaveBeenCalledTimes(1);
394+
expect(mockFetch).toHaveBeenCalledWith(
395+
'http://localhost:8080/v1/chat/completions',
396+
expect.objectContaining({
397+
method: 'POST',
398+
body: JSON.stringify({
399+
...mockRequest,
400+
stream: true,
401+
stream_options: {
402+
include_usage: true,
403+
},
404+
}),
405+
})
406+
);
393407
});
394408

395409
it('should handle errors in streaming chat completions', async () => {
396410
const mockRequest = {
397411
model: 'gpt-4o',
398412
messages: [{ role: MessageRole.user, content: 'Hello' }],
399-
stream: true,
400413
};
401414

402415
mockFetch.mockResolvedValueOnce({
@@ -420,10 +433,6 @@ describe('InferenceGatewayClient', () => {
420433
const mockRequest = {
421434
model: 'gpt-4o',
422435
messages: [{ role: MessageRole.user, content: 'Hello' }],
423-
stream: true,
424-
stream_options: {
425-
include_usage: true,
426-
},
427436
};
428437

429438
const mockStream = new TransformStream();
@@ -478,6 +487,9 @@ describe('InferenceGatewayClient', () => {
478487
body: JSON.stringify({
479488
...mockRequest,
480489
stream: true,
490+
stream_options: {
491+
include_usage: true,
492+
},
481493
}),
482494
})
483495
);

0 commit comments

Comments
 (0)