文档

快速入门

更新时间:

本文以通义千问大模型(qwen-turbo)为例,介绍通过DashScope玩转大语言模型的基本使用方法。

大语言模型可以与人类就几乎任何话题进行海阔天空的聊天。小明周末在家想做一顿美餐,但由于他是厨房新手,不知道该怎么烹饪。他希望聊天大模型能够帮到他,于是向大模型提出:“用萝卜、土豆、茄子做饭,给我个菜谱”。

前提条件

  • 请您参考API-KEY的获取与配置,开通DashScope并获得API-KEY。

  • 您可以使用OpenAI SDK、DashScope SDK或HTTP接口调用通义千问模型,请您根据您的需求,参考以下方式准备您的计算环境。

    说明

    如果您之前使用OpenAI SDK以及HTTP方式调用OpenAI的服务,只需在原有框架下调整API-KEY、base_url、model等参数,就可以直接调用通义千问模型。

    调用方式

    准备条件

    通过OpenAI Python SDK调用

    您可以通过以下命令安装或更新OpenAI SDK:

    # 如果下述命令报错,请将pip替换为pip3
    pip install -U openai

    您需要配置的base_url如下:

    https://dashscope.aliyuncs.com/compatible-mode/v1

    通过OpenAI兼容-HTTP调用

    如果您需要通过OpenAI兼容的HTTP方式进行调用,需要配置的完整访问endpoint如下:

    POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions

    通过DashScope SDK调用

    DashScope SDK提供了Python和Java两个版本,请参考安装SDK,安装最新版SDK。

    通过DashScope HTTP调用

    如果您需要通过DashScope的HTTP方式进行调用,需要配置的完整访问endpoint如下:

    POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation
说明

我们推荐您将API-KEY配置到环境变量中以降低API-KEY的泄漏风险,详情可参考通过环境变量设置API-KEY。您也可以在代码中配置API-KEY,但是会存在泄露风险。

从最简单的指令开始

OpenAI兼容

您可以通过OpenAI SDK或OpenAI兼容的HTTP方式调用通义千问模型。

Python

示例代码

from openai import OpenAI
import os

def get_response():
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # 填写DashScope服务的base_url
    )
    completion = client.chat.completions.create(
        model="qwen-turbo",
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},
            {'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}],
        temperature=0.8,
        top_p=0.8
        )
    print(completion.model_dump_json())

if __name__ == '__main__':
    get_response()

返回结果

{
  "id": "chatcmpl-cb68d043-fc6d-9b3b-87d2-151e2a0f6ac4",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "当然可以,这里有一个简单的三菜合一的菜品建议:蔬菜炖豆腐。这道菜结合了萝卜、土豆和茄子,营养丰富,做法也相当简单:\n\n**材料:**\n1. 萝卜半个\n2. 土豆1个\n3. 茄子2个\n4. 嫩豆腐1块\n5. 大葱1根\n6. 生姜适量\n7. 大蒜2瓣\n8. 食用油适量\n9. 盐适量\n10. 料酒适量\n11. 鸡精或味精适量\n12. 清水适量\n\n**步骤:**\n1. 萝卜、土豆去皮切块,茄子洗净去蒂,切成滚刀块。大葱切段,生姜切片,大蒜切末。\n2. 豆腐切块,放入开水中焯水,捞出沥干备用,这样可以去腥并使豆腐更加嫩滑。\n3. 热锅凉油,放入葱姜蒜爆香。\n4. 放入土豆块,翻煎至微黄色,再加入萝卜块和茄子块,继续翻炒均匀。\n5. 加入料酒,翻炒均匀后,倒入足够的清水,水量要没过所有蔬菜。\n6. 煮沸后转小火,慢慢炖煮15-20分钟,让蔬菜充分吸收汤汁。\n7. 加入焯过水的豆腐,再次煮沸后转小火,盖上锅盖炖5分钟左右,让豆腐充分入味。\n8. 最后加入适量的盐和鸡精(或其他调味品),搅拌均匀,尝一下味道,根据需要调整。\n9. 关火,撒上一些葱花点缀,即可出锅。\n\n这道菜色彩丰富,营养均衡,是一道适合家常的健康菜肴。",
        "role": "assistant",
        "function_call": null,
        "tool_calls": null
      }
    }
  ],
  "created": 1721636832,
  "model": "qwen-turbo",
  "object": "chat.completion",
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 398,
    "prompt_tokens": 32,
    "total_tokens": 430
  }
}

curl

示例代码

curl --location "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-turbo",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "用萝卜、土豆、茄子做饭,给我个菜谱。"
        }
    ]
}'

返回结果

{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "当然可以,这里有一个简单的三菜合一的菜品建议——“蔬菜炖锅”。这道菜不仅营养丰富,而且烹饪过程简单,非常适合家庭晚餐或周末烹饪。以下是具体的步骤:\n\n**材料:**\n- 萝卜 1 根\n- 土豆 2 个\n- 茄子 2 个\n- 洋葱 半个\n- 大蒜 3 瓣\n- 香菇 5-6 朵(可选)\n- 番茄酱 2 汤匙\n- 盐 适量\n- 黑胡椒粉 适量\n- 橄榄油 适量\n- 清水 适量\n\n**步骤:**\n1. **准备食材:**萝卜切块,土豆去皮切块,茄子去蒂切滚刀块,洋葱切片,大蒜剁碎,香菇洗净切片(如果使用)。\n\n2. **预热锅子:**在锅中加入适量橄榄油,加热后放入洋葱和大蒜炒香。\n\n3. **加入蔬菜:**将土豆和萝卜块放入锅中,翻煎几分钟让它们表面微焦,这样可以增加口感。\n\n4. **加入茄子:**将茄子块加入锅中,继续翻煎至所有蔬菜都稍微软化。\n\n5. **调入番茄酱:**倒入番茄酱,轻轻搅拌均匀,让蔬菜充分吸收番茄酱的味道。\n\n6. **加水:**加入足够的清水,水量要没过蔬菜,大火烧开后转小火慢慢炖煮,盖上锅盖。\n\n7. **调味:**根据个人口味添加盐和黑胡椒粉调味,炖煮约20-30分钟,直到蔬菜熟透且汤汁浓郁。\n\n8. **出锅:**最后撒上香菇片(如果使用),再炖煮5分钟左右即可出锅。\n\n这道“蔬菜炖锅”既美味又健康,你可以根据自己的口味调整食材和调料,例如添加一些香料如迷迭香或百里香来提升风味。享受你的美食!"
      },
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null
    }
  ],
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 32,
    "completion_tokens": 441,
    "total_tokens": 473
  },
  "created": 1721636938,
  "system_fingerprint": null,
  "model": "qwen-turbo",
  "id": "chatcmpl-31cc1e14-7ab4-9fd5-b831-57d2ee25f4bb"
}

