@@ -32,15 +32,15 @@ For details on OpenAI API parameters, see
3232``` python
3333from haystack.components.generators import AzureOpenAIGenerator
3434from haystack.utils import Secret
35+
3536client = AzureOpenAIGenerator(
36- azure_endpoint = " <Your Azure endpoint e.g. `https://your-company.azure.openai.com/>" ,
37- api_key = Secret.from_token(" <your-api-key>" ),
38- azure_deployment = " <this is a model name, e.g. gpt-4.1-mini>" )
37+ azure_endpoint = Secret.from_env_var(" AZURE_OPENAI_ENDPOINT" ).resolve_value(),
38+ api_key = Secret.from_env_var(" AZURE_OPENAI_API_KEY" ),
39+ azure_deployment = " gpt-4.1-mini" )
40+
3941response = client.run(" What's Natural Language Processing? Be brief." )
40- print (response)
41- ```
4242
43- ```
43+ print (response)
4444# >> {'replies': ['Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
4545# >> the interaction between computers and human language. It involves enabling computers to understand, interpret,
4646# >> and respond to natural human language in a way that is both meaningful and useful.'], 'meta': [{'model':
@@ -708,28 +708,32 @@ format for input and output. Use it to generate text with Hugging Face APIs:
708708
709709#### With the serverless inference API (Inference Providers) - free tier available
710710
711- <!-- test-ignore -->
712-
713711``` python
714712from haystack.components.generators.chat import HuggingFaceAPIChatGenerator
715713from haystack.dataclasses import ChatMessage
716714from haystack.utils import Secret
717715from haystack.utils.hf import HFGenerationAPIType
718716
719717messages = [ChatMessage.from_system(" \n You are a helpful, respectful and honest assistant" ),
720- ChatMessage.from_user(" What's Natural Language Processing?" )]
718+ ChatMessage.from_user(" What's Natural Language Processing? Please be succinct " )]
721719
722720# the api_type can be expressed using the HFGenerationAPIType enum or as a string
723721api_type = HFGenerationAPIType.SERVERLESS_INFERENCE_API
724722api_type = " serverless_inference_api" # this is equivalent to the above
725723
726- generator = HuggingFaceAPIChatGenerator(api_type = api_type,
727- api_params = {" model" : " Qwen/Qwen2.5-7B-Instruct" ,
728- " provider" : " together" },
729- token = Secret.from_token(" <your-api-key>" ))
724+ generator = HuggingFaceAPIChatGenerator(
725+ api_type = api_type,
726+ api_params = {" model" : " Qwen/Qwen2.5-7B-Instruct" , " provider" : " together" },
727+ token = Secret.from_env_var(" HF_API_TOKEN" )
728+ )
730729
731730result = generator.run(messages)
732731print (result)
732+ # >> {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>,
733+ # >> _content=[TextContent(text='Natural Language Processing (NLP) is a field of AI that focuses on the interaction
734+ # >> between humans and computers using natural language. It enables machines to understand, interpret, and
735+ # >> generate human language.')], _name=None, _meta={'model': 'Qwen/Qwen2.5-7B-Instruct', 'finish_reason':
736+ # >> 'tool_calls', 'index': 0, 'usage': {'prompt_tokens': 33, 'completion_tokens': 39}})]}
733737```
734738
735739#### With the serverless inference API (Inference Providers) and text+image input
@@ -743,18 +747,17 @@ from haystack.utils import Secret
743747from haystack.utils.hf import HFGenerationAPIType
744748
745749# Create an image from file path, URL, or base64
746- image = ImageContent.from_file_path(" path/to/your/image .jpg" )
750+ image = ImageContent.from_file_path(" test/test_files/images/apple .jpg" )
747751
748752# Create a multimodal message with both text and image
749753messages = [ChatMessage.from_user(content_parts = [" Describe this image in detail" , image])]
750754
751755generator = HuggingFaceAPIChatGenerator(
752756 api_type = HFGenerationAPIType.SERVERLESS_INFERENCE_API ,
753757 api_params = {
754- " model" : " Qwen/Qwen2.5-VL-7B-Instruct" , # Vision Language Model
755- " provider" : " hyperbolic"
758+ " model" : " Qwen/Qwen3.5-9B" , " provider" : " together" # Vision Language Model
756759 },
757- token = Secret.from_token( " <your-api-key> " )
760+ token = Secret.from_env_var( " HF_API_TOKEN " )
758761)
759762
760763result = generator.run(messages)
0 commit comments