Skip to content

Commit

Permalink
fix: stream case + linting
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Nov 26, 2024
1 parent b8afd1c commit ec8fcd5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
48 changes: 19 additions & 29 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 { EventEmitter } from 'events';
import { finalResponse, initOpenAIClient, overrideConfig } from '../utils';
import { createHeaders } from './createHeaders';

Expand Down Expand Up @@ -114,7 +113,7 @@ export class Threads extends ApiResource {
): Promise<any> {
const body: ThreadCreateAndRunParams = _body;
const { stream } = body;

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

const OAIclient = initOpenAIClient(this.client);

if(stream === true) {
const eventEmitter = new EventEmitter();

(async ()=> {
const streamResponse = await OAIclient.beta.threads.createAndRun(body, opts)
for await (const chunk of streamResponse as AsyncIterable<any>) {
eventEmitter.emit('data', chunk);
}
eventEmitter.emit('end');
})();
return eventEmitter;
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);
}
}
Expand Down Expand Up @@ -299,7 +293,7 @@ export class Runs extends ApiResource {
_body: RunCreateParams,
params?: ApiClientInterface,
opts?: RequestOptions
): Promise<any>{
): Promise<any> {
const body: RunCreateParams = _body;
const { stream } = body;
if (params) {
Expand All @@ -309,25 +303,21 @@ export class Runs extends ApiResource {
...createHeaders({ ...params, config }),
};
}

const OAIclient = initOpenAIClient(this.client);

if(stream === true) {
const eventEmitter = new EventEmitter();

(async ()=> {
const streamResponse = await OAIclient.beta.threads.runs.create(threadId, body, opts)
for await (const chunk of streamResponse as AsyncIterable<any>) {
eventEmitter.emit('data', chunk);
}
eventEmitter.emit('end');
})();
return eventEmitter;

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);
}
}
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 ec8fcd5

Please sign in to comment.