Skip to content

Commit

Permalink
Fix streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Graden Rea authored and stainless-app[bot] committed Mar 1, 2024
1 parent 0a11e67 commit 4d6e95f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { VERSION } from './version';
import { Stream } from './lib/streaming';
import {
GroqError,
APIError,
Expand Down Expand Up @@ -38,6 +39,19 @@ type APIResponseProps = {

async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
const { response } = props;
if (props.options.stream) {
debug('response', response.status, response.url, response.headers, response.body);

// Note: there is an invariant here that isn't represented in the type system
// that if you set `stream: true` the response type must also be `Stream<T>`

if (props.options.__streamClass) {
return props.options.__streamClass.fromSSEResponse(response, props.controller) as any;
}

return Stream.fromSSEResponse(response, props.controller) as any;
}

// fetch refuses to read the body when the status code is 204.
if (response.status === 204) {
return null as T;
Expand Down Expand Up @@ -736,6 +750,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
idempotencyKey?: string;

__binaryResponse?: boolean | undefined;
__streamClass?: typeof Stream;
};

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

__binaryResponse: true,
__streamClass: true,
};

export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
Expand Down
37 changes: 34 additions & 3 deletions src/resources/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
import * as Core from 'groq-sdk/core';
import { APIResource } from 'groq-sdk/resource';
import * as CompletionsAPI from 'groq-sdk/resources/chat/completions';
import { Stream } from 'groq-sdk/lib/streaming';
import { ChatCompletionChunk } from 'groq-sdk/lib/chat_completions_ext';

export class Completions extends APIResource {
/**
* Creates a completion for a chat prompt
*/
create(body: CompletionCreateParams, options?: Core.RequestOptions): Core.APIPromise<ChatCompletion> {
return this._client.post('/openai/v1/chat/completions', { body, ...options });
create(
body: ChatCompletionCreateParamsNonStreaming,
options?: Core.RequestOptions,
): Core.APIPromise<ChatCompletion>;
create(
body: ChatCompletionCreateParamsStreaming,
options?: Core.RequestOptions,
): Core.APIPromise<Stream<ChatCompletionChunk>>;
create(
body: ChatCompletionCreateParamsBase,
options?: Core.RequestOptions,
): Core.APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>;
create(
body: ChatCompletionCreateParams,
options?: Core.RequestOptions,
): Core.APIPromise<ChatCompletion> | Core.APIPromise<Stream<ChatCompletionChunk>> {
return this._client.post('/openai/v1/chat/completions', { body, ...options, stream: body.stream ?? false }) as
| Core.APIPromise<ChatCompletion>
| Core.APIPromise<Stream<ChatCompletionChunk>>;
}
}

Expand Down Expand Up @@ -111,7 +130,7 @@ export namespace ChatCompletion {
}
}

export interface CompletionCreateParams {
export interface ChatCompletionCreateParamsBase {
messages: Array<CompletionCreateParams.Message>;

model: string;
Expand Down Expand Up @@ -235,3 +254,15 @@ export namespace Completions {
export import ChatCompletion = CompletionsAPI.ChatCompletion;
export import CompletionCreateParams = CompletionsAPI.CompletionCreateParams;
}

export interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase {
stream?: false;
}

export interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase {
stream: true;
}

export type ChatCompletionCreateParams =
| ChatCompletionCreateParamsNonStreaming
| ChatCompletionCreateParamsStreaming;

0 comments on commit 4d6e95f

Please sign in to comment.