Skip to content

Commit 449e0f6

Browse files
authored
Merge branch 'main' into b281562687-classify
2 parents 28b1b45 + 35ec0a3 commit 449e0f6

4 files changed

Lines changed: 324 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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_extraction]
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 instance = {
51+
content: `Background: There is evidence that there have been significant changes \
52+
in Amazon rainforest vegetation over the last 21,000 years through the Last \
53+
Glacial Maximum (LGM) and subsequent deglaciation. Analyses of sediment \
54+
deposits from Amazon basin paleo lakes and from the Amazon Fan indicate that \
55+
rainfall in the basin during the LGM was lower than for the present, and this \
56+
was almost certainly associated with reduced moist tropical vegetation cover \
57+
in the basin. There is debate, however, over how extensive this reduction \
58+
was. Some scientists argue that the rainforest was reduced to small, isolated \
59+
refugia separated by open forest and grassland; other scientists argue that \
60+
the rainforest remained largely intact but extended less far to the north, \
61+
south, and east than is seen today. This debate has proved difficult to \
62+
resolve because the practical limitations of working in the rainforest mean \
63+
that data sampling is biased away from the center of the Amazon basin, and \
64+
both explanations are reasonably well supported by the available data.
65+
66+
Q: What does LGM stands for?
67+
A: Last Glacial Maximum.
68+
69+
Q: What did the analysis from the sediment deposits indicate?
70+
A: Rainfall in the basin during the LGM was lower than for the present.
71+
72+
Q: What are some of scientists arguments?
73+
A: The rainforest was reduced to small, isolated refugia separated by open forest and grassland.
74+
75+
Q: There have been major changes in Amazon rainforest vegetation over the last how many years?
76+
A: 21,000.
77+
78+
Q: What caused changes in the Amazon rainforest vegetation?
79+
A: The Last Glacial Maximum (LGM) and subsequent deglaciation
80+
81+
Q: What has been analyzed to compare Amazon rainfall in the past and present?
82+
A: Sediment deposits.
83+
84+
Q: What has the lower rainfall in the Amazon during the LGM been attributed to?
85+
A:
86+
`,
87+
};
88+
const instanceValue = helpers.toValue(instance);
89+
const instances = [instanceValue];
90+
91+
const parameter = {
92+
temperature: 0.2,
93+
maxOutputTokens: 256,
94+
topP: 0,
95+
topK: 1,
96+
};
97+
const parameters = helpers.toValue(parameter);
98+
99+
const request = {
100+
endpoint,
101+
instances,
102+
parameters,
103+
};
104+
105+
// Predict request
106+
const [response] = await predictionServiceClient.predict(request);
107+
console.log('Get text extraction response');
108+
const predictions = response.predictions;
109+
console.log('\tPredictions :');
110+
for (const prediction of predictions) {
111+
console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
112+
}
113+
}
114+
115+
callPredict();
116+
// [END aiplatform_sdk_extraction]
117+
}
118+
119+
process.on('unhandledRejection', err => {
120+
console.error(err.message);
121+
process.exitCode = 1;
122+
});
123+
124+
main(...process.argv.slice(2));
Lines changed: 120 additions & 0 deletions
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));
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 extraction', () => {
31+
it('should make predictions using a large language model', async () => {
32+
const stdout = execSync(
33+
`node ./predict-text-extraction.js ${project} ${location}`,
34+
{
35+
cwd,
36+
}
37+
);
38+
assert.match(stdout, /Get text extraction response/);
39+
});
40+
});
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 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)