|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 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() |
| 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() |
0 commit comments