make content field optional in chat request

This commit is contained in:
sailesh duddupudi 2025-02-13 19:33:38 +00:00 committed by drbh
parent 230aa25641
commit e14617d88d
2 changed files with 17 additions and 11 deletions

View File

@ -74,7 +74,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)
}

View File

@ -1181,7 +1181,7 @@ 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>,
@ -1224,15 +1224,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
},
}
}