Use Alibaba Cloud Model Studio in LangChain

更新时间:
复制 MD 格式

This topic describes how to integrate models from Alibaba Cloud Model Studio into the LangChain large language model (LLM) application development framework.

Prerequisites

Chat models

Python

OpenAI

Only some models from Alibaba Cloud Model Studio are supported. For a complete list, see List of models supported in OpenAI compatibility mode. For information about calling fees, input limits, and output limits, see Model overview.

Before you start, install the following dependency:

pip install langchain_openai

Call the model:

from langchain_openai import ChatOpenAI
import os

chatLLM = ChatOpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    model="qwen-plus",  # This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/en/model-studio/getting-started/models
    # other params...
)
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who are you?"}]
response = chatLLM.invoke(messages)
print(response.json())

For advanced techniques such as tool calling, see the official LangChain ChatOpenAI documentation. For the complete API reference, see the official LangChain ChatOpenAI API Reference.

DashScope

All text generation models from Alibaba Cloud Model Studio are supported. For a complete list and information on calling fees, see Model overview. (This also supports models after deployment.)

Before you start, install the following dependencies:

pip install langchain-community
pip install dashscope

Call the model:

from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage

chatLLM = ChatTongyi(
    model="qwen-plus",   # This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/en/model-studio/getting-started/models
    streaming=True,
    # other params...
)
res = chatLLM.stream([HumanMessage(content="hi")], streaming=True)
for r in res:
    print("chat resp:", r.content)

For advanced techniques such as multi-modal calling and tool calling, see the official LangChain ChatTongyi documentation. For the complete API reference, see the official LangChain ChatTongyi API Reference.

JavaScript

OpenAI

Only some models from Alibaba Cloud Model Studio are supported. For a complete list, see List of models supported in OpenAI compatibility mode. For information about calling fees, input limits, and output limits, see Model overview.

Before you start, install the following dependencies:

npm install @langchain/openai @langchain/core

Call the model:

import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  // This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/en/model-studio/getting-started/models
  model: "qwen-plus",
  apiKey: process.env.DASHSCOPE_API_KEY,
  configuration: {
    baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
    // other params...
  },
  // other params...
});

const aiMsg = await llm.invoke([
  {
    role: "system",
    content:
      "You are a helpful assistant that translates English to French. Translate the user sentence.",
  },
  {
    role: "user",
    content: "I love programming.",
  },
]);
console.log('---------------------------');
console.log(aiMsg.content);

For advanced techniques such as tool calling, see the official LangChain ChatOpenAI documentation. For the complete API reference, see the official LangChain ChatOpenAI API Reference.

DashScope

All text generation models from Alibaba Cloud Model Studio are supported. For a complete list and information on calling fees, see Model overview. (This also supports models after deployment.)

Before you start, install the following dependencies:

npm install @langchain/community @langchain/core

Call the model:

import { ChatAlibabaTongyi } from "@langchain/community/chat_models/alibaba_tongyi";
import { HumanMessage } from "@langchain/core/messages";

// Default model is qwen-turbo
const qwenTurbo = new ChatAlibabaTongyi({
  alibabaApiKey: process.env.DASHSCOPE_API_KEY,
  // other params...
});

// Use qwen-plus
const qwenPlus = new ChatAlibabaTongyi({
// This example uses qwen-plus. You can change the model name as needed. Model list: https://help.aliyun.com/en/model-studio/getting-started/models
  model: "qwen-plus",
  temperature: 1,
  alibabaApiKey: process.env.DASHSCOPE_API_KEY,
  // other params...
});

const messages = [new HumanMessage("Hello")];

const res = await qwenTurbo.invoke(messages);

const res2 = await qwenPlus.invoke(messages);

console.log('---------------------------');
console.log(res.content);
console.log('---------------------------');
console.log(res2.content);

For advanced techniques such as multi-modal calling and tool calling, see the official LangChain ChatTongyi documentation. For the complete API reference, see the official LangChain ChatTongyi API Reference.

Java

OpenAI

Only some models from Alibaba Cloud Model Studio are supported. For a complete list, see List of models supported in OpenAI compatibility mode. For information about calling fees, input limits, and output limits, see Model overview.

You can use the open source LangChain4j library to implement these features in Java. Two implementation methods are available: Plain Java and Spring Boot. For more details and code examples, see the official LangChain4j OpenAI website.

