Skip to content

Commit 4c0e0b9

Browse files
committed
feat: text summarization sample for Vertex LLMs
1 parent f3b0100 commit 4c0e0b9

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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_summarization]
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+
28+
const aiplatform = require('@google-cloud/aiplatform');
29+
30+
// Imports the Google Cloud Prediction service client
31+
const {PredictionServiceClient} = aiplatform.v1;
32+
33+
// Import the helper module for converting arbitrary protobuf.Value objects.
34+
const {helpers} = aiplatform;
35+
36+
// Specifies the location of the api endpoint
37+
const clientOptions = {
38+
apiEndpoint: 'us-central1-aiplatform.googleapis.com',
39+
};
40+
41+
const publisher = 'google';
42+
const model = 'text-bison@001';
43+
44+
// Instantiates a client
45+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
46+
47+
async function callPredict() {
48+
// Configure the parent resource
49+
const endpoint = `projects/${project}/locations/${location}/publishers/${publisher}/models/${model}`;
50+
51+
const instance = {
52+
content: `Provide a summary with about two sentences for the following article:
53+
The efficient-market hypothesis (EMH) is a hypothesis in financial \
54+
economics that states that asset prices reflect all available \
55+
information. A direct implication is that it is impossible to \
56+
"beat the market" consistently on a risk-adjusted basis since market \
57+
prices should only react to new information. Because the EMH is \
58+
formulated in terms of risk adjustment, it only makes testable \
59+
predictions when coupled with a particular model of risk. As a \
60+
result, research in financial economics since at least the 1990s has \
61+
focused on market anomalies, that is, deviations from specific \
62+
models of risk. The idea that financial market returns are difficult \
63+
to predict goes back to Bachelier, Mandelbrot, and Samuelson, but \
64+
is closely associated with Eugene Fama, in part due to his \
65+
influential 1970 review of the theoretical and empirical research. \
66+
The EMH provides the basic logic for modern risk-based theories of \
67+
asset prices, and frameworks such as consumption-based asset pricing \
68+
and intermediary asset pricing can be thought of as the combination \
69+
of a model of risk with the EMH. Many decades of empirical research \
70+
on return predictability has found mixed evidence. Research in the \
71+
1950s and 1960s often found a lack of predictability (e.g. Ball and \
72+
Brown 1968; Fama, Fisher, Jensen, and Roll 1969), yet the \
73+
1980s-2000s saw an explosion of discovered return predictors (e.g. \
74+
Rosenberg, Reid, and Lanstein 1985; Campbell and Shiller 1988; \
75+
Jegadeesh and Titman 1993). Since the 2010s, studies have often \
76+
found that return predictability has become more elusive, as \
77+
predictability fails to work out-of-sample (Goyal and Welch 2008), \
78+
or has been weakened by advances in trading technology and investor \
79+
learning (Chordia, Subrahmanyam, and Tong 2014; McLean and Pontiff \
80+
2016; Martineau 2021).
81+
Summary:
82+
`,
83+
};
84+
const instanceValue = helpers.toValue(instance);
85+
const instances = [instanceValue];
86+
87+
const parameter = {
88+
temperature: 0.2,
89+
maxOutputTokens: 256,
90+
topP: 0.95,
91+
topK: 40,
92+
};
93+
const parameters = helpers.toValue(parameter);
94+
95+
const request = {
96+
endpoint,
97+
instances,
98+
parameters,
99+
};
100+
101+
// Predict request
102+
const [response] = await predictionServiceClient.predict(request);
103+
console.log('Get text summarization response');
104+
const predictions = response.predictions;
105+
console.log('\tPredictions :');
106+
for (const prediction of predictions) {
107+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
108+
}
109+
}
110+
111+
callPredict();
112+
// [END aiplatform_sdk_summarization]
113+
}
114+
115+
process.on('unhandledRejection', err => {
116+
console.error(err.message);
117+
process.exitCode = 1;
118+
});
119+
120+
main(...process.argv.slice(2));
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 summarization', () => {
31+
it('should make predictions using a large language model', async () => {
32+
const stdout = execSync(
33+
`node ./predict-text-summarization.js ${project} ${location}`,
34+
{
35+
cwd,
36+
}
37+
);
38+
assert.match(stdout, /Get text summarization response/);
39+
});
40+
});

0 commit comments

Comments
 (0)