Skip to content

Commit

Permalink
Merge pull request #142 from Portkey-AI/feat/stream-runs
Browse files Browse the repository at this point in the history
feat: create and run stream support
  • Loading branch information
csgulati09 authored Nov 26, 2024
2 parents ee26c54 + ec8fcd5 commit 8c53d13
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
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

0 comments on commit 8c53d13

Please sign in to comment.