import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
messages = [{
"role": "user",
"content": [{
"image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/ctdzex/biaozhun.jpg",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192}]
}]
response = client.multimodal.chat.create(
model='qwen-vl-ocr-2025-11-20',
messages=messages,
# Set the built-in task to advanced recognition
parameters={"ocr_options": {"task": "advanced_recognition"}}
)
# The advanced recognition task returns results as plain text
print(response.output_text)
import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
messages = [{
"role": "user",
"content": [{
"image": "http://duguang-llm.oss-cn-hangzhou.aliyuncs.com/llm_data_keeper/data/doc_parsing/tables/photo/eng/17.jpg",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192}]
}]
response = client.multimodal.chat.create(
model='qwen-vl-ocr-2025-11-20',
messages=messages,
# Set the built-in task to table parsing
parameters={"ocr_options": {"task": "table_parsing"}}
)
# The table parsing task returns results in HTML format
print(response.output_text)
import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
messages = [{
"role": "user",
"content": [{
"image": "https://img.alicdn.com/imgextra/i1/O1CN01ukECva1cisjyK6ZDK_!!6000000003635-0-tps-1500-1734.jpg",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192}]
}]
response = client.multimodal.chat.create(
model='qwen-vl-ocr-2025-11-20',
messages=messages,
# Set the built-in task to document parsing
parameters={"ocr_options": {"task": "document_parsing"}}
)
# The document parsing task returns results in LaTeX format
print(response.output_text)
import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
messages = [{
"role": "user",
"content": [{
"image": "http://duguang-llm.oss-cn-hangzhou.aliyuncs.com/llm_data_keeper/data/formula_handwriting/test/inline_5_4.jpg",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192
}]
}]
response = client.multimodal.chat.create(
model='qwen-vl-ocr-2025-11-20',
messages=messages,
# Set the built-in task to formula recognition
parameters={"ocr_options": {"task": "formula_recognition"}}
)
# The formula recognition task returns results in LaTeX format
print(response.output_text)
import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
messages = [{
"role": "user",
"content": [{
"image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/ctdzex/biaozhun.jpg",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192}]
}]
response = client.multimodal.chat.create(
model='qwen-vl-ocr-2025-11-20',
messages=messages,
# Set the built-in task to text recognition
parameters={"ocr_options": {"task": "text_recognition"}}
)
# The text recognition task returns results as plain text
print(response.output_text)
import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
messages = [{
"role": "user",
"content": [{
"image": "https://img.alicdn.com/imgextra/i2/O1CN01VvUMNP1yq8YvkSDFY_!!6000000006629-2-tps-6000-3000.png",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192}]
}]
response = client.multimodal.chat.create(
model='qwen-vl-ocr-2025-11-20',
messages=messages,
# Set the built-in task to multi-language recognition
parameters={"ocr_options": {"task": "multi_lan"}}
)
# The multi-language recognition task returns results as plain text
print(response.output_text)
import os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
# For the US (Virginia) region, set region to us-east-1.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx",
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
PROMPT_TICKET_EXTRACTION = """
请提取车票图像中的发票号码、车次、起始站、终点站、发车日期和时间点、座位号、席别类型、票价、身份证号码、购票人姓名。
要求准确无误的提取上述关键信息、不要遗漏和捏造虚假信息,模糊或者强光遮挡的单个文字可以用英文问号?代替。
返回数据格式以json方式输出,格式为:{'发票号码': 'xxx', '起始站': 'xxx', '终点站': 'xxx', '发车日期和时间点':'xxx', '座位号': 'xxx','票价':'xxx', '身份证号码': 'xxx', '购票人姓名': 'xxx'},
"""
messages = [
{
"role": "user",
"content": [
{
"image": "https://img.alicdn.com/imgextra/i2/O1CN01ktT8451iQutqReELT_!!6000000004408-0-tps-689-487.jpg",
# Minimum pixel threshold for the input image: images smaller than this are enlarged until the total pixels exceed min_pixels
"min_pixels": 32 * 32 * 3,
# Maximum pixel threshold for the input image: images larger than this are shrunk until the total pixels fall below max_pixels
"max_pixels": 32 * 32 * 8192,
},
# When no built-in task is set, you can pass a prompt in the text field; if omitted, the default prompt is used: Please output only the text content from the image without any additional descriptions or formatting.
{
"type": "text",
"text": PROMPT_TICKET_EXTRACTION,
},
],
}
]
stream = client.multimodal.chat.create(
model="qwen-vl-ocr-2025-11-20",
messages=messages,
stream=True,
# Streaming output is incremental by default: each chunk contains only the newly generated content.
)
full_content = ""
print("流式输出内容为:")
for chunk in stream:
delta = chunk.choices[0].delta
if delta.content:
print(delta.content)
full_content += delta.content
print(f"完整内容为:{full_content}")
import java.util.*;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import io.reactivex.Flowable;
import com.alibaba.dashscope.utils.Constants;
public class Main {
// 以下为新加坡地域base_url,若使用弗吉尼亚地域模型,需要将base_url换成https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/api/v1
// 若使用北京地域的模型,需将base_url替换为:https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1
static {
Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
}
public static void simpleMultiModalConversationCall()
throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
Map<String, Object> map = new HashMap<>();
map.put("image", "https://img.alicdn.com/imgextra/i2/O1CN01ktT8451iQutqReELT_!!6000000004408-0-tps-689-487.jpg");
// 输入图像的最大像素阈值,超过该值图像会缩小,直到总像素低于max_pixels
map.put("max_pixels", 8388608);
// 输入图像的最小像素阈值,小于该值图像会放大,直到总像素大于min_pixels
map.put("min_pixels", 3072);
MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
.content(Arrays.asList(
map,
// 未设置内置任务时,支持在text字段中传入Prompt,若未传入则使用默认的Prompt:Please output only the text content from the image without any additional descriptions or formatting.
Collections.singletonMap("text", "请提取车票图像中的发票号码、车次、起始站、终点站、发车日期和时间点、座位号、席别类型、票价、身份证号码、购票人姓名。要求准确无误的提取上述关键信息、不要遗漏和捏造虚假信息,模糊或者强光遮挡的单个文字可以用英文问号?代替。返回数据格式以json方式输出,格式为:{'发票号码': 'xxx', '起始站': 'xxx', '终点站': 'xxx', '发车日期和时间点':'xxx', '座位号': 'xxx','票价':'xxx', '身份证号码': 'xxx', '购票人姓名': 'xxx'"))).build();
MultiModalConversationParam param = MultiModalConversationParam.builder()
// 各地域的API Key不同。获取API Key:https://www.alibabacloud.com/help/model-studio/get-api-key
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-vl-ocr-2025-11-20")
.message(userMessage)
.incrementalOutput(true)
.build();
Flowable<MultiModalConversationResult> result = conv.streamCall(param);
result.blockingForEach(item -> {
try {
List<Map<String, Object>> contentList = item.getOutput().getChoices().get(0).getMessage().getContent();
if (!contentList.isEmpty()){
System.out.println(contentList.get(0).get("text"));
}//
} catch (Exception e){
System.exit(0);
}
});
}
public static void main(String[] args) {
try {
simpleMultiModalConversationCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}