Plain Java

  1. Add dependencies

    For example, in Maven, add the following dependency to the pom.xml file:

    <!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-open-ai -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-open-ai</artifactId>
        <version>1.0.0-beta3</version>
    </dependency>
  2. Write code to call the model

    import dev.langchain4j.data.message.SystemMessage;
    import dev.langchain4j.data.message.UserMessage;
    import dev.langchain4j.model.chat.ChatLanguageModel;
    import dev.langchain4j.model.openai.OpenAiChatModel;
    
    public class LangChainOpenAITest {
        public static void main(String[] args) {
            ChatLanguageModel model = OpenAiChatModel.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                    .modelName("qwen-plus")
                    .build();
    
            SystemMessage systemMessage = SystemMessage.from("You are a psychology expert");
            UserMessage userMessage = UserMessage.from("Hello");
    
            System.out.println(model.chat(systemMessage, userMessage).aiMessage().text());
        }
    }

Spring Boot

  1. Add dependencies

    For example, in Maven, add the following dependency to the pom.xml file:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-open-ai-spring-boot-starter -->
        <dependency>
            <groupId>dev.langchain4j</groupId>
    	<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
    	<version>1.0.0-beta3</version>
        </dependency>
    </dependencies>
  2. Configure model information

    Configure the model, port number, and other information in the application.properties file:

    langchain4j.open-ai.chat-model.api-key=${DASHSCOPE_API_KEY}
    langchain4j.open-ai.chat-model.model-name=qwen-plus
    langchain4j.open-ai.chat-model.base-url=https://dashscope.aliyuncs.com/compatible-mode/v1
    
    server.port=9000
  3. Write code to call the model

    import dev.langchain4j.model.chat.ChatLanguageModel;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ChatLanguageModelController {
        ChatLanguageModel chatLanguageModel;
    
        ChatLanguageModelController(ChatLanguageModel chatLanguageModel) {
            this.chatLanguageModel = chatLanguageModel;
        }
    
        @GetMapping("/chat")
        public String chat(@RequestParam(value = "message", defaultValue = "Hello") String message) {
            return chatLanguageModel.chat(message);
        }
    }

DashScope

All text generation models from Alibaba Cloud Model Studio are supported. For a complete list and information on calling fees, see Model overview. (This also supports models after deployment.)

You can use the open source LangChain4j library to implement these features in Java. Two implementation methods are available: Plain Java and Spring Boot. For more details and code examples, see the official LangChain4j DashScope website.

Plain Java

  1. Add dependencies

    For example, in Maven, add the following dependency to the pom.xml file:

    <!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-community-dashscope -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-community-dashscope</artifactId>
        <version>1.0.0-beta3</version>
    </dependency>
  2. Write code to call the model

    import dev.langchain4j.community.model.dashscope.QwenChatModel;
    import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
    import dev.langchain4j.data.message.ChatMessage;
    import dev.langchain4j.data.message.SystemMessage;
    import dev.langchain4j.data.message.UserMessage;
    import dev.langchain4j.model.chat.ChatLanguageModel;
    import dev.langchain4j.model.chat.StreamingChatLanguageModel;
    import dev.langchain4j.model.chat.request.ChatRequest;
    import dev.langchain4j.model.chat.response.ChatResponse;
    import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
    
    public class LangChainDashScopeTest {
        public static void main(String[] args) {
            chatLanguageModelTest();
    //        streamingChatLanguageModelTest();
        }
    
        public static void chatLanguageModelTest() {
            ChatLanguageModel qwenModel = QwenChatModel.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .modelName("qwen-plus")
                    .build();
    //        System.out.println(qwenModel.chat("Hello"));
            ChatRequest request = ChatRequest.builder().messages(new ChatMessage[]{SystemMessage.from("You are a psychology expert"), UserMessage.from("Hello")}).build();
            System.out.println(qwenModel.chat(request).aiMessage().text());
        }
    
        public static void streamingChatLanguageModelTest() {
            StreamingChatLanguageModel model = QwenStreamingChatModel.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .modelName("qwen-plus")
                    .build();
            model.chat("Hello", new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String s) {
                    System.out.println(s);
                }
    
                @Override
                public void onCompleteResponse(ChatResponse chatResponse) {
                    System.out.println("Conversation ended");
                    System.exit(0);
                }
    
                @Override
                public void onError(Throwable throwable) {
                    System.out.println("An error occurred");
                    System.exit(0);
                }
            });
        }
    }

Spring Boot

  1. Add dependencies

    For example, in Maven, add the following dependency to the pom.xml file:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-community-dashscope-spring-boot-starter -->
        <dependency>
            <groupId>dev.langchain4j</groupId>
    	<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
    	<version>1.0.0-beta3</version>
        </dependency>
    </dependencies>
  2. Configure model information

    Configure the port number and other information in the application.properties file:

    server.port=9000
  3. Write code to call the model

    import dev.langchain4j.community.model.dashscope.QwenChatModel;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class QwenChatModelController {
        QwenChatModel qwenChatModel;
    
        QwenChatModelController() {
            qwenChatModel = QwenChatModel.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .modelName("qwen-plus")
                    .build();
        }
    
        @GetMapping("/chat")
        public String chat(@RequestParam(value = "message", defaultValue = "Hello") String message) {
            return qwenChatModel.chat(message);
        }
    }

