From 9c83a0cf4081dccc6d9254eac0dc5310e7175d73 Mon Sep 17 00:00:00 2001 From: Eirik Vullum Date: Wed, 3 May 2023 23:09:08 +0200 Subject: [PATCH 1/5] Conform to OpenAI API spec for prompt completion response --- utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.js b/utils.js index 0151967..214ba89 100644 --- a/utils.js +++ b/utils.js @@ -75,7 +75,7 @@ export const dataToCompletionResponse = ( created: currDate.getTime(), choices: [ { - text: {content: !!data ? unescapeWrongEscapes(data) : ''}, + text: unescapeWrongEscapes(data) || '', finish_reason: reason, index: 0, logprobs: null, From 57ff04bf1ca60f3523022caf64ed63e63b763db7 Mon Sep 17 00:00:00 2001 From: Eirik Vullum Date: Wed, 3 May 2023 23:10:14 +0200 Subject: [PATCH 2/5] Use correct completion response transform function --- routes/completionsRoutes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/completionsRoutes.js b/routes/completionsRoutes.js index 152997d..7e12035 100644 --- a/routes/completionsRoutes.js +++ b/routes/completionsRoutes.js @@ -132,7 +132,7 @@ router.post('/', async (req, res) => { if (responseStart) { process.stdout.write(data); controller.enqueue( - dataToResponse(data, promptTokens, completionTokens, stream) + dataToCompletionResponse(data, promptTokens, completionTokens, stream) ); } else { console.log('===== PROCESSING PROMPT... ====='); From fbb8c35e8b697c4a7ac2b839d3aa146ec356520f Mon Sep 17 00:00:00 2001 From: Eirik Vullum Date: Wed, 3 May 2023 23:10:28 +0200 Subject: [PATCH 3/5] End requests when done --- routes/chatRoutes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/routes/chatRoutes.js b/routes/chatRoutes.js index 17d31e1..a348a33 100644 --- a/routes/chatRoutes.js +++ b/routes/chatRoutes.js @@ -252,6 +252,7 @@ assistant:`; ); res.write('event: data\n'); res.write('data: [DONE]\n\n'); + res.end(); global.lastRequest = { type: 'chat', messages: [ From 44b70b12b414d6b9d35c6ade9317f155b1736a2f Mon Sep 17 00:00:00 2001 From: Eirik Vullum Date: Wed, 3 May 2023 23:11:30 +0200 Subject: [PATCH 4/5] Refer to correct fields in chunk --- routes/completionsRoutes.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routes/completionsRoutes.js b/routes/completionsRoutes.js index 7e12035..4768029 100644 --- a/routes/completionsRoutes.js +++ b/routes/completionsRoutes.js @@ -168,9 +168,9 @@ router.post('/', async (req, res) => { let lastChunk; // in case stop prompts are longer, lets combine the last 2 chunks to check const writable = new WritableStream({ write(chunk) { - const currContent = chunk.choices[0].delta.content; + const currContent = chunk.choices[0].text; const lastContent = !!lastChunk - ? lastChunk.choices[0].delta.content + ? lastChunk.choices[0].text : undefined; const last2Content = !!lastContent ? lastContent + currContent @@ -230,9 +230,9 @@ router.post('/', async (req, res) => { let lastChunk; // in case stop prompts are longer, lets combine the last 2 chunks to check const writable = new WritableStream({ write(chunk) { - const currContent = chunk.choices[0].message.content; + const currContent = chunk.choices[0].text; const lastContent = !!lastChunk - ? lastChunk.choices[0].message.content + ? lastChunk.choices[0].text : undefined; const last2Content = !!lastContent ? lastContent + currContent From 3e2a4bb1e24b21c5dcde68295e267c3e32d4faf8 Mon Sep 17 00:00:00 2001 From: Eirik Vullum Date: Wed, 3 May 2023 23:11:41 +0200 Subject: [PATCH 5/5] End request when done --- routes/completionsRoutes.js | 1 + 1 file changed, 1 insertion(+) diff --git a/routes/completionsRoutes.js b/routes/completionsRoutes.js index 4768029..1146d65 100644 --- a/routes/completionsRoutes.js +++ b/routes/completionsRoutes.js @@ -199,6 +199,7 @@ router.post('/', async (req, res) => { ); res.write('event: data\n'); res.write('data: [DONE]\n\n'); + res.end(); global.lastRequest = { type: 'completion', prompt: prompt,