Skip to content

Commit 52aa90a

Browse files
feat(vertex-ai): convert and fix evaluation/example-syntax markdown sample
1 parent b9b28d7 commit 52aa90a

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from google.auth.transport import Response
18+
19+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
20+
LOCATION = "us-central1"
21+
22+
23+
def send_evaluation_request_gapic() -> Response:
24+
# [START generativeaionvertexai_evaluation_example_syntax]
25+
import json
26+
27+
from google import auth
28+
from google.auth.transport import requests as google_auth_requests
29+
30+
# TODO(developer): Update & uncomment line below
31+
# PROJECT_ID = "your-project-id"
32+
# LOCATION = "us-central1"
33+
34+
creds, _ = auth.default(
35+
scopes=['https://www.googleapis.com/auth/cloud-platform'],
36+
)
37+
38+
# Check the API reference for details:
39+
# https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/evaluation#bleuinput
40+
data = {
41+
"bleu_input": {
42+
"metric_spec": {
43+
"use_effective_order": False,
44+
},
45+
"instances": [
46+
{
47+
"prediction": "The quick brown fox jumps over the lazy dog",
48+
"reference": "A fast brown fox leaps across the sleeping dog",
49+
}
50+
]
51+
}
52+
}
53+
54+
uri = f"https://{LOCATION}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT_ID}/locations/{LOCATION}:evaluateInstances"
55+
response = google_auth_requests.AuthorizedSession(creds).post(uri, json=data)
56+
57+
# print(response.json())
58+
print(json.dumps(response.json(), indent=2))
59+
# Example response:
60+
# {
61+
# "bleuResults": {
62+
# "bleuMetricValues": [
63+
# {
64+
# "score": 0.113395825
65+
# }
66+
# ]
67+
# }
68+
# }
69+
70+
# [END generativeaionvertexai_evaluation_example_syntax]
71+
return response
72+
73+
74+
if __name__ == "__main__":
75+
send_evaluation_request_gapic()

generative_ai/evaluation/test_evaluation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import example_syntax
1516
import get_rouge_score
1617
import pairwise_summarization_quality
1718

@@ -24,3 +25,8 @@ def test_create_evaluation_task() -> None:
2425
def test_pairwise_evaluation_summarization_quality() -> None:
2526
response = pairwise_summarization_quality.evaluate_output()
2627
assert response
28+
29+
30+
def test_example_syntax() -> None:
31+
response = example_syntax.send_evaluation_request_gapic()
32+
assert response

0 commit comments

Comments
 (0)