You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fd02695
Browse filesBrowse the repository at this point in the historyBrowse files
luk384090-cloud
committed
feat(region): add multi-region endpoint and workspace support
- Introduce `set_region(region, workspace_id)` to switch HTTP, WebSocket, and OpenAI-compatible endpoints
- Add support for MaaS international regions including Singapore, US East, Hong Kong, Germany, Japan, besides Beijing
- Enable environment variable configuration for region and workspace ID
- Implement runtime resolution of endpoint URLs with `{workspace_id}` placeholder substitution
- Update internal API methods to support per-call workspace parameter for endpoint resolution
- Enhance documentation with detailed usage examples of multi-region configuration
- Add comprehensive unit tests covering region switching, URL resolving, and environment variable handling
By default the SDK sends requests to the China (Beijing) public endpoint `dashscope.aliyuncs.com`. If your Model Studio (Bailian) workspace lives in another region, switch the endpoint before making calls.
95
+
96
+
### Using `set_region`
97
+
98
+
`dashscope.set_region(region, workspace_id)` points the HTTP, WebSocket and OpenAI-compatible base URLs at the given region in a single call. `workspace_id` is required and is used as the endpoint subdomain.
99
+
100
+
```python
101
+
import dashscope
102
+
103
+
# Switch to the Singapore region for workspace "ws-xxx123"
> `set_region` updates process-wide globals, so it is not concurrency-safe when a single process talks to multiple regions at the same time. Call it once at startup, or re-call it before each switch.
export DASHSCOPE_WORKSPACE_ID='ws-xxx123'# used to resolve the endpoint subdomain
131
+
```
132
+
133
+
When a MaaS region is set via `DASHSCOPE_API_REGION`, the SDK builds the regional endpoints and substitutes `DASHSCOPE_WORKSPACE_ID` into them. You can also override each base URL directly:
The SDK exposes an OpenAI-compatible chat completions entry that talks to `dashscope.base_compatible_api_url` (request path `chat/completions`) — no extra `openai` package required. It follows the region configured above.
144
+
145
+
```python
146
+
import dashscope
147
+
from dashscope.aigc.chat_completion import Completions
stream=False, # set True to get a generator of ChatCompletionChunk
156
+
)
157
+
print(response)
158
+
```
159
+
160
+
A complete runnable example is available in [`samples/set_region_example.py`](samples/set_region_example.py).
161
+
92
162
## AI Assistant: DashScope SDK Expert
93
163
94
164
The SDK ships with an interactive AI assistant, **DashScope SDK Expert**, built on the bundled Agentic CLI (`dashscope/acli`) framework. For DashScope SDK/CLI users it is the recommended way to get development consultation and AI coding help — answering SDK/API questions, generating runnable examples, showing CLI usage, and diagnosing errors, right in your terminal.
0 commit comments