Skip to content

Commit

Permalink
Merge pull request alibaba#15 from nauxliu/fix-converstaion-context
Browse files Browse the repository at this point in the history
Correct the improper context being used when it exceeds the limit.
  • Loading branch information
mckaywrigley committed Mar 19, 2023
2 parents dab7627 + 7c9e552 commit 240185c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pages/api/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const handler = async (req: Request): Promise<Response> => {

const charLimit = 12000;
let charCount = 0;
let messagesToSend = [];
let messagesToSend: Message[] = [];

for (let i = 0; i < messages.length; i++) {
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i];
if (charCount + message.content.length > charLimit) {
break;
}
charCount += message.content.length;
messagesToSend.push(message);
messagesToSend = [message, ...messagesToSend]
}

const stream = await OpenAIStream(model, key, messagesToSend);
Expand Down

0 comments on commit 240185c

Please sign in to comment.