DashScope

您可以通过DashScope SDK或HTTP方式调用通义千问模型。

Python

示例代码

import random
from http import HTTPStatus
# 建议dashscope SDK 的版本 >= 1.14.0
from dashscope import Generation


def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}]
    response = Generation.call(model="qwen-turbo",
                               messages=messages,
                               # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
                               seed=random.randint(1, 10000),
                               temperature=0.8,
                               top_p=0.8,
                               top_k=50,
                               # 将输出设置为"message"格式
                               result_format='message')
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))


if __name__ == '__main__':
    call_with_messages()

返回结果

{
  "status_code": 200,
  "request_id": "8925018f-3ab3-9da9-a783-4866e4254e9a",
  "code": "",
  "message": "",
  "output": {
    "text": null,
    "finish_reason": null,
    "choices": [
      {
        "finish_reason": "stop",
        "message": {
          "role": "assistant",
          "content": "当然可以,这里有一个简单的蔬菜炖煮菜谱,名为\"三蔬炖豆腐\",你可以试试看:\n\n**材料:**\n1. 萝卜半个\n2. 土豆一个\n3. 茄子一个\n4. 嫩豆腐一块\n5. 大葱适量\n6. 生姜适量\n7. 大蒜瓣适量\n8. 食盐适量\n9. 食用油适量\n10. 料酒适量\n11. 高汤或者清水适量\n12. 白胡椒粉适量(可选)\n\n**步骤:**\n1. 萝卜、土豆和茄子清洗干净,去皮切块。注意土豆要先切块后浸泡在水中防止氧化变色。\n2. 嫩豆腐切成厚片,大葱切段,生姜切片,大蒜瓣切末备用。\n3. 热锅凉油,放入葱姜蒜末炒香。\n4. 加入土豆块翻炒均匀,然后加入适量的料酒,继续翻炒让土豆吸收一些酒香。\n5. 放入萝卜块和茄子块,继续翻炒至蔬菜稍微软化。\n6. 倒入高汤或清水,水量没过蔬菜即可。如果用清水,可以适当加些鸡精或者鸡粉增加鲜味。\n7. 加入食盐调味,盖上锅盖,转小火慢慢炖煮,直到蔬菜熟软。\n8. 最后加入豆腐片,轻轻搅拌,让豆腐吸收汤汁的味道。如果喜欢,可以撒一些白胡椒粉增添风味。\n9. 炖煮几分钟,让豆腐充分入味后,就可以出锅了。\n\n这道菜营养丰富,颜色搭配也很好看,非常适合素食者或者想吃清淡健康菜肴的人。Enjoy your meal!"
        }
      }
    ]
  },
  "usage": {
    "input_tokens": 32,
    "output_tokens": 391,
    "total_tokens": 423
  }
}

Java

示例代码

// Copyright (c) Alibaba, Inc. and its affiliates.
// 建议dashscope SDK的版本 >= 2.12.0
import java.util.Arrays;
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;

public class Main {

    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = 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("用萝卜、土豆、茄子做饭,给我个菜谱。")
                .build();

        GenerationParam param = GenerationParam.builder()
                .model("qwen-turbo")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topK(50)
                .temperature(0.8f)
                .topP(0.8)
                .seed(1234)
                .build();

        return gen.call(param);
    }

    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println(JsonUtils.toJson(result));
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // 使用日志框架记录异常信息
            // Logger.error("An error occurred while calling the generation service", e);
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        System.exit(0);
    }
}

返回结果

{
  "requestId": "02edfe6d-26ce-9bed-a30e-f61803bcbaea",
  "usage": {
    "input_tokens": 32,
    "output_tokens": 398,
    "total_tokens": 430
  },
  "output": {
    "choices": [
      {
        "finish_reason": "stop",
        "message": {
          "role": "assistant",
          "content": "当然可以,这里有一个简单的三菜合一的菜品建议:蔬菜炖豆腐。这道菜结合了萝卜、土豆和茄子,营养丰富,口感也很好。以下是制作步骤:\n\n**材料:**\n1. 萝卜半个\n2. 土豆1个\n3. 茄子2个\n4. 嫩豆腐1块\n5. 大葱1根\n6. 生姜适量\n7. 大蒜3瓣\n8. 食用油适量\n9. 盐适量\n10. 料酒适量\n11. 鸡精或味精少许\n12. 清水适量\n\n**步骤:**\n1. 萝卜去皮切块,土豆去皮切滚刀块,茄子去蒂切段,豆腐切块备用。\n2. 大葱切段,生姜切片,大蒜切末。\n3. 热锅凉油,放入葱姜蒜爆香。\n4. 加入切好的萝卜和土豆,翻炒均匀,让它们吸收一些油分,这样煮出来的蔬菜会更甜。\n5. 当土豆边缘开始微微变软时,加入茄子,继续翻炒均匀。\n6. 倒入适量料酒,略煮一会儿去去腥味。\n7. 加入清水,水量大约没过蔬菜,大火烧开后转小火慢慢炖煮,让蔬菜充分熟透并吸收汤汁。\n8. 待蔬菜差不多软烂时,加入豆腐,轻轻搅拌以免破坏豆腐形状。\n9. 炖煮5分钟左右,让豆腐充分入味。\n10. 最后加盐调味,撒上一点鸡精或味精提鲜,即可出锅。\n\n这道菜可以根据个人口味调整调料的量,如果喜欢更浓郁的汤底,可以适当增加一些酱油或者番茄酱。希望你会喜欢!"
        }
      }
    ]
  }
}

