Skip to content

refactor: Remove redundant request option #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class InferenceGatewayClient {
* Creates a chat completion.
*/
async createChatCompletion(
request: SchemaCreateChatCompletionRequest,
request: Omit<SchemaCreateChatCompletionRequest, 'stream'>,
provider?: Provider
): Promise<SchemaCreateChatCompletionResponse> {
const query: Record<string, string> = {};
Expand All @@ -146,17 +146,25 @@ export class InferenceGatewayClient {
'/chat/completions',
{
method: 'POST',
body: JSON.stringify(request),
body: JSON.stringify({ ...request, stream: false }),
},
query
);
}

/**
* Creates a streaming chat completion.
* This method always sets stream=true internally, so there's no need to specify it in the request.
*
* @param request - Chat completion request (must include at least model and messages)
* @param callbacks - Callbacks for handling streaming events
* @param provider - Optional provider to use for this request
*/
async streamChatCompletion(
request: SchemaCreateChatCompletionRequest,
request: Omit<
SchemaCreateChatCompletionRequest,
'stream' | 'stream_options'
>,
callbacks: ChatCompletionStreamCallbacks,
provider?: Provider
): Promise<void> {
Expand Down Expand Up @@ -195,6 +203,9 @@ export class InferenceGatewayClient {
body: JSON.stringify({
...request,
stream: true,
stream_options: {
include_usage: true,
},
}),
signal: controller.signal,
});
Expand Down
36 changes: 24 additions & 12 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ describe('InferenceGatewayClient', () => {
{ role: MessageRole.system, content: 'You are a helpful assistant' },
{ role: MessageRole.user, content: 'Hello' },
],
stream: false,
};

const mockResponse: SchemaCreateChatCompletionResponse = {
Expand Down Expand Up @@ -152,7 +151,7 @@ describe('InferenceGatewayClient', () => {
'http://localhost:8080/v1/chat/completions',
expect.objectContaining({
method: 'POST',
body: JSON.stringify(mockRequest),
body: JSON.stringify({ ...mockRequest, stream: false }),
})
);
});
Expand All @@ -161,7 +160,6 @@ describe('InferenceGatewayClient', () => {
const mockRequest = {
model: 'claude-3-opus-20240229',
messages: [{ role: MessageRole.user, content: 'Hello' }],
stream: false,
};

const mockResponse: SchemaCreateChatCompletionResponse = {
Expand Down Expand Up @@ -200,7 +198,7 @@ describe('InferenceGatewayClient', () => {
'http://localhost:8080/v1/chat/completions?provider=anthropic',
expect.objectContaining({
method: 'POST',
body: JSON.stringify(mockRequest),
body: JSON.stringify({ ...mockRequest, stream: false }),
})
);
});
Expand All @@ -211,7 +209,6 @@ describe('InferenceGatewayClient', () => {
const mockRequest = {
model: 'gpt-4o',
messages: [{ role: MessageRole.user, content: 'Hello' }],
stream: true,
};

const mockStream = new TransformStream();
Expand Down Expand Up @@ -258,6 +255,9 @@ describe('InferenceGatewayClient', () => {
body: JSON.stringify({
...mockRequest,
stream: true,
stream_options: {
include_usage: true,
},
}),
})
);
Expand All @@ -267,7 +267,6 @@ describe('InferenceGatewayClient', () => {
const mockRequest = {
model: 'gpt-4o',
messages: [{ role: MessageRole.user, content: 'Hello' }],
stream: true,
};
const mockStream = new TransformStream();
const writer = mockStream.writable.getWriter();
Expand Down Expand Up @@ -318,6 +317,9 @@ describe('InferenceGatewayClient', () => {
body: JSON.stringify({
...mockRequest,
stream: true,
stream_options: {
include_usage: true,
},
}),
})
);
Expand All @@ -341,7 +343,6 @@ describe('InferenceGatewayClient', () => {
},
},
],
stream: true,
};

const mockStream = new TransformStream();
Expand Down Expand Up @@ -390,13 +391,25 @@ describe('InferenceGatewayClient', () => {
},
});
expect(callbacks.onFinish).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(
'http://localhost:8080/v1/chat/completions',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({
...mockRequest,
stream: true,
stream_options: {
include_usage: true,
},
}),
})
);
});

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

mockFetch.mockResolvedValueOnce({
Expand All @@ -420,10 +433,6 @@ describe('InferenceGatewayClient', () => {
const mockRequest = {
model: 'gpt-4o',
messages: [{ role: MessageRole.user, content: 'Hello' }],
stream: true,
stream_options: {
include_usage: true,
},
};

const mockStream = new TransformStream();
Expand Down Expand Up @@ -478,6 +487,9 @@ describe('InferenceGatewayClient', () => {
body: JSON.stringify({
...mockRequest,
stream: true,
stream_options: {
include_usage: true,
},
}),
})
);
Expand Down