Skip to content
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

feat: create and run stream support #142

Merged
merged 2 commits into from
Nov 26, 2024
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
43 changes: 32 additions & 11 deletions src/apis/threads.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApiClientInterface } from '../_types/generalTypes';
import { ApiResource } from '../apiResource';
import { RequestOptions } from '../baseClient';

import { finalResponse, initOpenAIClient, overrideConfig } from '../utils';
import { createHeaders } from './createHeaders';

Expand Down Expand Up @@ -113,6 +112,8 @@ export class Threads extends ApiResource {
opts?: RequestOptions
): Promise<any> {
const body: ThreadCreateAndRunParams = _body;
const { stream } = body;

if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
Expand All @@ -123,11 +124,19 @@ export class Threads extends ApiResource {

const OAIclient = initOpenAIClient(this.client);

const result = await OAIclient.beta.threads
.createAndRun(body, opts)
.withResponse();

return finalResponse(result);
if (stream === true) {
const streamResponse = await OAIclient.beta.threads.createAndRunStream(
body as any,
opts
);
return streamResponse;
} else {
const result = await OAIclient.beta.threads
.createAndRun(body, opts)
.withResponse();

return finalResponse(result);
}
}

async createAndRunPoll(
Expand Down Expand Up @@ -286,6 +295,7 @@ export class Runs extends ApiResource {
opts?: RequestOptions
): Promise<any> {
const body: RunCreateParams = _body;
const { stream } = body;
if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
Expand All @@ -296,11 +306,20 @@ export class Runs extends ApiResource {

const OAIclient = initOpenAIClient(this.client);

const result = await OAIclient.beta.threads.runs
.create(threadId, body, opts)
.withResponse();

return finalResponse(result);
if (stream === true) {
const streamResponse = await OAIclient.beta.threads.runs.stream(
threadId,
body as any,
opts
);
return streamResponse;
} else {
const result = await OAIclient.beta.threads.runs
.create(threadId, body, opts)
.withResponse();

return finalResponse(result);
}
}

async list(
Expand Down Expand Up @@ -685,6 +704,7 @@ export interface RunCreateParams {
metadata?: unknown | null;
model?: string | null;
tools?: Array<any> | null;
stream?: boolean | null;
}

export interface RunCreateParamsNonStreaming extends RunCreateParams {
Expand All @@ -698,6 +718,7 @@ export interface ThreadCreateAndRunParams {
model?: string | null;
thread?: any;
tools?: Array<any> | null;
stream?: boolean | null;
}

export interface ThreadCreateAndRunParamsNonStreaming
Expand Down
2 changes: 1 addition & 1 deletion src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
if (contentType?.includes('application/json')) {
const headers = defaultParseHeaders(props);
const json = {
...(await response.json() as any),
...((await response.json()) as any),
getHeaders: () => headers,
};

Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export function toQueryParams(
| VirtualKeysListParams
| ApiKeysListParams
| CongfigsListParams
| LogsExportListParams|any
| LogsExportListParams
| any
): string {
if (!params) {
return '';
Expand Down