async_dashscope/operation/generation/
output.rs

1use std::{pin::Pin, str};
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use tokio_stream::Stream;
6
7use crate::{error::DashScopeError, operation::common::Usage};
8
9
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct Message {
12    /// 输出消息的内容。当使用qwen-vl或qwen-audio系列模型时为array,其余情况为string。
13    #[serde(rename = "content")]
14    pub content: String,
15
16    /// 输出消息的角色,固定为assistant。
17    #[serde(rename = "role")]
18    pub role: String,
19
20    // 思考内容
21    #[serde(rename = "reasoning_content")]
22    pub reasoning_content: Option<String>,
23
24    // 函数调用
25    pub function_call: Option<Value>,
26    #[serde(rename = "tool_calls")]
27    pub tool_calls: Option<Vec<ToolCall>>,
28}
29
30#[derive(Serialize, Deserialize, Debug, Clone)]
31pub struct ToolCall {
32    #[serde(rename = "id")]
33    pub id: String,
34
35    #[serde(rename = "type")]
36    pub type_: String,
37
38    #[serde(rename = "index")]
39    pub index: i32,
40
41    #[serde(rename = "function")]
42    pub function: Function,
43}
44
45#[derive(Serialize, Deserialize, Debug, Clone)]
46pub struct Function {
47    pub name: String,
48    pub arguments: Option<String>,
49}
50
51#[derive(Serialize, Deserialize, Debug, Clone)]
52pub struct Choices {
53    /// 有四种情况:
54    /// - 正在生成时为null;
55    /// - 因模型输出自然结束,或触发输入参数中的stop条件而结束时为stop;
56    /// - 因生成长度过长而结束为length;
57    /// - 因发生工具调用为tool_calls。
58    #[serde(rename = "finish_reason")]
59    pub finish_reason: Option<String>,
60
61    /// 模型输出的消息对象。
62    #[serde(rename = "message")]
63    pub message: Message,
64}
65#[derive(Serialize, Deserialize, Debug, Clone)]
66pub struct Output {
67    /// 模型的输出信息。当result_format为message时返回choices参数。
68    #[serde(rename = "choices")]
69    pub choices: Option<Vec<Choices>>,
70
71    /// 模型生成的回复。当设置输入参数result_format为text时将回复内容返回到该字段。
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub text: Option<String>,
74
75    /// 当设置输入参数result_format为text时该参数不为空。
76    /// 有四种情况:
77    /// - 正在生成时为null;
78    /// - 因模型输出自然结束,或触发输入参数中的stop条件而结束时为stop;
79    /// - 因生成长度过长而结束为length;
80    /// - 因发生工具调用为tool_calls。
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub finish_reason: Option<String>,
83
84    /// 联网搜索到的信息,在设置search_options参数后会返回该参数。
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub search_info: Option<SearchInfo>,
87}
88
89#[derive(Serialize, Deserialize, Debug, Clone)]
90pub struct SearchInfo {
91    #[serde(rename = "search_results")]
92    pub search_results: Vec<SearchResult>,
93}
94
95#[derive(Serialize, Deserialize, Debug, Clone)]
96pub struct SearchResult {
97    /// 搜索结果来源的网站名称。
98    pub site_name: String,
99    /// 来源网站的图标URL,如果没有图标则为空字符串。
100    pub icon: Option<String>,
101
102    /// 搜索结果的序号,表示该搜索结果在search_results中的索引。
103    pub index: i32,
104
105    /// 搜索结果的标题。
106    pub title: Option<String>,
107
108    /// 搜索结果的链接地址。
109    pub url: String,
110}
111
112#[derive(Serialize, Deserialize, Debug, Clone)]
113pub struct GenerationOutput {
114    /// 调用结果信息。
115    #[serde(rename = "output")]
116    pub output: Output,
117
118    /// 本次调用的唯一标识符。
119    #[serde(rename = "request_id")]
120    pub request_id: Option<String>,
121
122    /// 本次chat请求使用的token信息。
123    #[serde(rename = "usage")]
124    pub usage: Option<Usage>,
125}
126
127pub type GenerationOutputStream =
128    Pin<Box<dyn Stream<Item = Result<GenerationOutput, DashScopeError>> + Send>>;