Skip to content

Commit

Permalink
feat: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaczan committed Feb 26, 2024
1 parent 434ff3a commit 56a6ab4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/types/global.d.ts
src/types/global.d.ts
lib/index.d.ts
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,3 @@ dist
.yarn/install-state.gz
.pnp.*

# Compiled code
lib/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Ryan Sonshine
Copyright (c) 2024 Jędrzej Maczan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 13 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import OpenAI from 'openai';
export declare function poll(
openai: OpenAI,
thread: OpenAI.Beta.Threads.Thread,
run: OpenAI.Beta.Threads.Runs.Run,
options?: PollOptions
): Promise<OpenAI.Beta.Threads.Runs.Run>;
export interface PollOptions {
maxAttempts: number;
intervalInMilliseconds: number;
showLogs: boolean;
}
export declare const defaultPollOptions: PollOptions;
48 changes: 48 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultPollOptions = exports.poll = void 0;
function poll(openai, thread, run, options) {
return __awaiter(this, void 0, void 0, function* () {
const mergedOptions = Object.assign(Object.assign({}, exports.defaultPollOptions), options);
const maxAttempts = mergedOptions.maxAttempts;
const interval = mergedOptions.intervalInMilliseconds;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const retrievedRun = yield openai.beta.threads.runs.retrieve(thread.id, run.id);
mergedOptions.showLogs && console.log(retrievedRun.status);
if (retrievedRun.status === 'completed' ||
retrievedRun.status === 'failed') {
mergedOptions.showLogs &&
console.log(`Run completed with status: ${retrievedRun.status}`);
return retrievedRun;
}
else {
mergedOptions.showLogs &&
console.log(`Attempt ${attempt}: Run status is ${retrievedRun.status}, polling again in ${interval / 1000} seconds...`);
}
}
catch (error) {
mergedOptions.showLogs &&
console.error('Error polling run status:', error);
throw error;
}
yield new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error('Max polling attempts reached');
});
}
exports.poll = poll;
exports.defaultPollOptions = {
maxAttempts: 60,
intervalInMilliseconds: 4000,
showLogs: false,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tmlc/openai-polling",
"version": "0.0.1",
"version": "0.0.2",
"description": "Polling library for OpenAI Threads API",
"main": "./lib/index.js",
"files": [
Expand Down

0 comments on commit 56a6ab4

Please sign in to comment.