From 56a6ab406ac73706b6a8ce5e72f96baf8ccf2f8a Mon Sep 17 00:00:00 2001 From: jmaczan Date: Mon, 26 Feb 2024 17:36:43 +0100 Subject: [PATCH] feat: update code --- .eslintignore | 3 ++- .gitignore | 2 -- LICENSE | 2 +- lib/index.d.ts | 13 +++++++++++++ lib/index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 lib/index.d.ts create mode 100644 lib/index.js diff --git a/.eslintignore b/.eslintignore index 2c508eb..18b533f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ -src/types/global.d.ts \ No newline at end of file +src/types/global.d.ts +lib/index.d.ts \ No newline at end of file diff --git a/.gitignore b/.gitignore index d5b3be1..0a22829 100644 --- a/.gitignore +++ b/.gitignore @@ -115,5 +115,3 @@ dist .yarn/install-state.gz .pnp.* -# Compiled code -lib/ diff --git a/LICENSE b/LICENSE index 114cc9f..a624850 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..5a5b8db --- /dev/null +++ b/lib/index.d.ts @@ -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; +export interface PollOptions { + maxAttempts: number; + intervalInMilliseconds: number; + showLogs: boolean; +} +export declare const defaultPollOptions: PollOptions; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..8407dd6 --- /dev/null +++ b/lib/index.js @@ -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, +}; diff --git a/package.json b/package.json index 6292e66..e1f25eb 100644 --- a/package.json +++ b/package.json @@ -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": [