use anyhow::{Context as _, bail};
use futures::{
StreamExt as _,
future::BoxFuture,
stream::{self, BoxStream},
};
use crate::{
datatype::Value,
message::{
Delta as _, FinishReason, Message, MessageDeltaOutput, MessageOutput, Part, PartDelta,
PartDeltaFunction, PartFunction, PartImage, Role, TokenUsage,
},
model::{InferLangModel, LangModelOptions, api_error},
to_value,
tool::ToolDesc,
};
const API_URL: &str = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions";
#[derive(Clone)]
pub struct QwenApiModel {
model: String,
api_key: String,
}
impl std::fmt::Debug for QwenApiModel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("QwenApiModel")
.field("model", &self.model)
.field("api_key", &"<redacted>")
.finish()
}
}
impl QwenApiModel {
pub fn new(model: impl Into<String>, api_key: impl Into<String>) -> Self {
Self {
model: model.into(),
api_key: api_key.into(),
}
}
pub fn from_env(model: impl Into<String>) -> anyhow::Result<Self> {
let api_key = std::env::var("DASHSCOPE_API_KEY")
.ok()
.filter(|v| !v.trim().is_empty())
.context("DASHSCOPE_API_KEY is not set")?;
Ok(Self::new(model, api_key))
}
fn body(
&self,
messages: &[Message],
tools: &[ToolDesc],
options: &LangModelOptions,
) -> anyhow::Result<serde_json::Value> {
let mut system = messages
.iter()
.filter(|m| m.role == Role::System)
.flat_map(|m| m.contents.iter().filter_map(|p| p.as_text()))
.collect::<Vec<_>>()
.join("\n\n");
if let Some(schema) = &options.output_schema {
let schema: serde_json::Value = schema.clone().into();
if !system.is_empty() {
system.push_str("\n\n");
}
system.push_str(&format!(
"Reply with a single JSON object that matches this JSON schema, and nothing \
else:\n{schema}"
));
}
let mut chat = Vec::new();
if !system.is_empty() {
chat.push(to_value!({"role": "system", "content": system}));
}
chat.extend(chat_messages(messages)?);
let mut body = to_value!({
"model": self.model.as_str(),
"messages": chat,
"stream": true,
"stream_options": {"include_usage": true},
});
let obj = body.as_object_mut().unwrap();
if !tools.is_empty() {
let tools: Vec<Value> = tools
.iter()
.map(|tool| {
let mut function =
to_value!({"name": &tool.name, "parameters": tool.parameters.clone()});
if let Some(desc) = &tool.description {
function
.as_object_mut()
.unwrap()
.insert("description".into(), desc.into());
}
to_value!({"type": "function", "function": function})
})
.collect();
obj.insert("tools".into(), Value::Array(tools));
obj.insert("tool_choice".into(), "auto".into());
}
if options.thinking_effort.is_some() {
obj.insert("enable_thinking".into(), true.into());
}
if options.output_schema.is_some() {
obj.insert("response_format".into(), to_value!({"type": "json_object"}));
}
Ok(body.into())
}
}
impl InferLangModel for QwenApiModel {
fn infer(
&self,
messages: &[Message],
tools: &[ToolDesc],
options: &LangModelOptions,
) -> BoxFuture<'static, anyhow::Result<MessageOutput>> {
let mut deltas = self.infer_stream(messages, tools, options);
Box::pin(async move {
let mut acc = MessageDeltaOutput::new();
while let Some(delta) = deltas.next().await {
acc = acc.accumulate(delta?)?;
}
acc.finish()
})
}
fn infer_stream(
&self,
messages: &[Message],
tools: &[ToolDesc],
options: &LangModelOptions,
) -> BoxStream<'static, anyhow::Result<MessageDeltaOutput>> {
let body = match self.body(messages, tools, options) {
Ok(body) => body,
Err(e) => return Box::pin(stream::once(async move { Err(e) })),
};
let api_key = self.api_key.clone();
Box::pin(async_stream::try_stream! {
let response = send(&api_key, &body).await?;
let mut saw_finish = false;
let mut bytes = response.bytes_stream();
let mut buf = Vec::new();
loop {
let chunk = bytes.next().await.transpose()?;
let at_eof = chunk.is_none();
match chunk {
Some(chunk) => buf.extend_from_slice(&chunk),
None => buf.extend_from_slice(b"\n\n"),
}
while let Some(data) = next_event(&mut buf) {
if let Some(out) = parse_chunk(&data)? {
saw_finish |= out.finish_reason.is_some();
yield out;
}
}
if at_eof {
break;
}
}
if !saw_finish {
Err(anyhow::anyhow!("the Qwen stream ended before the reply finished"))?;
}
})
}
}
static CLIENT: std::sync::LazyLock<reqwest::Client> = std::sync::LazyLock::new(|| {
reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(30))
.read_timeout(std::time::Duration::from_secs(300))
.build()
.expect("the HTTP client builds")
});
async fn send(api_key: &str, body: &serde_json::Value) -> anyhow::Result<reqwest::Response> {
const MAX_RETRIES: u32 = 3;
const MAX_WAIT_SECS: u64 = 10;
const RETRY_STATUSES: [u16; 6] = [429, 500, 502, 503, 504, 529];
for attempt in 0..=MAX_RETRIES {
let response = CLIENT
.post(API_URL)
.bearer_auth(api_key)
.header("accept", "text/event-stream")
.json(body)
.send()
.await?;
let status = response.status();
if status.is_success() {
return Ok(response);
}
if RETRY_STATUSES.contains(&status.as_u16()) && attempt < MAX_RETRIES {
let wait_secs = (1u64 << attempt).min(MAX_WAIT_SECS);
log::warn!(
"Got {status}. Retrying after {wait_secs}s (attempt {}/{MAX_RETRIES})",
attempt + 1
);
tokio::time::sleep(std::time::Duration::from_secs(wait_secs)).await;
continue;
}
let text = response.text().await.unwrap_or_default();
return Err(api_error(
status.as_u16(),
format!("Qwen API request failed with status {status}: {text}"),
));
}
unreachable!("retry loop returns or bails on every path")
}
fn next_event(buf: &mut Vec<u8>) -> Option<String> {
let (pos, len) = buf
.windows(2)
.position(|w| w == b"\n\n")
.map(|p| (p, 2))
.or_else(|| {
buf.windows(4)
.position(|w| w == b"\r\n\r\n")
.map(|p| (p, 4))
})?;
let raw: Vec<u8> = buf.drain(..pos + len).collect();
Some(
String::from_utf8_lossy(&raw)
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|data| data.strip_prefix(' ').unwrap_or(data))
.collect::<Vec<_>>()
.join("\n"),
)
}
fn parse_chunk(data: &str) -> anyhow::Result<Option<MessageDeltaOutput>> {
if data.is_empty() || data == "[DONE]" {
return Ok(None);
}
let val: serde_json::Value = serde_json::from_str(data)?;
if let Some(error) = val.get("error") {
bail!(
"Qwen stream error: {}",
error["message"].as_str().unwrap_or("(no message)")
);
}
let mut out = MessageDeltaOutput::new();
out.delta.role = Some(Role::Assistant);
let mut empty = true;
if let Some(choice) = val.pointer("/choices/0") {
let delta = &choice["delta"];
if let Some(text) = delta["content"].as_str().filter(|t| !t.is_empty()) {
out.delta.contents.push(PartDelta::Text {
text: text.to_owned(),
});
empty = false;
}
if let Some(text) = delta["reasoning_content"]
.as_str()
.filter(|t| !t.is_empty())
{
out.delta.thinking = Some(text.to_owned());
empty = false;
}
for call in delta["tool_calls"].as_array().into_iter().flatten() {
out.delta.tool_calls.push(PartDelta::Function {
id: call["id"]
.as_str()
.filter(|id| !id.is_empty())
.map(str::to_owned),
function: PartDeltaFunction::WithStringArgs {
name: call["function"]["name"]
.as_str()
.unwrap_or_default()
.to_owned(),
arguments: call["function"]["arguments"]
.as_str()
.unwrap_or_default()
.to_owned(),
},
});
empty = false;
}
if let Some(reason) = choice["finish_reason"].as_str() {
out.finish_reason = Some(match reason {
"stop" => FinishReason::Stop {},
"tool_calls" => FinishReason::ToolCall {},
"length" => FinishReason::Length {},
other => FinishReason::Refusal {
reason: format!("reason: {other}"),
},
});
empty = false;
}
}
if let Some(usage) = val.get("usage").filter(|u| u.is_object()) {
let count = |ptr: &str| usage.pointer(ptr).and_then(|v| v.as_u64());
out.usage = Some(TokenUsage {
input_tokens: count("/prompt_tokens").unwrap_or(0),
output_tokens: count("/completion_tokens").unwrap_or(0),
cache_creation_input_tokens: None,
cache_read_input_tokens: count("/prompt_tokens_details/cached_tokens"),
});
empty = false;
}
Ok((!empty).then_some(out))
}
fn chat_messages(messages: &[Message]) -> anyhow::Result<Vec<Value>> {
let mut out = Vec::new();
for msg in messages {
match msg.role {
Role::System => {}
Role::Tool => {
let id = msg
.id
.as_deref()
.context("a tool message has no id to match its call")?;
let text = msg
.contents
.iter()
.map(|p| match p {
Part::Text { text } => text.clone(),
Part::Value { value } => value_text(value),
Part::Image {
image: PartImage::Embedded { mime_type, .. },
} => format!("[image: {mime_type}]"),
Part::Image {
image: PartImage::Url { url },
} => format!("[image at {url}]"),
Part::Function { .. } => String::new(),
})
.collect::<String>();
out.push(to_value!({"role": "tool", "tool_call_id": id, "content": text}));
}
Role::User => {
let content: Vec<Value> = msg
.contents
.iter()
.map(|p| match p {
Part::Text { text } => to_value!({"type": "text", "text": text}),
Part::Value { value } => {
to_value!({"type": "text", "text": value_text(value)})
}
Part::Image { image } => {
let url = match image {
PartImage::Embedded { mime_type, data } => {
format!("data:{mime_type};base64,{}", data.base64())
}
PartImage::Url { url } => url.clone(),
};
to_value!({"type": "image_url", "image_url": {"url": url}})
}
Part::Function { .. } => to_value!({"type": "text", "text": ""}),
})
.collect();
out.push(to_value!({"role": "user", "content": content}));
}
Role::Assistant => {
let text = msg
.contents
.iter()
.filter_map(|p| p.as_text())
.collect::<String>();
let mut turn = to_value!({"role": "assistant", "content": text});
let obj = turn.as_object_mut().unwrap();
let calls: Vec<Value> = msg
.tool_calls
.iter()
.flatten()
.filter_map(|call| match call {
Part::Function {
id,
function: PartFunction { name, arguments },
} => Some(to_value!({
"id": id,
"type": "function",
"function": {
"name": name,
"arguments": serde_json::to_string(arguments).unwrap_or_default(),
},
})),
_ => None,
})
.collect();
if !calls.is_empty() {
obj.insert("tool_calls".into(), Value::Array(calls));
}
out.push(turn);
}
}
}
Ok(out)
}
fn value_text(value: &Value) -> String {
match value {
Value::String(s) => s.clone(),
other => serde_json::to_string(other).unwrap_or_default(),
}
}