@@ -264,27 +264,33 @@ class HuggingFaceAPIChatGenerator:
264264 ### Usage examples
265265
266266 #### With the serverless inference API (Inference Providers) - free tier available
267- <!-- test-ignore -->
267+
268268 ```python
269269 from haystack.components.generators.chat import HuggingFaceAPIChatGenerator
270270 from haystack.dataclasses import ChatMessage
271271 from haystack.utils import Secret
272272 from haystack.utils.hf import HFGenerationAPIType
273273
274274 messages = [ChatMessage.from_system("\\ nYou are a helpful, respectful and honest assistant"),
275- ChatMessage.from_user("What's Natural Language Processing?")]
275+ ChatMessage.from_user("What's Natural Language Processing? Please be succinct ")]
276276
277277 # the api_type can be expressed using the HFGenerationAPIType enum or as a string
278278 api_type = HFGenerationAPIType.SERVERLESS_INFERENCE_API
279279 api_type = "serverless_inference_api" # this is equivalent to the above
280280
281- generator = HuggingFaceAPIChatGenerator(api_type=api_type,
282- api_params={"model": "Qwen/Qwen2.5-7B-Instruct",
283- "provider": "together"},
284- token=Secret.from_token("<your-api-key>"))
281+ generator = HuggingFaceAPIChatGenerator(
282+ api_type=api_type,
283+ api_params={"model": "Qwen/Qwen2.5-7B-Instruct", "provider": "together"},
284+ token=Secret.from_env_var("HF_API_TOKEN")
285+ )
285286
286287 result = generator.run(messages)
287288 print(result)
289+ # >> {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>,
290+ # >> _content=[TextContent(text='Natural Language Processing (NLP) is a field of AI that focuses on the interaction
291+ # >> between humans and computers using natural language. It enables machines to understand, interpret, and
292+ # >> generate human language.')], _name=None, _meta={'model': 'Qwen/Qwen2.5-7B-Instruct', 'finish_reason':
293+ # >> 'tool_calls', 'index': 0, 'usage': {'prompt_tokens': 33, 'completion_tokens': 39}})]}
288294 ```
289295
290296 #### With the serverless inference API (Inference Providers) and text+image input
@@ -296,18 +302,17 @@ class HuggingFaceAPIChatGenerator:
296302 from haystack.utils.hf import HFGenerationAPIType
297303
298304 # Create an image from file path, URL, or base64
299- image = ImageContent.from_file_path("path/to/your/image .jpg")
305+ image = ImageContent.from_file_path("test/test_files/images/apple .jpg")
300306
301307 # Create a multimodal message with both text and image
302308 messages = [ChatMessage.from_user(content_parts=["Describe this image in detail", image])]
303309
304310 generator = HuggingFaceAPIChatGenerator(
305311 api_type=HFGenerationAPIType.SERVERLESS_INFERENCE_API,
306312 api_params={
307- "model": "Qwen/Qwen2.5-VL-7B-Instruct", # Vision Language Model
308- "provider": "hyperbolic"
313+ "model": "Qwen/Qwen3.5-9B", "provider": "together" # Vision Language Model
309314 },
310- token=Secret.from_token("<your-api-key> ")
315+ token=Secret.from_env_var("HF_API_TOKEN ")
311316 )
312317
313318 result = generator.run(messages)
0 commit comments