Text embedding models

Supported models:

MTEB and CMTEB are common benchmarks for evaluating embedding models. A higher value indicates better model performance. You cannot specify the vector dimensions for the text-embedding-v3 and text-embedding-v4 models in the LangChain framework. The output vector dimension defaults to 1024.

Model

MTEB

MTEB (Retrieval task)

CMTEB

CMTEB (Retrieval task)

text-embedding-v1

58.30

45.47

59.84

56.59

text-embedding-v2

60.13

49.49

62.17

62.78

text-embedding-v3 (1024 dimensions)

63.39

55.41

68.92

73.23

text-embedding-v4 (1024 dimensions)

68.36

59.30

70.14

73.98

Python

DashScope

Before you start, install the following dependencies:

pip install langchain-community
pip install dashscope

Call the model:

from langchain_community.embeddings import DashScopeEmbeddings
embeddings = DashScopeEmbeddings(
    model="text-embedding-v4",
    # other params...
)

text = "This is a test document."

query_result = embeddings.embed_query(text)
print("Text vector length:", len(query_result), sep='')

doc_results = embeddings.embed_documents(
    [
        "Hi there!",
        "Oh, hello!",
        "What's your name?",
        "My friends call me World",
        "Hello World!"
    ])
print("Number of text vectors:", len(doc_results), ",Text vector length:", len(doc_results[0]), sep='')

For a detailed introduction and usage examples, see the official LangChain DashScope Embeddings documentation. For the complete API reference, see the official LangChain Embedding API Reference.

JavaScript

DashScope

Before you start, install the following dependencies:

npm install @langchain/community @langchain/core

Call the model:

import { AlibabaTongyiEmbeddings } from "@langchain/community/embeddings/alibaba_tongyi";

const model = new AlibabaTongyiEmbeddings({ 
  apiKey: process.env.DASHSCOPE_API_KEY,
  modelName: "text-embedding-v4",
  // other params...
  });
const res = await model.embedQuery(
  "What would be a good company name a company that makes colorful socks?",
);
console.log('---------------------------');
console.log({ res });

For a detailed introduction and usage examples, see the official LangChain DashScope Embeddings documentation. For the complete API reference, see the official LangChain Embedding API Reference.

Java

DashScope

You can use the open source LangChain4j library to implement features in Java. For more information, including interface details and code examples, see the official LangChain4j DashScope website.

  1. Add dependencies

    For example, in Maven, add the following dependency to the pom.xml file:

    <!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-community-dashscope -->
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-community-dashscope</artifactId>
        <version>1.0.0-beta3</version>
    </dependency>
  2. Write code and call the model

    import dev.langchain4j.community.model.dashscope.QwenEmbeddingModel;
    import dev.langchain4j.data.embedding.Embedding;
    
    import java.util.List;
    
    import static dev.langchain4j.data.segment.TextSegment.textSegment;
    import static java.util.Arrays.asList;
    
    public class LangChainEmbeddingsTest {
        public static void main(String[] args) {
            QwenEmbeddingModel model = QwenEmbeddingModel.builder()
                    .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                    .modelName("text-embedding-v4")
                    .build();
    
            List<Embedding> embeddings = model.embedAll(asList(textSegment("hello"), textSegment("how are you?")))
                    .content();
            System.out.println(embeddings.size());
        }
    }

Reranker models

Supported models:

Model

Max number of documents

Max input tokens per item

Max input tokens per request

Supported languages

Price (Thousand input tokens)

Free quota

Scenarios

qwen3-rerank

500

4,000

30,000

Over 100 major languages, such as Chinese, English, Spanish, French, Portuguese, Indonesian, Japanese, Korean, German, and Russian

CNY 0.0005

1 million tokens

Valid for 90 days after you activate Model Studio.

  • Text semantic retrieval

  • Retrieval-augmented generation (RAG) applications

gte-rerank-v2

Over 50 languages, such as Chinese, English, Japanese, Korean, Thai, Spanish, French, Portuguese, German, Indonesian, and Arabic

CNY 0.0008

Python

DashScope

Before you start, install the following dependencies:

pip install langchain-community
pip install dashscope

Call the model:

from langchain_community.document_compressors.dashscope_rerank import DashScopeRerank

sequence = ["text1", "text2", "text3"]

reranker = DashScopeRerank(
    model="gte-rerank-v2",
    # other params...
)
print(reranker.rerank(documents=sequence, query="text3", top_n=2))

For a detailed introduction and more usage examples, see the official LangChain DashScope Rerank documentation. For the complete API reference, see the official LangChain Rerank API Reference.