in firestore-palm-chatbot/functions/src/discussion.ts [222:256]
private async generateMessageVertex(
request: VertexPredictRequest
): Promise<GenerateMessageResponse> {
if (!this.vertexClient) {
throw new Error('Vertex client not initialized.');
}
const [result] = await this.vertexClient.predict(request);
const prediction = result.predictions![0];
const value = helpers.fromValue(prediction as protobuf.common.IValue) as {
candidates: APIMessage[];
};
if (!value.candidates || !value.candidates.length) {
throw new Error('No candidates returned from server.');
}
const content = value.candidates[0].content;
if (!content && content !== '') {
throw new Error('No content returned in candidate.');
}
const candidates = value.candidates.map(c => c.content!) || [];
const messages = [] as APIMessage[];
return {
response: content,
candidates,
history: this.messagesFromApi(messages),
};
}