-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
bugSomething isn't workingSomething isn't workingfixed in v4Issues addressed by v4Issues addressed by v4
Description
Describe the bug
Sending an 'abort' signal to the createChatCompletion
does not raise an error nor stop the completion.
It makes me believe that this discussion on the openai community is true https://community.openai.com/t/cancelling-openai-apis-request/99754, but I would like to verify it isn't a bug in this library.
To Reproduce
Here's my code
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// instantiate the abortController
const abortController = new AbortController();
const abortSignal = abortController.signal;
// send the request, passing in the abortController's signal
const response = await openai.createChatCompletion(
{
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: "write a 1000-word essay about George Washington",
},
],
stream: true,
},
{
// This is an Axios request config object
responseType: "stream",
signal: abortSignal,
}
);
// abort the request in 1 second
const abort = () => {
abortController.abort();
};
setTimeout(abort, 1000);
// read the response data
const dataStream = response.data as unknown as AsyncIterable<Buffer>;
for await (const chunk of dataStream) {
if (abortSignal.aborted) {
console.log("aborted");
}
const lines = chunk
.toString("utf8")
.split("\n")
.filter((line) => line.trim().startsWith("data: "));
for (const line of lines) {
const message = line.replace("data: ", "");
if (message === "[DONE]") {
console.log("done");
}
const json = JSON.parse(message);
const token = json.choices[0]?.delta?.content;
if (token != null) {
console.log(`token: ${token}`);
}
}
}
Expectation: I should see output like this, and then an error should be raised:
token: George
token: Washington
token: is
token: perhaps
token: the
token: most
token: significant
token: figure
token: in
token: American
token: history
token: .
aborted
Actual: I see output like this that never stops:
token: George
token: Washington
token: is
token: perhaps
token: the
token: most
token: significant
token: figure
token: in
token: American
token: history
token: .
aborted
token: He
aborted
token: was
aborted
token: a
aborted
token: man
aborted
token: who
aborted
token: rose
aborted
token: from
aborted
token: humble
aborted
token: beginnings
aborted
token: to
aborted
token: become
aborted
token: the
aborted
token: first
aborted
token: President
aborted
token: of
aborted
token: the
aborted
token: United
aborted
token: States
aborted
token: of
aborted
token: America
aborted
token: ,
aborted
token: and
aborted
token: he
aborted
token: did
aborted
token: so
aborted
token: with
aborted
token: a
aborted
token: level
... (and so on)
Code snippets
No response
OS
macOS
Node version
v19.8.1
Library version
openai v3.2.1
numeralz, jgeggatt, jongmoon, goa, snlamm and 2 more
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingfixed in v4Issues addressed by v4Issues addressed by v4