Skip to content

Commit

Permalink
Added escape key to stop & Improved code strucure
Browse files Browse the repository at this point in the history
  • Loading branch information
thelastmayday committed Feb 8, 2024
1 parent c3c287d commit c12511b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"eslint-plugin-unused-imports": "^2.0.0",
"jiti": "^1.17.0",
"pkgroll": "^1.9.0",
"prettier": "^2.8.7",
"prettier": "^2.8.8",
"typescript": "^4.9.5"
}
}
60 changes: 25 additions & 35 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 @@ -74,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 @@ -83,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 @@ -114,7 +114,7 @@ export async function generateCompletion({
` +
'\n\n' +
messageString +
'\n',
'\n'
);
} else if (response && message) {
throw new KnownError(
Expand All @@ -123,7 +123,7 @@ export async function generateCompletion({
` +
'\n\n' +
messageString +
'\n',
'\n'
);
}

Expand Down Expand Up @@ -188,74 +188,64 @@ export const readData =
) =>
(writer: (data: string) => void): Promise<string> =>
new Promise(async (resolve) => {
let stopTextStream = false;
let data = '';
let content = '';
let dataStart = false;
let buffer = '';
const [excludedPrefix] = excluded;
const stopTextStreamKeys = ['q', 'escape'];

const rl = readline.createInterface({
input: process.stdin,
});

rl.input.setRawMode(true);
process.stdin.setRawMode(true);

rl.input.on('keypress', (key) => {
if (typeof key === 'string' && key.toLowerCase().includes('q')) {
stopWriting = true;
process.stdin.on('keypress', (key, data) => {
if (stopTextStreamKeys.includes(data.name)) {
stopTextStream = true;
}
});

let stopWriting = false;
let data = '';
let content = '';
let dataStart = false;
// This buffer will temporarily hold incoming data only for detecting the start
let buffer = '';

const [excludedPrefix] = excluded;

for await (const chunk of iterableStream) {
const payloads = chunk.toString().split('\n\n');

for (const payload of payloads) {
if (payload.includes('[DONE]' || stopWriting)) {
if (payload.includes('[DONE]') || stopTextStream) {
dataStart = false;
resolve(data);
return;
}

if (payload.startsWith('data:')) {
content = parseContent(payload);
// Use buffer only for start detection
if (!dataStart) {
// Append content to the buffer
buffer += content;
if (buffer.match(excludedPrefix ?? '')) {
dataStart = true;
// Clear the buffer once it has served its purpose
buffer = '';
if (excludedPrefix) break;
}
}

if (dataStart && content) {
const contentWithoutExcluded = stripRegexPatterns(
content,
excluded,
excluded
);

data += contentWithoutExcluded;
writer(contentWithoutExcluded);
}
}
}
}

function parseContent(payload: string): string {
const data = payload.replaceAll(/(\n)?^data:\s*/g, '');
function parseContent(payload) {
const data2 = payload.replaceAll(/(\n)?^data:\s*/g, '');
try {
const delta = JSON.parse(data.trim());
const delta = JSON.parse(data2.trim());
return delta.choices?.[0].delta?.content ?? '';
} catch (error) {
return `Error with JSON.parse and ${payload}.\n${error}`;
return `Error with JSON.parse and ${payload}.
${error}`;
}
}

resolve(data);
});

Expand Down

0 comments on commit c12511b

Please sign in to comment.