Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion haystack/components/audio/whisper_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ class LocalWhisperTranscriber:
[GitHub repository](https://github.com/openai/whisper).

### Usage example
<!-- test-ignore -->

```python
from haystack.components.audio import LocalWhisperTranscriber

whisper = LocalWhisperTranscriber(model="small")
transcription = whisper.run(sources=["test/test_files/audio/answer.wav"])
print(transcription)
print(transcription)
# >> {'documents': [Document(id=dae7051417caaf19304a4ef2ec845e981abe1efd4c8e6ee7ffb25867f165c411,
# >> content: ' Answer.', meta: {'audio_file': PosixPath('test/test_files/audio/answer.wav'), 'language': 'en'})]}
```
"""

Expand Down
14 changes: 5 additions & 9 deletions haystack/components/converters/file_to_file_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ class FileToFileContent:
from haystack.components.converters import FileToFileContent

converter = FileToFileContent()

sources = ["document.pdf", "video.mp4"]

sources = ["test/test_files/pdf/react_paper.pdf", "test/test_files/images/haystack-logo.png"]
file_contents = converter.run(sources=sources)["file_contents"]
print(file_contents)

# [FileContent(base64_data='...',
# mime_type='application/pdf',
# filename='document.pdf',
# extra={}),
# ...]
print(file_contents)
# >> [FileContent(base64_data='...', mime_type='application/pdf', filename='react_paper.pdf', extra={}),
# >> FileContent(base64_data='...', mime_type='image/png', filename='haystack-logo.png', extra={})
# >>]
```
"""

Expand Down
14 changes: 8 additions & 6 deletions haystack/components/converters/image/document_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,33 @@ class DocumentToImageContent:
- For PDF files, a `page_number` key specifying which page to extract

### Usage example
<!-- test-ignore -->

```python
from haystack import Document
from haystack.components.converters.image.document_to_image import DocumentToImageContent

converter = DocumentToImageContent(
file_path_meta_field="file_path",
root_path="/data/files",
root_path="test/test_files",
detail="high",
size=(800, 600)
)

documents = [
Document(content="Optional description of image.jpg", meta={"file_path": "image.jpg"}),
Document(content="Text content of page 1 of doc.pdf", meta={"file_path": "doc.pdf", "page_number": 1})
Document(content="Optional description of image.jpg", meta={"file_path": "images/apple.jpg"}),
Document(
content="Text content of page 1 of doc.pdf", meta={"file_path": "pdf/sample_pdf_1.pdf", "page_number": 1}
)
]

result = converter.run(documents)
image_contents = result["image_contents"]
# [ImageContent(
# base64_image='/9j/4A...', mime_type='image/jpeg', detail='high', meta={'file_path': 'image.jpg'}
# base64_image='/9j/4A...', mime_type='image/jpeg', detail='high', meta={'file_path': 'images/apple.jpg'}
# ),
# ImageContent(
# base64_image='/9j/4A...', mime_type='image/jpeg', detail='high',
# meta={'page_number': 1, 'file_path': 'doc.pdf'}
# meta={'file_path': 'pdf/sample_pdf_1.pdf', 'page_number': 1})
# )]
```
"""
Expand Down
6 changes: 5 additions & 1 deletion haystack/components/extractors/named_entity_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class NamedEntityExtractor:
in the documents.

Usage example:
<!-- test-ignore -->

```python
from haystack import Document
from haystack.components.extractors.named_entity_extractor import NamedEntityExtractor
Expand All @@ -101,6 +101,10 @@ class NamedEntityExtractor:
results = extractor.run(documents=documents)["documents"]
annotations = [NamedEntityExtractor.get_stored_annotations(doc) for doc in results]
print(annotations)
# >> [[NamedEntityAnnotation(entity='PER', start=4, end=10, score=np.float32(0.99054915))],
# >> [NamedEntityAnnotation(entity='PER', start=11, end=16, score=np.float32(0.99641764)),
# >> NamedEntityAnnotation(entity='LOC', start=31, end=39, score=np.float32(0.996198)),
# >> NamedEntityAnnotation(entity='LOC', start=41, end=51, score=np.float32(0.9990196))]]
```
"""

Expand Down
12 changes: 6 additions & 6 deletions haystack/components/generators/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class AzureOpenAIGenerator(OpenAIGenerator):
```python
from haystack.components.generators import AzureOpenAIGenerator
from haystack.utils import Secret

client = AzureOpenAIGenerator(
azure_endpoint="<Your Azure endpoint e.g. `https://your-company.azure.openai.com/>",
api_key=Secret.from_token("<your-api-key>"),
azure_deployment="<this a model name, e.g. gpt-4.1-mini>")
azure_endpoint=Secret.from_env_var("AZURE_OPENAI_ENDPOINT").resolve_value(),
api_key=Secret.from_env_var("AZURE_OPENAI_API_KEY"),
azure_deployment="gpt-4.1-mini")

