Examples¶
Start with a runnable class, then use its guide to understand the SDK calls it makes. The examples are a separate Maven project under examples/; use a checkout or tag that matches your target SDK. See the full repository catalog for exact classes, inputs, and flags.
Run your first example¶
Clone the Java SDK repository. In its root, with JDK 8+ and Maven, install the SDK from that checkout into your local Maven repository:
mvn -q -f examples/pom.xml compile exec:java \
-Dexec.mainClass=com.alibaba.dashscope.examples.engineering.testing.LocalMockDebugging
No API key or cloud request. Look for response text, token usage, a typed exception, and an inline error response. The gateway listens on loopback; see local mock debugging.
export DASHSCOPE_API_KEY="<your-api-key>"
mvn -q -f examples/pom.xml compile exec:java \
-Dexec.mainClass=com.alibaba.dashscope.examples.atomic.chat.ChatCompletionsCreate
Look for a complete reply and then incremental text. If needed, configure your region, workspace and model. See configuration. Cloud calls may incur charges.
Find an example by capability¶
The following example class names are relative to com.alibaba.dashscope.examples.. Substitute the full name in -Dexec.mainClass above.
| I want to… | Example class | Learn how |
|---|---|---|
| Chat or stream, synchronously or asynchronously | atomic.chat.ChatCompletionsCreate, atomic.chat.ChatCompletionsCreateAsync |
Chat and streaming, async client |
| Ask a question about an image | atomic.multimodal.MultimodalChatCreate |
Multimodal |
| Retrieve and rerank knowledge | atomic.embeddings.EmbeddingsCreate, atomic.rerank.RerankCreate |
Embeddings and rerank |
| Generate media and track jobs | atomic.images.ImagesGenerate, atomic.videos.VideosGenerate |
Images and videos |
| Synthesize, transcribe, or start a realtime session | atomic.audio.* — start with the guide's example for your transport and audio input |
Audio |
| Upload data or monitor fine-tuning | atomic.files.FilesManage, atomic.finetunes.FineTunesCreate |
Files and fine-tuning |
| Count tokens locally or remotely | atomic.chat.TokenizationLocalCount, atomic.chat.TokenizationCount |
Tokenization |
| Call an app or get a structured decision | atomic.apps.AppsCompletionsCreate, atomic.decision.DecisionCreate |
App example, decision example |
| Run a managed agent and consume session events | recipes.agentstudio.AgentStudioQuickStart |
AgentStudio |
Go from a call to an application¶
| Layer | What to look for |
|---|---|
01_atomic_capabilities |
One capability per class: request builders, typed responses, and inputs. |
02_advanced_engineering |
Inject a mock, cache results, or use a circuit breaker to switch regions. |
03_agent_recipes |
Combine calls in a tool loop, a RAG pipeline, or a managed AgentStudio workflow. |
04_real_world_scenarios |
Customer service uses cloud calls. scenarios.rag.ProductionRagHarness, scenarios.meeting.RealtimeMeetingHarness, and scenarios.triage.TicketTriageHarness default to local demos. |
After installing the SDK from the same checkout, run mvn -f examples/pom.xml clean test to exercise the example dry runs without a cloud key. The suite uses mock endpoints; hardware-dependent microphone input and the HTTPS batch-transcription download are not covered. Maven may still need to fetch dependencies on first use.
Use a test workspace for examples that create resources. Some audio examples need an input file or a microphone. The legacy archive is not built by the examples project; use the migration guide when adapting a 2.x example.