Skip to content

Commit 64cf1d5

Browse files
feat(generative-ai): update claude_3_streaming_example.py (#12710)
1 parent dc30570 commit 64cf1d5

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed

generative_ai/model_garden/claude_3_streaming_example.py

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,54 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# 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()
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()

generative_ai/model_garden/model_garden_test.py renamed to generative_ai/model_garden/test_model_garden_examples.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
from google.api_core.exceptions import ResourceExhausted
1818

19-
# import claude_3_streaming_example
19+
import claude_3_streaming_example
2020
import claude_3_tool_example
2121
import claude_3_unary_example
2222

2323

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
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
2828

2929

3030
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)

0 commit comments

Comments
 (0)