Skip to content

Commit

Permalink
Fix: patch streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Graden Rea authored and stainless-app[bot] committed May 23, 2024
1 parent ebfa13b commit 259791c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 105 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
98 changes: 0 additions & 98 deletions src/lib/chat_completions_ext.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/lib/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ReadableStream, type Response } from 'groq-sdk/_shims/index';
import { GroqError } from 'groq-sdk/error';
import { ReadableStream, type Response } from '../_shims/index';
import { GroqError } from '../error';

import { APIError } from 'groq-sdk/error';
import { APIError } from '../error';

type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;

Expand Down
52 changes: 48 additions & 4 deletions src/resources/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@ import { APIResource } from '../../resource';
import * as ChatCompletionsAPI from './completions';
import * as CompletionsAPI from '../completions';
import * as Shared from '../shared';
import { Stream } from '../../lib/streaming';

export class Completions extends APIResource {
/**
* Creates a model response for the given chat conversation.
*/
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 @@ -205,7 +225,7 @@ export namespace ChatCompletionChunk {
* number of tokens specified in the request was reached, `tool_calls` if the model
* called a tool, or `function_call` (deprecated) if the model called a function.
*/
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call' | null;
finish_reason: 'stop' | 'length' | 'tool_calls' | 'function_call' | null;

/**
* The index of the choice in the list of choices.
Expand Down Expand Up @@ -640,7 +660,11 @@ export interface ChatCompletionUserMessageParam {
name?: string;
}

export interface CompletionCreateParams {
export type ChatCompletionCreateParams =
| ChatCompletionCreateParamsNonStreaming
| ChatCompletionCreateParamsStreaming;

export interface ChatCompletionCreateParamsBase {
/**
* A list of messages comprising the conversation so far.
*/
Expand Down Expand Up @@ -843,6 +867,26 @@ export namespace CompletionCreateParams {
}
}

export interface ChatCompletionCreateParamsNonStreaming extends ChatCompletionCreateParamsBase {
/**
* If set, partial message deltas will be sent. Tokens will be sent as data-only
* [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
* as they become available, with the stream terminated by a `data: [DONE]`
* message. [Example code](/docs/text-chat#streaming-a-chat-completion).
*/
stream?: false | null;
}

export interface ChatCompletionCreateParamsStreaming extends ChatCompletionCreateParamsBase {
/**
* If set, partial message deltas will be sent. Tokens will be sent as data-only
* [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
* as they become available, with the stream terminated by a `data: [DONE]`
* message. [Example code](/docs/text-chat#streaming-a-chat-completion).
*/
stream: true;
}

export namespace Completions {
export import ChatCompletion = ChatCompletionsAPI.ChatCompletion;
export import ChatCompletionAssistantMessageParam = ChatCompletionsAPI.ChatCompletionAssistantMessageParam;
Expand Down

0 comments on commit 259791c

Please sign in to comment.