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

feat: improve message content chunks handling #1845

Closed
wants to merge 3 commits into from

Conversation

drbh
Copy link
Collaborator

@drbh drbh commented May 2, 2024

This PR improves the parsing of content in messages by deserializing all content into ContentChunks (a vector of ContentChunk). Each ContentChunk is an enum representing either raw text or an image URL. For minimal change impact, ContentChunks can be serialized directly into a flattened string.

@drbh drbh marked this pull request as ready for review May 14, 2024 19:38
@drbh
Copy link
Collaborator Author

drbh commented May 14, 2024

**note this PR improves how images are handle when sent as separate content in the chat endpoint. If ![]() markdown images are included they are still handled the same way, and a future RP may deprecate markdown images in chat and improve how images are sent to the model

example request

from huggingface_hub import InferenceClient

client = InferenceClient("http://127.0.0.1:3000")

chat = client.chat_completion(
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "Whats in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"
                    },
                },
            ],
        },
    ],
    seed=42,
    max_tokens=100,
)

router/src/infer.rs Outdated Show resolved Hide resolved
router/src/lib.rs Outdated Show resolved Hide resolved
router/src/lib.rs Outdated Show resolved Hide resolved
@Narsil
Copy link
Collaborator

Narsil commented May 15, 2024

Can you add some tests too for the deserialization ?

@drbh
Copy link
Collaborator Author

drbh commented May 15, 2024

updates:

this pr now also parses markdown images into typed ContentChunks

for example both the string and structured JSON inputs are deserialized into ContentChunks and both currently serialize back into a string

let content = json!("Whats in this image?![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png)");
let chunks = message_content_serde::deserialize(content)
.expect("Failed to deserialize")
.unwrap();
assert_eq!(
chunks,
ContentChunks(vec![
ContentChunk::Text("Whats in this image?".to_string()),
ContentChunk::ImageUrl("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png".to_string())
])
);

and

let content = json!([
{"type": "text", "text": "Whats in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"
},
},
]);
let chunks: ContentChunks = message_content_serde::deserialize(content)
.expect("Failed to deserialize")
.unwrap();
assert_eq!(
chunks,
ContentChunks(vec![
ContentChunk::Text("Whats in this image?".to_string()),
ContentChunk::ImageUrl("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png".to_string())
])
);


fn parse_markdown_to_chunks(s: &str) -> Result<Vec<ContentChunk>, serde_json::Error> {
let mut chunks = Vec::new();
let re = Regex::new(r"!\[([^\]]*)\]\(([^)]+)\)").unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what we do not want.

@drbh
Copy link
Collaborator Author

drbh commented May 16, 2024

closing in favor of #1906

@drbh drbh closed this May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants