Skip to main content
Toolkit/Framework

Use Alibaba Cloud Model Studio in LangChain

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
  • JavaScript
  • Java
  • OpenAI
  • DashScope
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/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.

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
  • JavaScript
  • Java
  • 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.

Reranker models

Supported models:

Model

Max documents

Max input tokens per item

Max input tokens per request

Supported languages

Scenarios

qwen3-vl-rerank

Text: 100

Image: 40

Video: 4

8,000

120,000

33 major languages, such as Chinese, English, Japanese, Korean, French, and German

  • Image clustering

  • Cross-modal search

  • Image retrieval

  • Video retrieval

qwen3-rerank

500

4,000

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

  • Text semantic retrieval

  • Retrieval-augmented generation (RAG) applications

gte-rerank-v2

30,000

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

  • 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.
Text Generation
Image Generation
Video Generation
World models
3D Model Generation
Audio
  • Audio generation
Realtime API
Text Embedding
Decision Model
TokenPlan
Model Production