Skip to content

Commit

Permalink
Merge pull request #178 from GoogleCloudPlatform/@invertase/limit-pal…
Browse files Browse the repository at this point in the history
…m-request-size

fix(firestore-palm-chatbot): truncate history if big payload
  • Loading branch information
cabljac authored Aug 31, 2023
2 parents 492fd2f + 504b556 commit 9d599b8
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions firestore-palm-chatbot/functions/src/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Discussion {

// here location is hard-coded, following https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings#generative-ai-get-text-embedding-nodejs
const clientOptions = {
apiEndpoint: `us-central1-aiplatform.googleapis.com`,
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};

this.vertexClient = new v1.PredictionServiceClient(clientOptions);
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Discussion {
{author: '0', content: message},
];

const prompt: PaLMPrompt = {
const prompt: PaLMPrompt = truncatePrompt({
messages,
context:
options.context || this.context || config.provider === 'vertex'
Expand All @@ -122,7 +122,7 @@ export class Discussion {
examples: this.messagesToExamples(
options.examples || this.examples || []
),
};
});

if (config.provider === 'vertex') {
const request = this.createVertexRequest(prompt, options);
Expand Down Expand Up @@ -178,6 +178,7 @@ export class Discussion {
topK: options.topK || this.topK,
candidateCount: options.candidateCount || this.candidateCount,
};

return request;
}

Expand Down Expand Up @@ -283,3 +284,21 @@ export class Discussion {
}));
}
}

// function to truncate payload to an upper limit of bytes (20k but leave some room for other fields and overhead)
function truncatePrompt(prompt: PaLMPrompt, bytes = 19500): PaLMPrompt {
let payloadBytes = Buffer.byteLength(JSON.stringify(prompt), 'utf8');

while (payloadBytes > bytes) {
prompt.messages.shift();
payloadBytes = Buffer.byteLength(JSON.stringify(prompt), 'utf8');
}

if (prompt.messages.length === 0) {
throw new Error(
'Payload size exceeded. This is either because the latest message is too long, or the context/examples you have provided are too long. Please try again with a shorter message, or reconfigure examples/context.'
);
}

return prompt;
}

0 comments on commit 9d599b8

Please sign in to comment.