response = client.run("What's Natural Language Processing? Be brief.")
print(response)
```

```
print(response)
# >> {'replies': ['Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
# >> the interaction between computers and human language. It involves enabling computers to understand, interpret,
# >> and respond to natural human language in a way that is both meaningful and useful.'], 'meta': [{'model':
Expand Down
24 changes: 15 additions & 9 deletions haystack/components/generators/chat/hugging_face_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,27 +263,33 @@ class HuggingFaceAPIChatGenerator:
### Usage examples

#### With the serverless inference API (Inference Providers) - free tier available
<!-- test-ignore -->

```python
from haystack.components.generators.chat import HuggingFaceAPIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret
from haystack.utils.hf import HFGenerationAPIType

messages = [ChatMessage.from_system("\\nYou are a helpful, respectful and honest assistant"),
ChatMessage.from_user("What's Natural Language Processing?")]
ChatMessage.from_user("What's Natural Language Processing? Please be succinct")]

# the api_type can be expressed using the HFGenerationAPIType enum or as a string
api_type = HFGenerationAPIType.SERVERLESS_INFERENCE_API
api_type = "serverless_inference_api" # this is equivalent to the above

generator = HuggingFaceAPIChatGenerator(api_type=api_type,
api_params={"model": "Qwen/Qwen2.5-7B-Instruct",
"provider": "together"},
token=Secret.from_token("<your-api-key>"))
generator = HuggingFaceAPIChatGenerator(
api_type=api_type,
api_params={"model": "Qwen/Qwen2.5-7B-Instruct", "provider": "together"},
token=Secret.from_env_var("HF_API_TOKEN")
)

result = generator.run(messages)
print(result)
# >> {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>,
# >> _content=[TextContent(text='Natural Language Processing (NLP) is a field of AI that focuses on the interaction
# >> between humans and computers using natural language. It enables machines to understand, interpret, and
# >> generate human language.')], _name=None, _meta={'model': 'Qwen/Qwen2.5-7B-Instruct', 'finish_reason':
# >> 'tool_calls', 'index': 0, 'usage': {'prompt_tokens': 33, 'completion_tokens': 39}})]}
```

#### With the serverless inference API (Inference Providers) and text+image input
Expand All @@ -295,18 +301,18 @@ class HuggingFaceAPIChatGenerator:
from haystack.utils.hf import HFGenerationAPIType

# Create an image from file path, URL, or base64
image = ImageContent.from_file_path("path/to/your/image.jpg")
image = ImageContent.from_file_path("test/test_files/images/apple.jpg")

# Create a multimodal message with both text and image
messages = [ChatMessage.from_user(content_parts=["Describe this image in detail", image])]

generator = HuggingFaceAPIChatGenerator(
api_type=HFGenerationAPIType.SERVERLESS_INFERENCE_API,
api_params={
"model": "Qwen/Qwen2.5-VL-7B-Instruct", # Vision Language Model
"model": "Qwen/Qwen3-Coder-480B-A35B-Instruct", # Vision Language Model
"provider": "hyperbolic"
},
token=Secret.from_token("<your-api-key>")
token=Secret.from_env_var("HF_API_TOKEN")
)

result = generator.run(messages)
Expand Down