@ovotech/genesys-web-messaging-tester-cli
Version:
86 lines (85 loc) • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createChatCompletionClient = void 0;
const aiplatform_1 = require("@google-cloud/aiplatform");
const { PredictionServiceClient } = aiplatform_1.v1;
function createChatCompletionClient({ location, project, temperature, topK, topP, seed, modelVersion, examples, }) {
const version = modelVersion ? `@${modelVersion}` : '';
const endpoint = `projects/${project}/locations/${location}/publishers/google/models/chat-bison${version}`;
const predictionServiceClient = new PredictionServiceClient({
apiEndpoint: `${location}-aiplatform.googleapis.com`,
});
const parameters = aiplatform_1.helpers.toValue({
...(temperature ? { temperature } : {}),
...(topK ? { topK } : {}),
...(topP ? { topP } : {}),
...(seed ? { seed } : {}),
});
return {
getProviderName() {
return 'Google Vertex AI';
},
async preflightCheck() {
const prompt = {
context: 'You help people with math problems',
messages: [{ author: 'student', content: 'What is 1+1?' }],
};
const instanceValue = aiplatform_1.helpers.toValue(prompt);
const request = {
endpoint,
instances: [instanceValue],
parameters,
};
try {
await predictionServiceClient.predict(request);
return { ok: true };
}
catch (error) {
return {
ok: false,
reasonForError: error instanceof Error ? error.message : String(error),
};
}
},
async predict(context, conversationUtterances) {
const prompt = {
context,
...(examples
? {
examples: examples.map(({ input, output }) => ({
input: { content: input },
output: { content: output },
})),
}
: {}),
messages: [
// Google requires at least one message. This message is hopefully innocuous enough not to lead to an unexpected result.
{ content: '...', author: 'bot' },
...conversationUtterances.map((u) => ({
author: u.role,
content: u.content,
})),
],
};
const instanceValue = aiplatform_1.helpers.toValue(prompt);
const request = {
endpoint,
instances: [instanceValue],
parameters,
};
const [response] = await predictionServiceClient.predict(request);
for (const prediction of response?.predictions || []) {
const candidates = prediction.structValue?.fields?.candidates;
for (const candidate of candidates?.listValue?.values || []) {
const content = candidate.structValue?.fields?.content?.stringValue;
// const author = candidate.structValue?.fields?.author?.stringValue;
if (content) {
return { content: content.trim(), role: 'customer' };
}
}
}
return null;
},
};
}
exports.createChatCompletionClient = createChatCompletionClient;