Accept wrapped text content from LLM providers (#31048)
Some providers sometimes send `{ "type": "text", "text": ... }` instead of just the text as a string. Now we accept those instead of erroring. Release Notes: - N/A
This commit is contained in:
parent
89700c3682
commit
4bb04cef9d
9 changed files with 72 additions and 26 deletions
|
@ -153,19 +153,29 @@ pub struct LanguageModelToolResult {
|
|||
pub enum LanguageModelToolResultContent {
|
||||
Text(Arc<str>),
|
||||
Image(LanguageModelImage),
|
||||
WrappedText(WrappedTextContent),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq, Hash)]
|
||||
pub struct WrappedTextContent {
|
||||
#[serde(rename = "type")]
|
||||
pub content_type: String,
|
||||
pub text: Arc<str>,
|
||||
}
|
||||
|
||||
impl LanguageModelToolResultContent {
|
||||
pub fn to_str(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::Text(text) => Some(&text),
|
||||
Self::Text(text) | Self::WrappedText(WrappedTextContent { text, .. }) => Some(&text),
|
||||
Self::Image(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
match self {
|
||||
Self::Text(text) => text.chars().all(|c| c.is_whitespace()),
|
||||
Self::Text(text) | Self::WrappedText(WrappedTextContent { text, .. }) => {
|
||||
text.chars().all(|c| c.is_whitespace())
|
||||
}
|
||||
Self::Image(_) => false,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue