Skip to content

Commit 9d363e0

Browse files
Graden ReaGraden Rea
Graden Rea
authored and
Graden Rea
committed
Fix streaming
1 parent 3c37331 commit 9d363e0

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/core.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { VERSION } from './version';
2+
import { Stream } from './lib/streaming';
23
import {
34
GroqError,
45
APIError,
@@ -38,6 +39,19 @@ type APIResponseProps = {
3839

3940
async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
4041
const { response } = props;
42+
if (props.options.stream) {
43+
debug('response', response.status, response.url, response.headers, response.body);
44+
45+
// Note: there is an invariant here that isn't represented in the type system
46+
// that if you set `stream: true` the response type must also be `Stream<T>`
47+
48+
if (props.options.__streamClass) {
49+
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
50+
}
51+
52+
return Stream.fromSSEResponse(response, props.controller) as any;
53+
}
54+
4155
// fetch refuses to read the body when the status code is 204.
4256
if (response.status === 204) {
4357
return null as T;
@@ -736,6 +750,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
736750
idempotencyKey?: string;
737751

738752
__binaryResponse?: boolean | undefined;
753+
__streamClass?: typeof Stream;
739754
};
740755

741756
// This is required so that we can determine if a given object matches the RequestOptions
@@ -756,6 +771,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
756771
idempotencyKey: true,
757772

758773
__binaryResponse: true,
774+
__streamClass: true,
759775
};
760776

761777
export const isRequestOptions = (obj: unknown): obj is RequestOptions => {

src/resources/chat/completions.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@
33
import * as Core from 'groq-sdk/core';
44
import { APIResource } from 'groq-sdk/resource';
55
import * as CompletionsAPI from 'groq-sdk/resources/chat/completions';
6+
import { Stream } from 'groq-sdk/lib/streaming';
7+
import { ChatCompletionChunk } from 'groq-sdk/lib/chat_completions_ext';
68

79
export class Completions extends APIResource {
810
/**
911
* Creates a completion for a chat prompt
1012
*/
11-
create(body: CompletionCreateParams, options?: Core.RequestOptions): Core.APIPromise<ChatCompletion> {
12-
return this._client.post('/openai/v1/chat/completions', { body, ...options });
13+
create(
14+
body: ChatCompletionCreateParamsNonStreaming,
15+
options?: Core.RequestOptions,
16+
): Core.APIPromise<ChatCompletion>;
17+
create(
18+
body: ChatCompletionCreateParamsStreaming,
19+
options?: Core.RequestOptions,
20+
): Core.APIPromise<Stream<ChatCompletionChunk>>;
21+
create(
22+
body: ChatCompletionCreateParamsBase,
23+
options?: Core.RequestOptions,
24+
): Core.APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
25+
create(
26+
body: ChatCompletionCreateParams,
27+
options?: Core.RequestOptions,
28+
): Core.APIPromise<ChatCompletion> | Core.APIPromise<Stream<ChatCompletionChunk>> {
29+
return this._client.post('/openai/v1/chat/completions', { body, ...options, stream: body.stream ?? false }) as
30+
| Core.APIPromise<ChatCompletion>
31+
| Core.APIPromise<Stream<ChatCompletionChunk>>;
1332
}
1433
}
1534

@@ -109,7 +128,7 @@ export namespace ChatCompletion {
109128
}
110129
}
111130

112-
export interface CompletionCreateParams {
131+
export interface ChatCompletionCreateParamsBase {
113132
messages: Array<CompletionCreateParams.Message>;
114133

115134
model: string;
@@ -233,3 +252,15 @@ export namespace Completions {
233252
export import ChatCompletion = CompletionsAPI.ChatCompletion;
234253
export import CompletionCreateParams = CompletionsAPI.CompletionCreateParams;
235254
}
255+
256+
export interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase {
257+
stream?: false;
258+
}
259+
260+
export interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase {
261+
stream: true;
262+
}
263+
264+
export type ChatCompletionCreateParams =
265+
| ChatCompletionCreateParamsNonStreaming
266+
| ChatCompletionCreateParamsStreaming;

0 commit comments

Comments
 (0)