Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stop feature #103

Merged
merged 4 commits into from
Feb 9, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { streamToString } from './stream-to-string';
import './replace-all-polyfill';
import i18n from './i18n';
import { stripRegexPatterns } from './strip-regex-patterns';
import readline from 'readline';

const explainInSecondRequest = true;

function getOpenAi(key: string, apiEndpoint: string) {
const openAi = new OpenAIApi(
new Configuration({ apiKey: key, basePath: apiEndpoint })
new Configuration({ apiKey: key, basePath: apiEndpoint }),
);
return openAi;
}
Expand Down Expand Up @@ -73,7 +74,7 @@ export async function generateCompletion({
n: Math.min(number, 10),
stream: true,
},
{ responseType: 'stream' }
{ responseType: 'stream' },
);

return completion.data as unknown as IncomingMessage;
Expand All @@ -82,15 +83,15 @@ export async function generateCompletion({

if (error.code === 'ENOTFOUND') {
throw new KnownError(
`Error connecting to ${error.request.hostname} (${error.request.syscall}). Are you connected to the internet?`
`Error connecting to ${error.request.hostname} (${error.request.syscall}). Are you connected to the internet?`,
);
}

const response = error.response;
let message = response?.data as string | object | IncomingMessage;
if (response && message instanceof IncomingMessage) {
message = await streamToString(
response.data as unknown as IncomingMessage
response.data as unknown as IncomingMessage,
);
try {
// Handle if the message is JSON. It should be but occasionally will
Expand All @@ -113,7 +114,7 @@ export async function generateCompletion({
` +
'\n\n' +
messageString +
'\n'
'\n',
);
} else if (response && message) {
throw new KnownError(
Expand All @@ -122,7 +123,7 @@ export async function generateCompletion({
` +
'\n\n' +
messageString +
'\n'
'\n',
);
}

Expand Down Expand Up @@ -187,6 +188,19 @@ export const readData =
) =>
(writer: (data: string) => void): Promise<string> =>
new Promise(async (resolve) => {
const rl = readline.createInterface({
input: process.stdin,
});

rl.input.setRawMode(true);

rl.input.on('keypress', (key) => {
if (typeof key === 'string' && key.toLowerCase().includes('q')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a great idea. can/should we support the esc key as well? or is that not possible. i'm guessing most users will intuitively hit esc when wanting to stop the stream, so if that works as well it could be a nice addition

stopWriting = true;
}
});

let stopWriting = false;
let data = '';
let content = '';
let dataStart = false;
Expand All @@ -199,7 +213,7 @@ export const readData =
const payloads = chunk.toString().split('\n\n');

for (const payload of payloads) {
if (payload.includes('[DONE]')) {
if (payload.includes('[DONE]' || stopWriting)) {
dataStart = false;
resolve(data);
return;
Expand All @@ -222,7 +236,7 @@ export const readData =
if (dataStart && content) {
const contentWithoutExcluded = stripRegexPatterns(
content,
excluded
excluded,
);

data += contentWithoutExcluded;
Expand Down
Loading