MCPcopy Index your code
hub / github.com/dashscope/dashscope-sdk-java

github.com/dashscope/dashscope-sdk-java @v2.22.24

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.22.24 ↗ · + Follow
2,990 symbols 12,073 edges 547 files 452 documented · 15% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

dashscope-sdk-java

This is the java sdk for the DashScope models.

Usage

To use the sdk in your java systems, please add the maven dependency in your pom.xml:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dashscope-sdk-java</artifactId>
    <version>{dashscope-sdk-java-version}</version>
</dependency>

QuickStart

Generation

You can create a generation client simply by:

Generation generation = new Generation();

The generation interface supports both stream and non-stream queries. These queries all accept GenerationParam as input, and return GenerationResult as output.

Here shows the usages of each method, with the examples of qwen-turbo model.

Support stream and non-stream mode, accept output from callback

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
      Generation generation = new Generation();
      GenerationParam param = GenerationParam.builder()
          .apiKey(System.getenv("DASHSCOPE_API_KEY"))
          .model(Generation.Models.QWEN_TURBO)
          .messages(Arrays.asList(
              Message.builder()
                  .role(Role.USER.getValue())
                  .content("Hello, how are you?").build()
          )).build();

      class ReactCallback extends ResultCallback<GenerationResult> {

        @Override
        public void onEvent(GenerationResult message) {
          System.out.println(JsonUtils.toJson(message));
        }

        public void onComplete() {
          // TODO all messages received
        }

        public void onError(Exception e) {
          ApiException apiException = (ApiException) e;
          // TODO deal with exception
        }
      }

      generation.call(param, new ReactCallback());
    }
}

The Exception instance is an ApiException instance. This Exception may contain two parts:

  • A Status instance. This instance carries a status_code (the http error code), a code (server error code), a message (server error message), the request id, and the usage information.
  • If an exception occurs, the ApiException instance may only carry an Exception stack trace, you can deal with it as you usually do.

Stream only, accept by reactive io

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    Generation generation = new Generation();

    Message systemMsg = Message.builder()
        .role(Role.SYSTEM.getValue())
        .content("You are a helpful assistant.")
        .build();
    Message userMsg = Message.builder()
        .role(Role.USER.getValue())
        .content("Hello!")
        .build();
    GenerationParam param = GenerationParam.builder()
        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
        .model(Generation.Models.QWEN_TURBO)
        .messages(Arrays.asList(systemMsg, userMsg))
        .resultFormat(GenerationParam.ResultFormat.MESSAGE)
        .build();

    try {
      Flowable<GenerationResult> result = generation.streamCall(param);
      result.blockingForEach(msg -> System.out.println(JsonUtils.toJson(msg)));
    } catch (ApiException | NoApiKeyException | InputRequiredException e) {
      System.err.println("An error occurred: " + e.getMessage());
    }
  }
}

The streamCall method accepts a GenerationParam, and returns a Flowable, which you can get the streaming result by blockingForEach, and catch the exception by the try-catch block.

Non-stream only

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    Generation generation = new Generation();

    Message systemMsg = Message.builder()
        .role(Role.SYSTEM.getValue())
        .content("You are a helpful assistant.")
        .build();
    Message userMsg = Message.builder()
        .role(Role.USER.getValue())
        .content("Hello!")
        .build();
    GenerationParam param = GenerationParam.builder()
        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
        .model(Generation.Models.QWEN_TURBO)
        .messages(Arrays.asList(systemMsg, userMsg))
        .resultFormat(GenerationParam.ResultFormat.MESSAGE)
        .build();

    try {
      GenerationResult result = generation.call(param);
      System.out.println(JsonUtils.toJson(result));
    } catch (ApiException | NoApiKeyException | InputRequiredException e) {
      System.err.println("An error occurred: " + e.getMessage());
    }
  }
}

The call method accepts a GenerationParam, and returns a GenerationResult, you can also catch the exception with a try-catch block.

Extension points exported contracts — how you extend this code

ServiceOption (Interface)
Internal used for config the service. [4 implementers]
src/main/java/com/alibaba/dashscope/protocol/ServiceOption.java
SseEventBuilderInterface (Interface)
A builder for an SSE event. [2 implementers]
src/test/java/com/alibaba/dashscope/ServerSentEvent.java
MultiModalMessageItemBase (Interface)
(no doc) [11 implementers]
src/main/java/com/alibaba/dashscope/aigc/multimodalconversation/MultiModalMessageItemBase.java
HalfDuplexClient (Interface)
(no doc) [4 implementers]
src/main/java/com/alibaba/dashscope/protocol/HalfDuplexClient.java
AudioWebsocketCallback (Interface)
@author songsong.shao [2 implementers]
src/main/java/com/alibaba/dashscope/audio/protocol/AudioWebsocketCallback.java
AssistantEventHandler (Interface)
(no doc) [4 implementers]
src/main/java/com/alibaba/dashscope/threads/runs/AssistantEventHandler.java

Core symbols most depended-on inside this repo

build
called by 908
src/test/java/com/alibaba/dashscope/ServerSentEvent.java
builder
called by 873
src/main/java/com/alibaba/dashscope/agentstudio/AgentStudioClient.java
get
called by 705
src/main/java/com/alibaba/dashscope/api/GeneralApi.java
format
called by 279
src/main/java/com/alibaba/dashscope/utils/StringUtils.java
toJson
called by 224
src/main/java/com/alibaba/dashscope/utils/JsonUtils.java
getMessage
called by 186
src/main/java/com/alibaba/dashscope/exception/ApiException.java
getValue
called by 179
src/main/java/com/alibaba/dashscope/common/Role.java
getId
called by 142
src/main/java/com/alibaba/dashscope/tools/ToolCallBase.java

Shape

Method 2,260
Class 684
Enum 35
Interface 11

Languages

Java100%

Modules by API surface

src/test/java/com/alibaba/dashscope/TestAgentStudio.java76 symbols
src/test/java/com/alibaba/dashscope/WebSocketRecorder.java52 symbols
src/test/java/com/alibaba/dashscope/AssistantEventHandlerTest.java31 symbols
src/main/java/com/alibaba/dashscope/threads/runs/DefaultAssistantEventHandler.java31 symbols
src/main/java/com/alibaba/dashscope/audio/omni/OmniRealtimeConversation.java29 symbols
src/main/java/com/alibaba/dashscope/audio/ttsv2/SpeechSynthesizerV2.java28 symbols
src/main/java/com/alibaba/dashscope/agentstudio/resource/Vaults.java28 symbols
src/main/java/com/alibaba/dashscope/threads/runs/AssistantEventHandler.java26 symbols
src/main/java/com/alibaba/dashscope/multimodal/MultiModalDialog.java24 symbols
src/main/java/com/alibaba/dashscope/audio/ttsv2/SpeechSynthesizer.java24 symbols
src/main/java/com/alibaba/dashscope/protocol/okhttp/OkHttpWebSocketClient.java23 symbols
src/main/java/com/alibaba/dashscope/audio/qwen_tts_realtime/QwenTtsRealtime.java23 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add dashscope-sdk-java \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact