Skip to content

Commit 7dc63c5

Browse files
Add co-author
Co-authored-by: Aaron Pang <pykm05@gmail.com>
1 parent ff0e58a commit 7dc63c5

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

tensorzero-core/src/endpoints/openai_compatible.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,16 @@ impl TryFrom<Vec<OpenAICompatibleMessage>> for Input {
668668
#[serde(tag = "type", deny_unknown_fields, rename_all = "snake_case")]
669669
enum OpenAICompatibleContentBlock {
670670
Text(TextContent),
671-
ImageUrl { image_url: OpenAICompatibleImageUrl },
672-
File { file: OpenAICompatibleFile },
671+
ImageUrl {
672+
image_url: OpenAICompatibleImageUrl,
673+
},
674+
File {
675+
file: OpenAICompatibleFile,
676+
},
677+
#[serde(rename = "tensorzero::raw_text")]
678+
RawText {
679+
value: String,
680+
},
673681
}
674682

675683
#[derive(Deserialize, Debug)]
@@ -690,7 +698,7 @@ struct OpenAICompatibleFile {
690698
// Two mutually exclusive modes - the standard OpenAI text, and our special TensorZero mode
691699
pub enum TextContent {
692700
/// A normal openai text content block: `{"type": "text", "text": "Some content"}`. The `type` key comes from the parent `OpenAICompatibleContentBlock`
693-
RawText { text: String },
701+
Text { text: String },
694702
/// A special TensorZero mode: `{"type": "text", "tensorzero::arguments": {"custom_key": "custom_val"}}`.
695703
TensorZeroArguments {
696704
tensorzero_arguments: Map<String, Value>,
@@ -703,7 +711,7 @@ impl<'de> Deserialize<'de> for TextContent {
703711
let text = object.remove("text");
704712
let arguments = object.remove("tensorzero::arguments");
705713
match (text, arguments) {
706-
(Some(text), None) => Ok(TextContent::RawText {
714+
(Some(text), None) => Ok(TextContent::Text {
707715
text: match text {
708716
Value::String(text) => text,
709717
_ => return Err(serde::de::Error::custom(
@@ -756,7 +764,8 @@ fn convert_openai_message_content(content: Value) -> Result<Vec<InputMessageCont
756764
for val in a {
757765
let block = serde_json::from_value::<OpenAICompatibleContentBlock>(val.clone());
758766
let output = match block {
759-
Ok(OpenAICompatibleContentBlock::Text(TextContent::RawText { text })) => InputMessageContent::Text(TextKind::Text {text }),
767+
Ok(OpenAICompatibleContentBlock::RawText{ value }) => InputMessageContent::RawText { value },
768+
Ok(OpenAICompatibleContentBlock::Text(TextContent::Text { text })) => InputMessageContent::Text(TextKind::Text {text }),
760769
Ok(OpenAICompatibleContentBlock::Text(TextContent::TensorZeroArguments { tensorzero_arguments })) => InputMessageContent::Text(TextKind::Arguments { arguments: tensorzero_arguments }),
761770
Ok(OpenAICompatibleContentBlock::ImageUrl { image_url }) => {
762771
if image_url.url.scheme() == "data" {
@@ -1429,6 +1438,26 @@ mod tests {
14291438

14301439
#[test]
14311440
fn test_convert_openai_message_content() {
1441+
// text content
1442+
let content = "Hello, world!".to_string();
1443+
let value = convert_openai_message_content(Value::String(content.clone())).unwrap();
1444+
assert_eq!(
1445+
value,
1446+
vec![InputMessageContent::Text(TextKind::Text { text: content })]
1447+
);
1448+
// tensorzero::raw_text
1449+
let content = json!([{
1450+
"type": "tensorzero::raw_text",
1451+
"value": "This is raw text"
1452+
}]);
1453+
let value = convert_openai_message_content(content.clone()).unwrap();
1454+
assert_eq!(
1455+
value,
1456+
vec![InputMessageContent::RawText {
1457+
value: "This is raw text".to_string()
1458+
}]
1459+
);
1460+
// tensorzero::arguments
14321461
let content = json!([{
14331462
"country": "Japan",
14341463
"city": "Tokyo",

0 commit comments

Comments
 (0)