curl

示例代码

curl --location "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-turbo",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "用萝卜、土豆、茄子做饭,给我个菜谱。"
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

返回结果

{
  "output": {
    "choices": [
      {
        "finish_reason": "stop",
        "message": {
          "role": "assistant",
          "content": "当然可以,这里有一个简单的三菜合一的菜品建议——“蔬菜炖锅”。这道菜不仅营养丰富,而且烹饪过程简单,非常适合家庭晚餐或周末烹饪。以下是具体的步骤:\n\n**材料:**\n- 萝卜 1 根\n- 土豆 2 个\n- 茄子 2 个\n- 洋葱 半个\n- 大蒜 3 瓣\n- 香菇 5-6 朵(可选)\n- 番茄酱 2 汤匙\n- 盐 适量\n- 黑胡椒粉 适量\n- 橄榄油 适量\n- 清水 适量\n\n**步骤:**\n1. **准备食材:**萝卜切块,土豆去皮切块,茄子去蒂切滚刀块,洋葱切片,大蒜剁碎,香菇洗净切片(如果使用)。\n\n2. **预热锅子:**在锅中加入适量橄榄油,加热后放入洋葱和大蒜炒香。\n\n3. **加入蔬菜:**将土豆和萝卜块放入锅中,翻煎几分钟让它们表面微焦,这样可以增加口感。\n\n4. **加入茄子:**将茄子块加入锅中,继续翻煎至所有蔬菜都稍微软化。\n\n5. **调入番茄酱:**倒入番茄酱,轻轻搅拌均匀,让蔬菜充分吸收番茄酱的味道。\n\n6. **加水:**加入足够的清水,水量要没过蔬菜,大火烧开后转小火慢慢炖煮,盖上锅盖。\n\n7. **调味:**根据个人口味添加盐和黑胡椒粉调味,炖煮约20-30分钟,直到蔬菜熟透且汤汁浓郁。\n\n8. **出锅:**最后撒上香菇片(如果使用),再炖煮5分钟左右即可出锅。\n\n这道“蔬菜炖锅”既美味又健康,你可以根据自己的口味调整食材和调料,例如添加一些香料如迷迭香或百里香提升风味。享受你的美食时间吧!"
        }
      }
    ]
  },
  "usage": {
    "total_tokens": 474,
    "output_tokens": 442,
    "input_tokens": 32
  },
  "request_id": "2e03b68f-2636-9227-a223-9c623c47c23e"
}

接收流式输出

上述代码的会在整体文本生成完成后,一次性返回所有输出结果。小明修改了代码,让大模型一边生成一边输出,通过流式输出的方式尽快的将中间结果显示在屏幕上。

OpenAI兼容

您可以通过OpenAI SDK或OpenAI兼容的HTTP方式调用通义千问模型,体验流式输出的功能。

Python

示例代码

from openai import OpenAI
import os

def get_response():
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    )
    completion = client.chat.completions.create(
        model="qwen-turbo",
        messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
                  {'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}],
        stream=True,
        # 可选,配置以后会在流式输出的最后一行展示token使用信息
        stream_options={"include_usage": True}
        )
    for chunk in completion:
        print(chunk.model_dump_json())

if __name__ == '__main__':
    get_response()

返回结果

{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"","function_call":null,"role":"assistant","tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"当然","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"可以","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":",","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"这里有一个简单的三菜","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"合一的菜品建议——“蔬菜炖","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"锅”。这道菜不仅营养丰富","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":",而且烹饪过程简单,非常适合家庭","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"晚餐或周末烹饪。\n\n**材料:","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"**\n- 萝卜半个\n","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"- 土豆2个\n-","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 茄子2个\n-","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 洋葱1/2个","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"\n- 大蒜3瓣\n","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"- 鸡汤或蔬菜汤","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 4杯\n- 番","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"茄酱 2大勺\n-","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 橄榄油 2大","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"勺\n- 盐适量\n-","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 黑胡椒粉适量\n-","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 百里香或者迷迭香","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"少许(可选)\n\n**步骤:","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"**\n1. **准备食材:**","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 萝卜、土豆和茄子","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"洗净去皮,切成块状;","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"洋葱和大蒜剁碎备用。\n\n2","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":". **预热锅子:**","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 在锅中加入橄榄油,中","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"火加热。\n\n3. **炒香","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"洋葱和大蒜:** 当油热","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"后,放入洋葱和大蒜炒至","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"微黄色,散发出香味。\n\n4","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":". **加入蔬菜:** 加入","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"切好的萝卜、土豆和茄子,","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"翻煎均匀,让蔬菜表面稍微","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"焦香,这样能提升口感。\n\n","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"5. **调入调料:**","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" 倒入番茄酱,加入","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"鸡汤或蔬菜汤,搅拌均匀。","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"如果喜欢的话,可以撒上一些","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"百里香或迷迭香增加","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"风味。\n\n6. **煮炖:","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"** 盖上锅盖,转","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"小火慢炖20-2","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"5分钟,直到蔬菜变得软烂","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":",汤汁浓稠。\n\n7.","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":" **调味:** 根据口味","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"加盐和黑胡椒粉调味","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":",最后尝一下味道,根据需要","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"再做调整。\n\n8. **出","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"锅:** 关火,让菜肴","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"稍微冷却几分钟,然后就可以享用了","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"。\n\n这道\"蔬菜炖锅\"","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"既健康又美味,适合搭配米饭","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"或者面包食用,是一道家常","function_call":null,"role":null,"tool_calls":null},"finish_reason":null,"index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[{"delta":{"content":"的好菜。","function_call":null,"role":null,"tool_calls":null},"finish_reason":"stop","index":0,"logprobs":null}],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":null}
{"id":"chatcmpl-7de61ea7-afba-9baf-8708-6bb9a197a35d","choices":[],"created":1721637291,"model":"qwen-turbo","object":"chat.completion.chunk","service_tier":null,"system_fingerprint":null,"usage":{"completion_tokens":411,"prompt_tokens":32,"total_tokens":443}}

