Skip to content

Commit 6d917c4

Browse files
author
luk384090-cloud
committed
feat(samples): add set_region example demonstrating regional switching for MaaS SDK
1 parent fd02695 commit 6d917c4

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

‎samples/set_region_example.py‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) Alibaba, Inc. and its affiliates.
3+
"""Example demonstrating how to use set_region() to switch MaaS regions.
4+
5+
This example shows how to configure the SDK to use different regional
6+
endpoints for MaaS (Model as a Service).
7+
"""
8+
9+
import dashscope
10+
from dashscope.aigc.chat_completion import Completions
11+
from dashscope.aigc.generation import Generation
12+
13+
14+
def example_basic_usage():
15+
"""Basic usage: switch to a specific region."""
16+
# Switch to Singapore region
17+
dashscope.set_region(region="ap-southeast-1", workspace_id="ws-xxx123")
18+
19+
# Now all API calls will use the Singapore endpoint
20+
print(f"HTTP API URL: {dashscope.base_http_api_url}")
21+
print(f"Compatible API URL: {dashscope.base_compatible_api_url}")
22+
23+
24+
def example_generation_call():
25+
"""Example: Use Generation.call with a specific region."""
26+
# Switch to US East region
27+
dashscope.set_region(region="us-east-1", workspace_id="ws-us-456")
28+
29+
# Make a generation call
30+
# This will use: https://ws-us-456.us-east-1.maas.aliyuncs.com/api/v1
31+
response = Generation.call(
32+
model="qwen-max",
33+
messages=[{"role": "user", "content": "Hello, world!"}],
34+
api_key="your-api-key", # Replace with your actual API key
35+
)
36+
print(response)
37+
38+
39+
def example_chat_completion():
40+
"""Example: Use Completions.create (OpenAI-compatible) with a region."""
41+
# Switch to Hong Kong region
42+
dashscope.set_region(region="cn-hongkong", workspace_id="ws-hk-789")
43+
44+
# Make a chat completion call
45+
# This will use: https://ws-hk-789.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1
46+
response = Completions.create(
47+
model="qwen-max",
48+
messages=[{"role": "user", "content": "你好,世界!"}],
49+
api_key="your-api-key", # Replace with your actual API key
50+
stream=False,
51+
)
52+
print(response)
53+
54+
55+
def example_streaming():
56+
"""Example: Streaming response with a specific region."""
57+
# Switch to Europe region
58+
dashscope.set_region(region="eu-central-1", workspace_id="ws-eu-001")
59+
60+
# Stream the response
61+
responses = Completions.create(
62+
model="qwen-max",
63+
messages=[{"role": "user", "content": "讲一个故事"}],
64+
api_key="your-api-key",
65+
stream=True,
66+
)
67+
68+
for chunk in responses:
69+
if chunk.choices:
70+
print(chunk.choices[0].delta.content, end="", flush=True)
71+
print()
72+
73+
74+
def example_multiple_regions():
75+
"""Example: Switch between multiple regions in the same session."""
76+
# First call to Singapore
77+
dashscope.set_region(region="ap-southeast-1", workspace_id="ws-sg-111")
78+
print(f"Region 1: {dashscope.base_http_api_url}")
79+
80+
# Then switch to Tokyo
81+
dashscope.set_region(region="ap-northeast-1", workspace_id="ws-jp-222")
82+
print(f"Region 2: {dashscope.base_http_api_url}")
83+
84+
# Finally switch to Beijing
85+
dashscope.set_region(region="cn-beijing", workspace_id="ws-bj-333")
86+
print(f"Region 3: {dashscope.base_http_api_url}")
87+
88+
89+
def example_error_handling():
90+
"""Example: Handle invalid region errors."""
91+
try:
92+
# This will raise ValueError
93+
dashscope.set_region(region="invalid-region", workspace_id="ws-xxx")
94+
except ValueError as e:
95+
print(f"Error: {e}")
96+
97+
try:
98+
# This will raise ValueError for missing workspace_id
99+
dashscope.set_region(region="cn-beijing")
100+
except ValueError as e:
101+
print(f"Error: {e}")
102+
103+
104+
if __name__ == "__main__":
105+
print("=== Basic Usage ===")
106+
example_basic_usage()
107+
108+
print("\n=== Multiple Regions ===")
109+
example_multiple_regions()
110+
111+
print("\n=== Error Handling ===")
112+
example_error_handling()
113+
114+
# Uncomment to test actual API calls (requires valid API key)
115+
# print("\n=== Generation Call ===")
116+
# example_generation_call()
117+
118+
# print("\n=== Chat Completion ===")
119+
# example_chat_completion()
120+
121+
# print("\n=== Streaming ===")
122+
# example_streaming()

0 commit comments

Comments
 (0)