File tree Expand file tree Collapse file tree
haystack_experimental/components Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ class DocumentToImageContent:
3434 - A supported image format (MIME type must be one of the supported image types)
3535 - For PDF files, a `page_number` key specifying which page to extract
3636
37- Usage example:
37+ ### Usage example
3838 ```python
3939 from haystack import Document
4040 from haystack_experimental.components.image_converters.document_to_image import DocumentToImageContent
Original file line number Diff line number Diff line change 3131class ImageFileToImageContent :
3232 """
3333 Converts image files to ImageContent objects.
34+
35+ ### Usage example
36+ ```python
37+ from haystack_experimental.components.converters.image import ImageFileToImageContent
38+
39+ converter = ImageFileToImageContent()
40+
41+ sources = ["image.jpg", "another_image.png"]
42+
43+ image_contents = converter.run(sources=sources)["image_contents"]
44+ print(image_contents)
45+
46+ # [ImageContent(base64_image='...',
47+ # mime_type='image/jpeg',
48+ # detail=None,
49+ # meta={'file_path': 'image.jpg'}),
50+ # ...]
51+ ```
3452 """
3553
3654 def __init__ (
Original file line number Diff line number Diff line change @@ -23,6 +23,23 @@ class ImageFileToDocument:
2323
2424 It does **not** extract any content from the image files, instead it creates `Document` objects with `None` as
2525 their content and attaches metadata such as file path and any user-provided values.
26+
27+ ### Usage example
28+ ```python
29+ from haystack_experimental.components.converters.image import ImageFileToDocument
30+
31+ converter = ImageFileToDocument()
32+
33+ sources = ["image.jpg", "another_image.png"]
34+
35+ result = converter.run(sources=sources)
36+ documents = result["documents"]
37+
38+ print(documents)
39+
40+ # [Document(id=..., meta: {'file_path': 'image.jpg'}),
41+ # Document(id=..., meta: {'file_path': 'another_image.png'})]
42+ ```
2643 """
2744
2845 def __init__ (self , * , store_full_path : bool = False ):
Original file line number Diff line number Diff line change 2424class PDFToImageContent :
2525 """
2626 Converts PDF files to ImageContent objects.
27+
28+ ### Usage example
29+ ```python
30+ from haystack_experimental.components.converters.image import PDFToImageContent
31+
32+ converter = PDFToImageContent()
33+
34+ sources = ["file.pdf", "another_file.pdf"]
35+
36+ image_contents = converter.run(sources=sources)["image_contents"]
37+ print(image_contents)
38+
39+ # [ImageContent(base64_image='...',
40+ # mime_type='application/pdf',
41+ # detail=None,
42+ # meta={'file_path': 'file.pdf', 'page_number': 1}),
43+ # ...]
44+ ```
2745 """
2846
2947 def __init__ (
Original file line number Diff line number Diff line change @@ -33,6 +33,31 @@ class SentenceTransformersDocumentImageEmbedder:
3333 A component for computing Document embeddings based on images using Sentence Transformers models.
3434
3535 The embedding of each Document is stored in the `embedding` field of the Document.
36+
37+ ### Usage example
38+ ```python
39+ from haystack import Document
40+ from haystack_experimental.components.embedders.image import SentenceTransformersDocumentImageEmbedder
41+
42+ embedder = SentenceTransformersDocumentImageEmbedder(model="sentence-transformers/clip-ViT-B-32")
43+ embedder.warm_up()
44+
45+ documents = [
46+ Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}),
47+ Document(content="A photo of a dog", meta={"file_path": "dog.jpg"}),
48+ ]
49+
50+ result = embedder.run(documents=documents)
51+ documents_with_embeddings = result["documents"]
52+ print(documents_with_embeddings)
53+
54+ # [Document(id=...,
55+ # content='A photo of a cat',
56+ # meta={'file_path': 'cat.jpg',
57+ # 'embedding_source': {'type': 'image', 'file_path_meta_field': 'file_path'}},
58+ # embedding=vector of size 512),
59+ # ...]
60+ ```
3661 """
3762
3863 def __init__ (
Original file line number Diff line number Diff line change 3939 class AmazonBedrockChatGenerator :
4040 """
4141 Experimental version of AmazonBedrockChatGenerator that allows multimodal chat messages.
42+
43+ ### Usage example
44+ ```python
45+ from haystack_experimental.components.generators.chat import AmazonBedrockChatGenerator
46+ from haystack_experimental.dataclasses import ChatMessage, ImageContent
47+
48+ generator = AmazonBedrockChatGenerator(model="anthropic.claude-3-5-sonnet-20240620-v1:0")
49+
50+ image_content = ImageContent.from_file_path(file_path="apple.jpg")
51+
52+ message = ChatMessage.from_user(content_parts=["Describe the image using 10 words at most.", image_content])
53+
54+ response = generator.run(messages=[message])["replies"][0].text
55+
56+ print(response)
57+ # The image shows a red apple.
58+ ```
4259 """
4360
4461 def __init__ ( # pylint: disable=too-many-positional-arguments
Original file line number Diff line number Diff line change 1616class OpenAIChatGenerator (haystack .components .generators .chat .openai .OpenAIChatGenerator ):
1717 """
1818 Experimental version of OpenAIChatGenerator that allows multimodal chat messages.
19+
20+ ### Usage example
21+ ```python
22+ from haystack_experimental.components.generators.chat import OpenAIChatGenerator
23+ from haystack_experimental.dataclasses import ChatMessage, ImageContent
24+
25+ generator = OpenAIChatGenerator(model="gpt-4o-mini")
26+
27+ image_content = ImageContent.from_file_path(file_path="apple.jpg")
28+
29+ message = ChatMessage.from_user(content_parts=["Please describe the image using 5 words at most.", image_content])
30+
31+ response = generator.run(messages=[message])["replies"][0].text
32+
33+ print(response)
34+ # Red apple on straw background.
35+ ```
1936 """
2037
2138 pass
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ class QueryExpander:
6161 {"queries": ["expanded query 1", "expanded query 2", "expanded query 3"]}
6262 ```
6363
64- Usage example:
64+ ### Usage example
6565
6666 ```python
6767 from haystack.components.generators.chat.openai import OpenAIChatGenerator
You can’t perform that action at this time.
0 commit comments