curl

示例代码

curl --location "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--data '{
    "model": "qwen-turbo",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "用萝卜、土豆、茄子做饭,给我个菜谱。"
        }
    ],
    "stream":true
}'

返回结果

data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"finish_reason":null,"delta":{"content":"当然"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"可以"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":","},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"这里有一个简单的三菜"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"合一的菜品建议——“蔬菜炖"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"锅”。这道菜不仅营养丰富"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":",而且烹饪过程简单,非常适合家庭"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"晚餐或周末烹饪。以下是具体的步骤"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":":\n\n**材料:**\n- "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"萝卜 1 根\n-"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":" 土豆 2 个\n"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"- 茄子 2 "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"个\n- 洋葱 "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"半个\n- 大蒜 "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"3 瓣\n- "},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"香菇 5-6 朵"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"(可选)\n- 番"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"茄酱 2 汤匙"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"\n- 盐 适量\n-"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":" 黑胡椒粉 适量\n"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"- 橄榄油 适量"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"\n- 清水 适量\n\n"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"**步骤:**\n1. **准备"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"食材:**萝卜切块,土豆"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"去皮切块,茄子去蒂"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"切滚刀块,洋葱切片"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":",大蒜剁碎,香菇洗净切"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"片(如果使用)。\n\n2."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":" **预热锅子:**在"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"锅中加入适量橄榄油,加热"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"后放入洋葱和大蒜炒香。\n\n"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"3. **加入蔬菜:**将"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"土豆和萝卜块放入锅中,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"翻煎几分钟让它们表面微焦"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":",这样可以增加口感。\n\n4."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":" **加入茄子:**将茄子块"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"加入锅中,继续翻煎至"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"所有蔬菜都稍微软化。\n\n5"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":". **调入番茄酱:**"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"倒入番茄酱,轻轻搅拌均匀,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"让蔬菜充分吸收番茄酱的味道。\n\n"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"6. **加水:**加入"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"足够的清水,水量要没过蔬菜"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":",大火烧开后转小火"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"慢慢炖煮,盖上锅盖"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"。\n\n7. **调味:**根据"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"个人口味添加盐和黑胡椒"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"粉调味,炖煮约20"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"-30分钟,直到蔬菜熟"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"透且汤汁浓郁。\n\n8."},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":" **出锅:**最后撒上"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"香菇片(如果使用),再炖"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"煮5分钟左右即可出锅。\n\n这"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"道“蔬菜炖锅”既美味"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"又健康,你可以根据自己的口味调整"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"食材和调料,例如添加一些香"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"料如迷迭香或百里"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"香来提升风味。享受你的美食"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: {"choices":[{"delta":{"content":"!"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1721637415,"system_fingerprint":null,"model":"qwen-turbo","id":"chatcmpl-edcf5357-8253-92b3-85d2-a91da241e108"}

data: [DONE]

DashScope

您可以通过DashScope SDK或HTTP方式调用通义千问模型,体验流式输出的功能。

Python

示例代码

from http import HTTPStatus
from dashscope import Generation


def call_with_stream():
    messages = [
        {'role':'system','content':'you are a helpful assistant'},
        {'role': 'user','content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}
        ]
    responses = Generation.call(
        model="qwen-turbo",
        messages=messages,
        # 设置输出为'message'格式
        result_format='message',
        # 设置输出方式为流式输出
        stream=True,
        # 增量式流式输出
        incremental_output=True
        )
    for response in responses:
        if response.status_code == HTTPStatus.OK:
            print(response)
        else:
            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                response.request_id, response.status_code,
                response.code, response.message
            ))

if __name__ == '__main__':
    call_with_stream()

返回结果

{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "当然"}}]}, "usage": {"input_tokens": 31, "output_tokens": 1, "total_tokens": 32}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "可以"}}]}, "usage": {"input_tokens": 31, "output_tokens": 2, "total_tokens": 33}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ","}}]}, "usage": {"input_tokens": 31, "output_tokens": 3, "total_tokens": 34}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "这里有一个简单的三菜"}}]}, "usage": {"input_tokens": 31, "output_tokens": 8, "total_tokens": 39}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "一汤的菜谱,使用萝卜"}}]}, "usage": {"input_tokens": 31, "output_tokens": 16, "total_tokens": 47}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "、土豆和茄子:\n\n**菜品:"}}]}, "usage": {"input_tokens": 31, "output_tokens": 24, "total_tokens": 55}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "红烧萝卜土豆**\n1. "}}]}, "usage": {"input_tokens": 31, "output_tokens": 32, "total_tokens": 63}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "材料:萝卜半个,土豆2"}}]}, "usage": {"input_tokens": 31, "output_tokens": 40, "total_tokens": 71}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "个,葱姜适量,生抽"}}]}, "usage": {"input_tokens": 31, "output_tokens": 48, "total_tokens": 79}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "、老抽、糖、盐各"}}]}, "usage": {"input_tokens": 31, "output_tokens": 56, "total_tokens": 87}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "适量,清水适量。\n2. "}}]}, "usage": {"input_tokens": 31, "output_tokens": 64, "total_tokens": 95}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "做法:\n   - "}}]}, "usage": {"input_tokens": 31, "output_tokens": 71, "total_tokens": 102}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "萝卜和土豆去皮切块"}}]}, "usage": {"input_tokens": 31, "output_tokens": 80, "total_tokens": 111}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",葱切段,姜切片"}}]}, "usage": {"input_tokens": 31, "output_tokens": 88, "total_tokens": 119}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "备用。\n   - 热锅"}}]}, "usage": {"input_tokens": 31, "output_tokens": 96, "total_tokens": 127}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "凉油,放入糖小火炒"}}]}, "usage": {"input_tokens": 31, "output_tokens": 104, "total_tokens": 135}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "至红色,加入葱姜炒香"}}]}, "usage": {"input_tokens": 31, "output_tokens": 112, "total_tokens": 143}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "。\n   - 加入萝卜和土豆"}}]}, "usage": {"input_tokens": 31, "output_tokens": 120, "total_tokens": 151}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "块翻炒均匀,加入生抽"}}]}, "usage": {"input_tokens": 31, "output_tokens": 128, "total_tokens": 159}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "、老抽上色。\n   -"}}]}, "usage": {"input_tokens": 31, "output_tokens": 136, "total_tokens": 167}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " 倒入足够的清水,水量"}}]}, "usage": {"input_tokens": 31, "output_tokens": 144, "total_tokens": 175}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "要没过食材,大火烧开"}}]}, "usage": {"input_tokens": 31, "output_tokens": 152, "total_tokens": 183}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "后转中小火慢炖30"}}]}, "usage": {"input_tokens": 31, "output_tokens": 160, "total_tokens": 191}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "分钟左右,直到萝卜和土豆熟软"}}]}, "usage": {"input_tokens": 31, "output_tokens": 168, "total_tokens": 199}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "。\n   - 最后加盐调味"}}]}, "usage": {"input_tokens": 31, "output_tokens": 176, "total_tokens": 207}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",收汁即可。\n\n**菜品:"}}]}, "usage": {"input_tokens": 31, "output_tokens": 184, "total_tokens": 215}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "茄汁土豆泥**\n1. "}}]}, "usage": {"input_tokens": 31, "output_tokens": 192, "total_tokens": 223}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "材料:土豆2个,茄子"}}]}, "usage": {"input_tokens": 31, "output_tokens": 200, "total_tokens": 231}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "1个,洋葱半个,蒜瓣"}}]}, "usage": {"input_tokens": 31, "output_tokens": 208, "total_tokens": 239}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "2-3个,牛奶或植物"}}]}, "usage": {"input_tokens": 31, "output_tokens": 216, "total_tokens": 247}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "奶适量,盐、黑胡椒"}}]}, "usage": {"input_tokens": 31, "output_tokens": 224, "total_tokens": 255}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "粉适量。\n2. 做"}}]}, "usage": {"input_tokens": 31, "output_tokens": 232, "total_tokens": 263}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "法:\n   - 土豆和"}}]}, "usage": {"input_tokens": 31, "output_tokens": 240, "total_tokens": 271}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "茄子切块,洋葱切末,"}}]}, "usage": {"input_tokens": 31, "output_tokens": 248, "total_tokens": 279}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "蒜瓣切片备用。\n   -"}}]}, "usage": {"input_tokens": 31, "output_tokens": 256, "total_tokens": 287}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " 先将土豆煮熟,捣"}}]}, "usage": {"input_tokens": 31, "output_tokens": 264, "total_tokens": 295}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "成泥,保留一些水分,备用"}}]}, "usage": {"input_tokens": 31, "output_tokens": 272, "total_tokens": 303}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "。\n   - 炒锅加油"}}]}, "usage": {"input_tokens": 31, "output_tokens": 280, "total_tokens": 311}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",放入洋葱末和蒜片炒"}}]}, "usage": {"input_tokens": 31, "output_tokens": 288, "total_tokens": 319}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "香,加入茄子块煎至两"}}]}, "usage": {"input_tokens": 31, "output_tokens": 296, "total_tokens": 327}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "面微黄。\n   - 加入"}}]}, "usage": {"input_tokens": 31, "output_tokens": 304, "total_tokens": 335}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "适量水,煮至茄子变软"}}]}, "usage": {"input_tokens": 31, "output_tokens": 312, "total_tokens": 343}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",然后倒入土豆泥中,加入"}}]}, "usage": {"input_tokens": 31, "output_tokens": 320, "total_tokens": 351}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "牛奶搅拌均匀。\n   - 最后"}}]}, "usage": {"input_tokens": 31, "output_tokens": 328, "total_tokens": 359}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "加盐、黑胡椒粉调味"}}]}, "usage": {"input_tokens": 31, "output_tokens": 336, "total_tokens": 367}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",再小火煮一会儿让味道"}}]}, "usage": {"input_tokens": 31, "output_tokens": 344, "total_tokens": 375}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "融合。\n\n**汤品:萝卜土豆"}}]}, "usage": {"input_tokens": 31, "output_tokens": 352, "total_tokens": 383}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "炖排骨汤**\n1. 材"}}]}, "usage": {"input_tokens": 31, "output_tokens": 360, "total_tokens": 391}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "料:排骨500克,"}}]}, "usage": {"input_tokens": 31, "output_tokens": 368, "total_tokens": 399}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "萝卜1个,土豆2个,"}}]}, "usage": {"input_tokens": 31, "output_tokens": 376, "total_tokens": 407}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "生姜2片,料酒、盐"}}]}, "usage": {"input_tokens": 31, "output_tokens": 384, "total_tokens": 415}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "适量,清水适量。\n2. "}}]}, "usage": {"input_tokens": 31, "output_tokens": 392, "total_tokens": 423}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "做法:\n   - 排"}}]}, "usage": {"input_tokens": 31, "output_tokens": 400, "total_tokens": 431}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "骨洗净,焯水去血水"}}]}, "usage": {"input_tokens": 31, "output_tokens": 408, "total_tokens": 439}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",萝卜和土豆切块。\n  "}}]}, "usage": {"input_tokens": 31, "output_tokens": 416, "total_tokens": 447}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " - 锅中加水,放入"}}]}, "usage": {"input_tokens": 31, "output_tokens": 424, "total_tokens": 455}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "排骨、姜片和料酒,"}}]}, "usage": {"input_tokens": 31, "output_tokens": 432, "total_tokens": 463}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "大火烧开后撇去浮沫"}}]}, "usage": {"input_tokens": 31, "output_tokens": 440, "total_tokens": 471}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",转小火炖煮1小时"}}]}, "usage": {"input_tokens": 31, "output_tokens": 448, "total_tokens": 479}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "。\n   - 加入萝卜和土豆"}}]}, "usage": {"input_tokens": 31, "output_tokens": 456, "total_tokens": 487}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "块,继续炖煮30分钟"}}]}, "usage": {"input_tokens": 31, "output_tokens": 464, "total_tokens": 495}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": ",直至萝卜土豆熟透。\n  "}}]}, "usage": {"input_tokens": 31, "output_tokens": 472, "total_tokens": 503}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": " - 最后加盐调味即可。\n\n"}}]}, "usage": {"input_tokens": 31, "output_tokens": 480, "total_tokens": 511}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "这是一份基础的组合,你可以"}}]}, "usage": {"input_tokens": 31, "output_tokens": 488, "total_tokens": 519}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": "根据个人口味调整调料和烹饪时间"}}]}, "usage": {"input_tokens": 31, "output_tokens": 496, "total_tokens": 527}}
{"status_code": 200, "request_id": "f4b247aa-871f-9819-89e0-89b6cc8c7108", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": "。祝你做菜愉快!"}}]}, "usage": {"input_tokens": 31, "output_tokens": 503, "total_tokens": 534}}

Java

示例代码

// Copyright (c) Alibaba, Inc. and its affiliates.

import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.lang.System;

public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    private static void handleGenerationResult(GenerationResult message) {
        System.out.println(JsonUtils.toJson(message));
    }
    public static void streamCallWithMessage(Generation gen, Message userMsg)
            throws NoApiKeyException, ApiException, InputRequiredException {
        GenerationParam param = buildGenerationParam(userMsg);
        Flowable<GenerationResult> result = gen.streamCall(param);
        result.blockingForEach(message -> handleGenerationResult(message));
    }
    private static GenerationParam buildGenerationParam(Message userMsg) {
        return GenerationParam.builder()
                .model("qwen-turbo")
                .messages(Arrays.asList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .incrementalOutput(true)
                .build();
    }
    public static void main(String[] args) {
        try {
            Generation gen = new Generation();
            Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭,给我个菜谱。").build();
            streamCallWithMessage(gen, userMsg);
        } catch (ApiException | NoApiKeyException | InputRequiredException  e) {
            logger.error("An exception occurred: {}", e.getMessage());
        }
    }
}

返回结果

{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":1,"total_tokens":22},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"当然"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":2,"total_tokens":23},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"可以"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":3,"total_tokens":24},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":","}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":8,"total_tokens":29},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"这里有一个简单的三菜"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":16,"total_tokens":37},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"合一的菜品建议——“蔬菜炖"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":24,"total_tokens":45},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"锅”。这道菜不仅营养丰富"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":32,"total_tokens":53},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":",而且烹饪过程简单,非常适合家庭"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":40,"total_tokens":61},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"晚餐或周末烹饪。以下是具体的步骤"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":47,"total_tokens":68},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":":\n\n**材料:**\n- "}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":56,"total_tokens":77},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"萝卜 1 根\n-"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":64,"total_tokens":85},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":" 土豆 2 个\n"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":72,"total_tokens":93},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"- 茄子 2 "}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":80,"total_tokens":101},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"个\n- 洋葱 "}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":88,"total_tokens":109},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"半个\n- 大蒜 "}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":95,"total_tokens":116},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"3 瓣\n- "}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":104,"total_tokens":125},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"香菇 5-6 朵"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":112,"total_tokens":133},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"(可选)\n- 番"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":120,"total_tokens":141},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"茄酱 2 汤匙"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":128,"total_tokens":149},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"\n- 盐 适量\n-"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":136,"total_tokens":157},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":" 黑胡椒粉 适量\n"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":144,"total_tokens":165},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"- 橄榄油 适量"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":152,"total_tokens":173},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"\n- 清水 适量\n\n"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":160,"total_tokens":181},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"**步骤:**\n1. **准备"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":168,"total_tokens":189},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"食材:**萝卜切块,土豆"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":176,"total_tokens":197},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"去皮切块,茄子去蒂"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":184,"total_tokens":205},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"切滚刀块,洋葱切片"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":192,"total_tokens":213},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":",大蒜剁碎,香菇洗净切"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":200,"total_tokens":221},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"片(如果使用)。\n\n2."}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":208,"total_tokens":229},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":" **预热锅子:**在"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":216,"total_tokens":237},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"锅中加入适量橄榄油,加热"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":224,"total_tokens":245},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"后放入洋葱和大蒜炒香。\n\n"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":232,"total_tokens":253},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"3. **加入蔬菜:**将"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":240,"total_tokens":261},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"土豆和萝卜块放入锅中,"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":248,"total_tokens":269},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"翻煎几分钟让它们表面微焦"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":256,"total_tokens":277},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":",这样可以增加口感。\n\n4."}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":264,"total_tokens":285},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":" **加入茄子:**将茄子块"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":272,"total_tokens":293},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"加入锅中,继续翻煎至"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":280,"total_tokens":301},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"所有蔬菜都稍微软化。\n\n5"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":288,"total_tokens":309},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":". **调入番茄酱:**"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":296,"total_tokens":317},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"倒入番茄酱,轻轻搅拌均匀,"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":304,"total_tokens":325},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"让蔬菜充分吸收番茄酱的味道。\n\n"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":312,"total_tokens":333},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"6. **加水:**加入"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":320,"total_tokens":341},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"足够的清水,水量要没过蔬菜"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":328,"total_tokens":349},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":",大火烧开后转小火"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":336,"total_tokens":357},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"慢慢炖煮,盖上锅盖"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":344,"total_tokens":365},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"。\n\n7. **调味:**根据"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":352,"total_tokens":373},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"个人口味添加盐和黑胡椒"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":360,"total_tokens":381},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"粉调味,炖煮约20"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":368,"total_tokens":389},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"-30分钟,直到蔬菜熟"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":376,"total_tokens":397},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"透且汤汁浓郁。\n\n8."}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":384,"total_tokens":405},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":" **出锅:**最后撒上"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":392,"total_tokens":413},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"香菇片(如果使用),再炖"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":400,"total_tokens":421},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"煮5分钟左右即可出锅。\n\n这"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":408,"total_tokens":429},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"道“蔬菜炖锅”既美味"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":416,"total_tokens":437},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"又健康,你可以根据自己的口味调整"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":424,"total_tokens":445},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"食材和调料,例如添加一些香"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":432,"total_tokens":453},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"料如迷迭香或百里"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":440,"total_tokens":461},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":"香来提升风味。享受你的美食"}}]}}
{"requestId":"b858aa05-4d67-9132-9d88-022d714374d5","usage":{"input_tokens":21,"output_tokens":441,"total_tokens":462},"output":{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":"!"}}]}}

curl

示例代码

curl --location "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header "Content-Type: application/json" \
--header "X-DashScope-SSE: enable" \
--data '{
    "model": "qwen-turbo",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "用萝卜、土豆、茄子做饭,给我个菜谱。"
            }
        ]
    },
    "parameters": {
        "result_format": "message",
        "incremental_output":true
    }
}'

返回结果

id:1
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"当然","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":33,"input_tokens":32,"output_tokens":1},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:2
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"可以","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":34,"input_tokens":32,"output_tokens":2},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:3
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":",","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":35,"input_tokens":32,"output_tokens":3},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:4
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"这里有一个简单的三菜","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":40,"input_tokens":32,"output_tokens":8},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:5
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"合一的菜品建议——“蔬菜炖","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":48,"input_tokens":32,"output_tokens":16},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:6
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"锅”。这道菜不仅营养丰富","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":56,"input_tokens":32,"output_tokens":24},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:7
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":",而且烹饪过程简单,非常适合家庭","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":64,"input_tokens":32,"output_tokens":32},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:8
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"晚餐或周末烹饪。以下是具体的步骤","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":72,"input_tokens":32,"output_tokens":40},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:9
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":":\n\n**材料:**\n- ","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":79,"input_tokens":32,"output_tokens":47},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:10
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"萝卜 1 根\n-","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":88,"input_tokens":32,"output_tokens":56},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:11
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" 土豆 2 个\n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":96,"input_tokens":32,"output_tokens":64},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:12
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"- 茄子 2 ","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":104,"input_tokens":32,"output_tokens":72},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:13
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"个\n- 洋葱 ","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":112,"input_tokens":32,"output_tokens":80},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:14
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"半个\n- 大蒜 ","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":120,"input_tokens":32,"output_tokens":88},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:15
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"3 瓣\n- ","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":127,"input_tokens":32,"output_tokens":95},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:16
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"香菇 5-6 朵","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":136,"input_tokens":32,"output_tokens":104},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:17
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"(可选)\n- 番","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":144,"input_tokens":32,"output_tokens":112},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:18
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"茄酱 2 汤匙","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":152,"input_tokens":32,"output_tokens":120},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:19
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"\n- 盐 适量\n-","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":160,"input_tokens":32,"output_tokens":128},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:20
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" 黑胡椒粉 适量\n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":168,"input_tokens":32,"output_tokens":136},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:21
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"- 橄榄油 适量","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":176,"input_tokens":32,"output_tokens":144},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:22
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"\n- 清水 适量\n\n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":184,"input_tokens":32,"output_tokens":152},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:23
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"**步骤:**\n1. **准备","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":192,"input_tokens":32,"output_tokens":160},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:24
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"食材:**萝卜切块,土豆","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":200,"input_tokens":32,"output_tokens":168},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:25
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"去皮切块,茄子去蒂","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":208,"input_tokens":32,"output_tokens":176},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:26
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"切滚刀块,洋葱切片","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":216,"input_tokens":32,"output_tokens":184},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:27
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":",大蒜剁碎,香菇洗净切","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":224,"input_tokens":32,"output_tokens":192},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:28
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"片(如果使用)。\n\n2.","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":232,"input_tokens":32,"output_tokens":200},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:29
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" **预热锅子:**在","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":240,"input_tokens":32,"output_tokens":208},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:30
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"锅中加入适量橄榄油,加热","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":248,"input_tokens":32,"output_tokens":216},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:31
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"后放入洋葱和大蒜炒香。\n\n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":256,"input_tokens":32,"output_tokens":224},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:32
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"3. **加入蔬菜:**将","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":264,"input_tokens":32,"output_tokens":232},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:33
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"土豆和萝卜块放入锅中,","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":272,"input_tokens":32,"output_tokens":240},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:34
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"翻煎几分钟让它们表面微焦","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":280,"input_tokens":32,"output_tokens":248},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:35
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":",这样可以增加口感。\n\n4.","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":288,"input_tokens":32,"output_tokens":256},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:36
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" **加入茄子:**将茄子块","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":296,"input_tokens":32,"output_tokens":264},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:37
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"加入锅中,继续翻煎至","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":304,"input_tokens":32,"output_tokens":272},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:38
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"所有蔬菜都稍微软化。\n\n5","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":312,"input_tokens":32,"output_tokens":280},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:39
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":". **调入番茄酱:**","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":320,"input_tokens":32,"output_tokens":288},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:40
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"倒入番茄酱,轻轻搅拌均匀,","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":328,"input_tokens":32,"output_tokens":296},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:41
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"让蔬菜充分吸收番茄酱的味道。\n\n","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":336,"input_tokens":32,"output_tokens":304},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:42
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"6. **加水:**加入","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":344,"input_tokens":32,"output_tokens":312},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:43
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"足够的清水,水量要没过蔬菜","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":352,"input_tokens":32,"output_tokens":320},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:44
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":",大火烧开后转小火","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":360,"input_tokens":32,"output_tokens":328},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:45
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"慢慢炖煮,盖上锅盖","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":368,"input_tokens":32,"output_tokens":336},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:46
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"。\n\n7. **调味:**根据","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":376,"input_tokens":32,"output_tokens":344},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:47
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"个人口味添加盐和黑胡椒","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":384,"input_tokens":32,"output_tokens":352},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:48
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"粉调味,炖煮约20","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":392,"input_tokens":32,"output_tokens":360},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:49
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"-30分钟,直到蔬菜熟","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":400,"input_tokens":32,"output_tokens":368},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:50
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"透且汤汁浓郁。\n\n8.","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":408,"input_tokens":32,"output_tokens":376},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:51
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":" **出锅:**最后撒上","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":416,"input_tokens":32,"output_tokens":384},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:52
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"香菇片(如果使用),再炖","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":424,"input_tokens":32,"output_tokens":392},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:53
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"煮5分钟左右即可出锅。\n\n这","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":432,"input_tokens":32,"output_tokens":400},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:54
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"道“蔬菜炖锅”既美味","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":440,"input_tokens":32,"output_tokens":408},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:55
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"又健康,你可以根据自己的口味调整","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":448,"input_tokens":32,"output_tokens":416},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:56
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"食材和调料,例如添加一些香","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":456,"input_tokens":32,"output_tokens":424},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:57
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"料如迷迭香或百里","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":464,"input_tokens":32,"output_tokens":432},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:58
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"香来提升风味。享受你的美食","role":"assistant"},"finish_reason":"null"}]},"usage":{"total_tokens":472,"input_tokens":32,"output_tokens":440},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

