Skip to content

Commit f3b0100

Browse files
authored
Merge pull request #3245 from GoogleCloudPlatform/b281562687-0
feat(aiplatform): text prompt sample for LLMs
2 parents 0fa9c61 + f69415e commit f3b0100

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
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_ideation]
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+
40+
const publisher = 'google';
41+
const model = 'text-bison@001';
42+
43+
// Instantiates a client
44+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
45+
46+
async function callPredict() {
47+
// Configure the parent resource
48+
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
49+
50+
const prompt = {
51+
prompt:
52+
'Give me ten interview questions for the role of program manager.',
53+
};
54+
const instanceValue = helpers.toValue(prompt);
55+
const instances = [instanceValue];
56+
57+
const parameter = {
58+
temperature: 0.2,
59+
maxOutputTokens: 5,
60+
topP: 0.95,
61+
topK: 40,
62+
};
63+
const parameters = helpers.toValue(parameter);
64+
65+
const request = {
66+
endpoint,
67+
instances,
68+
parameters,
69+
};
70+
71+
// Predict request
72+
const response = await predictionServiceClient.predict(request);
73+
console.log('Get text prompt response');
74+
console.log(response);
75+
}
76+
77+
callPredict();
78+
// [END aiplatform_sdk_ideation]
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 text prompt', () => {
31+
it('should make predictions using a large language model', async () => {
32+
const stdout = execSync(
33+
`node ./predict-text-prompt.js ${project} ${location}`,
34+
{
35+
cwd,
36+
}
37+
);
38+
assert.match(stdout, /Get text prompt response/);
39+
});
40+
});

0 commit comments

Comments
 (0)