Skip to content

Commit

Permalink
When using openai-generic, use a string as the content type in the ap…
Browse files Browse the repository at this point in the history
…i request if theres no media (#988)

<!-- ELLIPSIS_HIDDEN -->



> [!IMPORTANT]
> Modify `role_to_message()` in `OpenAIClient` to concatenate text parts
for `openai-generic` provider if all parts are text.
> 
>   - **Behavior**:
> - In `role_to_message()` of `OpenAIClient`, for `openai-generic`
provider, concatenate all text parts into a single string if all parts
are text.
>     - If any part is non-text, use existing structure for `content`.
>     - For other providers, retain existing structure for `content`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 31626f6. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
aaronvg authored Sep 27, 2024
1 parent c27a64f commit e8fa739
Showing 1 changed file with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,43 @@ impl ToProviderMessage for OpenAIClient {
) -> Result<serde_json::Map<String, serde_json::Value>> {
let mut message = serde_json::Map::new();
message.insert("role".into(), json!(content.role));
message.insert(
"content".into(),
json!(self.parts_to_message(&content.parts)?),
);
if self.provider == "openai-generic" {
// Check if all parts are text
let all_text = content
.parts
.iter()
.all(|part| matches!(part, ChatMessagePart::Text(_)));
if all_text {
// Concatenate all text parts into a single string
let combined_text = content
.parts
.iter()
.map(|part| {
if let ChatMessagePart::Text(text) = part {
Ok(text.clone())
} else {
Err(anyhow::anyhow!("Non-text part encountered"))
}
})
.collect::<Result<Vec<String>>>()?
.join(" ");

message.insert("content".into(), json!(combined_text));
} else {
// If there are media parts, use the existing structure
message.insert(
"content".into(),
json!(self.parts_to_message(&content.parts)?),
);
}
} else {
// For other providers, use the existing structure
message.insert(
"content".into(),
json!(self.parts_to_message(&content.parts)?),
);
}

Ok(message)
}
}
Expand Down

0 comments on commit e8fa739

Please sign in to comment.