id:59
event:result
:HTTP_STATUS/200
data:{"output":{"choices":[{"message":{"content":"!","role":"assistant"},"finish_reason":"stop"}]},"usage":{"total_tokens":473,"input_tokens":32,"output_tokens":441},"request_id":"6438745a-694e-9d89-812a-fc4bc29eaf08"}

在屏幕上显示美观的格式

上面的代码可以接收流式输出,但打印在屏幕上却并不美观。小明希望让用户在屏幕上看到美观的结果。因此他修改了代码,让模型的回复美观地展示出来。

OpenAI兼容

from openai import OpenAI
import os

def get_response():
    client = OpenAI(
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    )
    completion = client.chat.completions.create(
        model="qwen-turbo",
        messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},
                  {'role': 'user', 'content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}],
        stream=True,
        # 可选,配置以后会在流式输出的最后一行展示token使用信息
        stream_options={"include_usage": True}
        )
    for chunk in completion:
        try:
            print(chunk.choices[0].delta.content, end="", flush=True)
        except:
            pass

if __name__ == '__main__':
    get_response()

DashScope

from http import HTTPStatus
from dashscope import Generation


def call_with_stream():
    messages = [
        {'role':'system','content':'you are a helpful assistant'},
        {'role': 'user','content': '用萝卜、土豆、茄子做饭,给我个菜谱。'}
        ]
    responses = Generation.call(
        model="qwen-turbo",
        messages=messages,
        # 设置输出为'message'格式
        result_format='message',
        # 设置输出方式为流式输出
        stream=True,
        # 增量式流式输出
        incremental_output=True
        )
    for response in responses:
        if response.status_code == HTTPStatus.OK:
            print(response.output.choices[0].message.content,end="",flush=True)
        else:
            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                response.request_id, response.status_code,
                response.code, response.message
            ))

