日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

快速入門

本文以通義千問大模型(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使用通義千問進行了解。