Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(generative-ai): update claude_3_streaming_example.py #12710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 51 additions & 51 deletions 102 generative_ai/model_garden/claude_3_streaming_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,54 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# import os
#
# PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
#
#
# def generate_text_streaming() -> str:
# # [START generativeaionvertexai_claude_3_streaming]
# # TODO(developer): Vertex AI SDK - uncomment below & run
# # pip3 install --upgrade --user google-cloud-aiplatform
# # gcloud auth application-default login
# # pip3 install -U 'anthropic[vertex]'
#
# # TODO(developer): Update and un-comment below line
# # PROJECT_ID = "your-project-id"
#
# from anthropic import AnthropicVertex
#
# client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
# result = []
#
# with client.messages.stream(
# model="claude-3-5-sonnet@20240620",
# max_tokens=1024,
# messages=[
# {
# "role": "user",
# "content": "Send me a recipe for banana bread.",
# }
# ],
# ) as stream:
# for text in stream.text_stream:
# print(text, end="", flush=True)
# result.append(text)
#
# # Example response:
# # Here's a simple recipe for delicious banana bread:
# # Ingredients:
# # - 2-3 ripe bananas, mashed
# # - 1/3 cup melted butter
# # ...
# # ...
# # 8. Bake for 50-60 minutes, or until a toothpick inserted into the center comes out clean.
# # 9. Let cool in the pan for a few minutes, then remove and cool completely on a wire rack.
#
# # [END generativeaionvertexai_claude_3_streaming]
# return "".join(result)
#
#
# if __name__ == "__main__":
# generate_text_streaming()

import os

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def generate_text_streaming() -> str:
# [START generativeaionvertexai_claude_3_streaming]
# TODO(developer): Vertex AI SDK - uncomment below & run
# pip3 install --upgrade --user google-cloud-aiplatform
# gcloud auth application-default login
# pip3 install -U 'anthropic[vertex]'

# TODO(developer): Update and un-comment below line
# PROJECT_ID = "your-project-id"

from anthropic import AnthropicVertex

client = AnthropicVertex(project_id=PROJECT_ID, region="us-east5")
result = []

with client.messages.stream(
model="claude-3-5-sonnet@20240620",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Send me a recipe for banana bread.",
}
],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
result.append(text)

# Example response:
# Here's a simple recipe for delicious banana bread:
# Ingredients:
# - 2-3 ripe bananas, mashed
# - 1/3 cup melted butter
# ...
# ...
# 8. Bake for 50-60 minutes, or until a toothpick inserted into the center comes out clean.
# 9. Let cool in the pan for a few minutes, then remove and cool completely on a wire rack.

# [END generativeaionvertexai_claude_3_streaming]
return "".join(result)


if __name__ == "__main__":
generate_text_streaming()
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

from google.api_core.exceptions import ResourceExhausted

# import claude_3_streaming_example
import claude_3_streaming_example
import claude_3_tool_example
import claude_3_unary_example


# @backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
# def test_generate_text_streaming() -> None:
# responses = claude_3_streaming_example.generate_text_streaming()
# assert "bread" in responses
@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
def test_generate_text_streaming() -> None:
responses = claude_3_streaming_example.generate_text_streaming()
assert "bread" in responses


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=10)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.