if __name__ == '__main__':
    call_with_stream()
// Copyright (c) Alibaba, Inc. and its affiliates.

import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.lang.System;

public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);
    private static void handleGenerationResult(GenerationResult message) {
        System.out.print(message.getOutput().getChoices().get(0).getMessage().getContent());
    }
    public static void streamCallWithMessage(Generation gen, Message userMsg)
            throws NoApiKeyException, ApiException, InputRequiredException {
        GenerationParam param = buildGenerationParam(userMsg);
        Flowable<GenerationResult> result = gen.streamCall(param);
        result.blockingForEach(message -> handleGenerationResult(message));
    }
    private static GenerationParam buildGenerationParam(Message userMsg) {
        return GenerationParam.builder()
                .model("qwen-turbo")
                .messages(Arrays.asList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .incrementalOutput(true)
                .build();
    }
    public static void main(String[] args) {
        try {
            Generation gen = new Generation();
            Message userMsg = Message.builder().role(Role.USER.getValue()).content("用萝卜、土豆、茄子做饭,给我个菜谱。").build();
            streamCallWithMessage(gen, userMsg);
            System.exit(0);
        } catch (ApiException | NoApiKeyException | InputRequiredException  e) {
            logger.error("An exception occurred: {}", e.getMessage());
        }
    }
}

最终,小明获得了满意的结果。

2024-07-22_17-13-55 (1)

了解更多

有关通义千问模型API的详细调用文档可前往通过API使用通义千问进行了解。