Skip to content

Commit 7233f32

Browse files
Thoughtseize1riathakkar
authored andcommitted
refactor: (GenAI) Reorganized Model Garden Samples (Group B) (GoogleCloudPlatform#12599)
1 parent a8d058f commit 7233f32

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
18+
19+
20+
def generate_text_streaming() -> str:
21+
# [START generativeaionvertexai_claude_3_streaming]
22+
# TODO(developer): Vertex AI SDK - uncomment below & run
23+
# pip3 install --upgrade --user google-cloud-aiplatform
24+
# gcloud auth application-default login
25+
# pip3 install -U 'anthropic[vertex]'
26+
27+
# TODO(developer): Update and un-comment below line
28+
# PROJECT_ID = "your-project-id"
29+
30+
from anthropic import AnthropicVertex
31+
32+
client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
33+
result = []
34+
35+
with client.messages.stream(
36+
model="claude-3-5-sonnet@20240620",
37+
max_tokens=1024,
38+
messages=[
39+
{
40+
"role": "user",
41+
"content": "Send me a recipe for banana bread.",
42+
}
43+
],
44+
) as stream:
45+
for text in stream.text_stream:
46+
print(text, end="", flush=True)
47+
result.append(text)
48+
49+
# Example response:
50+
# Here's a simple recipe for delicious banana bread:
51+
# Ingredients:
52+
# - 2-3 ripe bananas, mashed
53+
# - 1/3 cup melted butter
54+
# ...
55+
# ...
56+
# 8. Bake for 50-60 minutes, or until a toothpick inserted into the center comes out clean.
57+
# 9. Let cool in the pan for a few minutes, then remove and cool completely on a wire rack.
58+
59+
# [END generativeaionvertexai_claude_3_streaming]
60+
return "".join(result)
61+
62+
63+
if __name__ == "__main__":
64+
generate_text_streaming()
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def tool_use() -> object:
20+
# [START generativeaionvertexai_claude_3_tool_use]
21+
# TODO(developer): Vertex AI SDK - uncomment below & run
22+
# pip3 install --upgrade --user google-cloud-aiplatform
23+
# gcloud auth application-default login
24+
# pip3 install -U 'anthropic[vertex]'
25+
from anthropic import AnthropicVertex
26+
27+
# TODO(developer): Update and un-comment below line
28+
# PROJECT_ID = "your-project-id"
29+
30+
client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
31+
message = client.messages.create(
32+
model="claude-3-5-sonnet@20240620",
33+
max_tokens=1024,
34+
tools=[
35+
{
36+
"name": "text_search_places_api",
37+
"description": "returns information about a set of places based on a string",
38+
"input_schema": {
39+
"type": "object",
40+
"properties": {
41+
"textQuery": {
42+
"type": "string",
43+
"description": "The text string on which to search",
44+
},
45+
"priceLevels": {
46+
"type": "array",
47+
"description": "Price levels to query places, value can be one of [PRICE_LEVEL_INEXPENSIVE, PRICE_LEVEL_MODERATE, PRICE_LEVEL_EXPENSIVE, PRICE_LEVEL_VERY_EXPENSIVE]",
48+
},
49+
"openNow": {
50+
"type": "boolean",
51+
"description": "whether those places are open for business.",
52+
},
53+
},
54+
"required": ["textQuery"],
55+
},
56+
}
57+
],
58+
messages=[
59+
{
60+
"role": "user",
61+
"content": "What are some affordable and good Italian restaurants open now in San Francisco??",
62+
}
63+
],
64+
)
65+
print(message.model_dump_json(indent=2))
66+
# Example response:
67+
# {
68+
# "id": "msg_vrtx_018pk1ykbbxAYhyWUdP1bJoQ",
69+
# "content": [
70+
# {
71+
# "text": "To answer your question about affordable and good Italian restaurants
72+
# that are currently open in San Francisco....
73+
# ...
74+
75+
# [END generativeaionvertexai_claude_3_tool_use]
76+
return message
77+
78+
79+
if __name__ == "__main__":
80+
tool_use()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def generate_text() -> object:
20+
# [START generativeaionvertexai_claude_3_unary]
21+
# TODO(developer): Vertex AI SDK - uncomment below & run
22+
# pip3 install --upgrade --user google-cloud-aiplatform
23+
# gcloud auth application-default login
24+
# pip3 install -U 'anthropic[vertex]'
25+
26+
# TODO(developer): Update and un-comment below line
27+
# PROJECT_ID = "your-project-id"
28+
29+
from anthropic import AnthropicVertex
30+
31+
client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
32+
message = client.messages.create(
33+
model="claude-3-5-sonnet@20240620",
34+
max_tokens=1024,
35+
messages=[
36+
{
37+
"role": "user",
38+
"content": "Send me a recipe for banana bread.",
39+
}
40+
],
41+
)
42+
print(message.model_dump_json(indent=2))
43+
# Example response:
44+
# {
45+
# "id": "msg_vrtx_0162rhgehxa9rvJM5BSVLZ9j",
46+
# "content": [
47+
# {
48+
# "text": "Here's a simple recipe for delicious banana bread:\n\nIngredients:\n- 2-3 ripe bananas...
49+
# ...
50+
51+
# [END generativeaionvertexai_claude_3_unary]
52+
return message
53+
54+
55+
if __name__ == "__main__":
56+
generate_text()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 backoff
16+
17+
import claude_3_streaming_example
18+
import claude_3_tool_example
19+
import claude_3_unary_example
20+
21+
from google.api_core.exceptions import ResourceExhausted
22+
23+
24+
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
25+
def test_generate_text_streaming() -> None:
26+
responses = claude_3_streaming_example.generate_text_streaming()
27+
assert "bread" in responses
28+
29+
30+
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
31+
def test_tool_use() -> None:
32+
response = claude_3_tool_example.tool_use()
33+
json_response = response.model_dump_json(indent=2)
34+
assert "restaurant" in json_response
35+
assert "tool_use" in json_response
36+
assert "text_search_places_api" in json_response
37+
38+
39+
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
40+
def test_generate_text() -> None:
41+
responses = claude_3_unary_example.generate_text()
42+
assert "bread" in responses.model_dump_json(indent=2)

0 commit comments

Comments
 (0)