Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional content field in Chat Completions Request #3021

Draft
wants to merge 2 commits into
base: git_v2.4.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion router/src/infer/chat_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ impl ChatTemplate {
format!("\n---\n{}", tool_prompt)
};
if let Some(last_message) = messages.last_mut() {
last_message.content.push(MessageChunk::Text { text });
if let Some(content) = last_message.content.as_mut() {
content.push(MessageChunk::Text { text });
}
}
Some(tools)
}
Expand Down
25 changes: 15 additions & 10 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,10 +1183,11 @@ pub struct Message {
#[schema(example = "user")]
role: String,
#[schema(example = "My name is David and I")]
pub content: MessageContent,
pub content: Option<MessageContent>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schema(example = "\"David\"")]
name: Option<String>,
tool_calls: Option<Vec<ToolCall>>,
}

#[derive(Clone, Deserialize, Serialize, ToSchema, Debug, PartialEq)]
Expand Down Expand Up @@ -1226,15 +1227,19 @@ impl From<Message> for TextMessage {
TextMessage {
role: value.role,
content: match value.content {
MessageContent::SingleText(text) => text,
MessageContent::MultipleChunks(chunks) => chunks
.into_iter()
.map(|chunk| match chunk {
MessageChunk::Text { text } => text,
MessageChunk::ImageUrl { image_url } => format!("![]({})", image_url.url),
})
.collect::<Vec<_>>()
.join(""),
// If content is Some(MessageContent), handle it accordingly
Some(MessageContent::SingleText(text)) => text,
Some(MessageContent::MultipleChunks(chunks)) => {
chunks.into_iter()
.map(|chunk| match chunk {
MessageChunk::Text { text } => text,
MessageChunk::ImageUrl { image_url } => format!("![]({})", image_url.url),
})
.collect::<Vec<_>>()
.join("")
}
// If content is None, use an empty string or a default message
None => String::new(), // or you could use "No content" or another placeholder
},
}
}
Expand Down