async_dashscope/operation/
generation.rs

1use crate::{client::Client, error::DashScopeError, operation::validate::Validator};
2use crate::{error::Result, operation::validate::check_model_parameters};
3pub use output::*;
4pub use param::{
5    AssistantMessageBuilder, GenerationParam, GenerationParamBuilder, InputBuilder, MessageBuilder,
6    SystemMessageBuilder, ToolMessageBuilder, UserMessageBuilder,
7};
8
9mod output;
10mod param;
11
12const GENERATION_PATH: &str = "/services/aigc/text-generation/generation";
13
14pub struct Generation<'a> {
15    client: &'a Client,
16}
17
18impl<'a> Generation<'a> {
19    pub fn new(client: &'a Client) -> Self {
20        Self { client }
21    }
22
23    /// 异步调用生成服务
24    ///
25    /// 此函数用于当请求参数中的stream设置为false时,发送一次性生成请求
26    /// 如果stream参数为true,则会返回错误,提示用户使用call_stream方法
27    ///
28    /// # 参数
29    /// * `request`: 包含生成参数的请求对象
30    ///
31    /// # 返回
32    /// 返回生成输出的结果,如果请求配置了stream且为true,则返回错误
33    pub async fn call(&self, request: GenerationParam) -> Result<GenerationOutput> {
34        // 检查请求是否启用了流式生成,如果是,则返回错误
35        if request.stream == Some(true) {
36            return Err(DashScopeError::InvalidArgument(
37                "When stream is true, use Generation::call_stream".into(),
38            ));
39        }
40
41        // 检查参数
42        let validators = check_model_parameters(&request.model);
43        for valid in validators {
44            valid.validate(&request)?;
45        }
46
47        // 发送POST请求到生成服务,并等待结果
48        self.client.post(GENERATION_PATH, request).await
49    }
50
51    /// 异步调用生成流函数
52    ///
53    /// 此函数用于处理文本生成的流式请求。流式请求意味着响应会随着时间的推移逐步返回,
54    /// 而不是一次性返回所有内容。这对于需要实时处理生成内容的场景特别有用。
55    ///
56    /// # 参数
57    /// * `request`: 一个可变的 `GenerationParam` 类型对象,包含了生成文本所需的参数。
58    ///
59    /// # 返回
60    /// 返回一个 `Result` 类型,包含一个 `GenerationOutputStream` 对象,用于接收生成的文本流。
61    /// 如果 `request` 中的 `stream` 字段为 `Some(false)`,则返回一个 `DashScopeError::InvalidArgument` 错误,
62    /// 提示用户应使用 `Generation::call` 函数而不是 `call_stream`。
63    ///
64    /// # 错误处理
65    /// 如果 `request` 参数中的 `stream` 属性为 `Some(false)`,表示用户不希望使用流式处理,
66    /// 函数将返回一个错误,提示用户应使用非流式处理的 `call` 方法。
67    ///
68    /// # 注意
69    /// 该函数自动将 `request` 的 `stream` 属性设置为 `Some(true)`,确保总是以流式处理方式执行生成任务。
70    pub async fn call_stream(
71        &self,
72        mut request: GenerationParam,
73    ) -> Result<GenerationOutputStream> {
74        // 检查 `request` 中的 `stream` 属性,如果明确为 `false`,则返回错误
75        if request.stream == Some(false) {
76            return Err(DashScopeError::InvalidArgument(
77                "When stream is false, use Generation::call".into(),
78            ));
79        }
80
81        // 确保 `stream` 属性被设置为 `true`,即使它之前是 `None`
82        request.stream = Some(true);
83
84        // 检查参数(保持与 call 方法的一致性)
85        let validators = check_model_parameters(&request.model);
86        for valid in validators {
87            valid.validate(&request)?;
88        }
89
90        // 通过客户端发起 POST 请求,使用修改后的 `request` 对象,并等待异步响应
91        self.client.post_stream(GENERATION_PATH, request).await
92    }
93}