Skip to content

Commit 55f52cb

Browse files
authored
Merge pull request #3261 from GoogleCloudPlatform/b279595025-codefunction
feat(aiplatform): code generation function sample for Vertex LLMs
2 parents a9c82de + 0a43221 commit 55f52cb

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(project, location = 'us-central1') {
20+
// [START aiplatform_sdk_code_generation_function]
21+
/**
22+
* TODO(developer): Uncomment these variables before running the sample.\
23+
* (Not necessary if passing values as arguments)
24+
*/
25+
// const project = 'YOUR_PROJECT_ID';
26+
// const location = 'YOUR_PROJECT_LOCATION';
27+
const aiplatform = require('@google-cloud/aiplatform');
28+
29+
// Imports the Google Cloud Prediction service client
30+
const {PredictionServiceClient} = aiplatform.v1;
31+
32+
// Import the helper module for converting arbitrary protobuf.Value objects.
33+
const {helpers} = aiplatform;
34+
35+
// Specifies the location of the api endpoint
36+
const clientOptions = {
37+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
38+
};
39+
const publisher = 'google';
40+
const model = 'code-bison@001';
41+
42+
// Instantiates a client
43+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
44+
45+
async function callPredict() {
46+
// Configure the parent resource
47+
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
48+
49+
const prompt = {
50+
prefix: 'Write a function that checks if a year is a leap year.',
51+
};
52+
const instanceValue = helpers.toValue(prompt);
53+
const instances = [instanceValue];
54+
55+
const parameter = {
56+
temperature: 0.2,
57+
maxOutputTokens: 256,
58+
};
59+
const parameters = helpers.toValue(parameter);
60+
61+
const request = {
62+
endpoint,
63+
instances,
64+
parameters,
65+
};
66+
67+
// Predict request
68+
const [response] = await predictionServiceClient.predict(request);
69+
console.log('Get code generation response');
70+
const predictions = response.predictions;
71+
console.log('\tPredictions :');
72+
for (const prediction of predictions) {
73+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
74+
}
75+
}
76+
77+
callPredict();
78+
// [END aiplatform_sdk_code_generation_function]
79+
}
80+
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
86+
main(...process.argv.slice(2));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const {assert} = require('chai');
21+
const {describe, it} = require('mocha');
22+
23+
const cp = require('child_process');
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
const cwd = path.join(__dirname, '..');
26+
27+
const project = process.env.CAIP_PROJECT_ID;
28+
const location = 'us-central1';
29+
30+
describe('AI platform predict code generation', () => {
31+
it('should make predictions using a large language model', async () => {
32+
const stdout = execSync(
33+
`node ./predict-code-generation-function.js ${project} ${location}`,
34+
{
35+
cwd,
36+
}
37+
);
38+
assert.match(stdout, /Get code generation response/);
39+
});
40+
});

0 commit comments

Comments
 (0)