watsonx.md
1 --- 2 title: "IBM watsonx.ai" 3 id: integrations-watsonx 4 description: "IBM watsonx.ai integration for Haystack" 5 slug: "/integrations-watsonx" 6 --- 7 8 9 ## haystack_integrations.components.embedders.watsonx.document_embedder 10 11 ### WatsonxDocumentEmbedder 12 13 Computes document embeddings using IBM watsonx.ai models. 14 15 ### Usage example 16 17 ```python 18 from haystack import Document 19 from haystack_integrations.components.embedders.watsonx.document_embedder import WatsonxDocumentEmbedder 20 21 documents = [ 22 Document(content="I love pizza!"), 23 Document(content="Pasta is great too"), 24 ] 25 26 document_embedder = WatsonxDocumentEmbedder( 27 model="ibm/slate-30m-english-rtrvr-v2", 28 api_key=Secret.from_env_var("WATSONX_API_KEY"), 29 api_base_url="https://us-south.ml.cloud.ibm.com", 30 project_id=Secret.from_env_var("WATSONX_PROJECT_ID"), 31 ) 32 33 result = document_embedder.run(documents=documents) 34 print(result["documents"][0].embedding) 35 36 # [0.017020374536514282, -0.023255806416273117, ...] 37 ``` 38 39 #### __init__ 40 41 ```python 42 __init__( 43 *, 44 model: str = "ibm/slate-30m-english-rtrvr-v2", 45 api_key: Secret = Secret.from_env_var("WATSONX_API_KEY"), 46 api_base_url: str = "https://us-south.ml.cloud.ibm.com", 47 project_id: Secret = Secret.from_env_var("WATSONX_PROJECT_ID"), 48 truncate_input_tokens: int | None = None, 49 prefix: str = "", 50 suffix: str = "", 51 batch_size: int = 1000, 52 concurrency_limit: int = 5, 53 timeout: float | None = None, 54 max_retries: int | None = None, 55 meta_fields_to_embed: list[str] | None = None, 56 embedding_separator: str = "\n" 57 ) 58 ``` 59 60 Creates a WatsonxDocumentEmbedder component. 61 62 **Parameters:** 63 64 - **model** (<code>str</code>) – The name of the model to use for calculating embeddings. 65 Default is "ibm/slate-30m-english-rtrvr-v2". 66 - **api_key** (<code>Secret</code>) – The WATSONX API key. Can be set via environment variable WATSONX_API_KEY. 67 - **api_base_url** (<code>str</code>) – The WATSONX URL for the watsonx.ai service. 68 Default is "https://us-south.ml.cloud.ibm.com". 69 - **project_id** (<code>Secret</code>) – The ID of the Watson Studio project. 70 Can be set via environment variable WATSONX_PROJECT_ID. 71 - **truncate_input_tokens** (<code>int | None</code>) – Maximum number of tokens to use from the input text. 72 If set to `None` (or not provided), the full input text is used, up to the model's maximum token limit. 73 - **prefix** (<code>str</code>) – A string to add at the beginning of each text. 74 - **suffix** (<code>str</code>) – A string to add at the end of each text. 75 - **batch_size** (<code>int</code>) – Number of documents to embed in one API call. Default is 1000. 76 - **concurrency_limit** (<code>int</code>) – Number of parallel requests to make. Default is 5. 77 - **timeout** (<code>float | None</code>) – Timeout for API requests in seconds. 78 - **max_retries** (<code>int | None</code>) – Maximum number of retries for API requests. 79 80 #### to_dict 81 82 ```python 83 to_dict() -> dict[str, Any] 84 ``` 85 86 Serialize the component to a dictionary. 87 88 **Returns:** 89 90 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 91 92 #### from_dict 93 94 ```python 95 from_dict(data: dict[str, Any]) -> 'WatsonxDocumentEmbedder' 96 ``` 97 98 Deserializes the component from a dictionary. 99 100 **Parameters:** 101 102 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 103 104 **Returns:** 105 106 - <code>'WatsonxDocumentEmbedder'</code> – The deserialized component instance. 107 108 #### run 109 110 ```python 111 run(documents: list[Document]) -> dict[str, list[Document] | dict[str, Any]] 112 ``` 113 114 Embeds a list of documents. 115 116 **Parameters:** 117 118 - **documents** (<code>list\[Document\]</code>) – A list of documents to embed. 119 120 **Returns:** 121 122 - <code>dict\[str, list\[Document\] | dict\[str, Any\]\]</code> – A dictionary with: 123 - 'documents': List of Documents with embeddings added 124 - 'meta': Information about the model usage 125 126 ## haystack_integrations.components.embedders.watsonx.text_embedder 127 128 ### WatsonxTextEmbedder 129 130 Embeds strings using IBM watsonx.ai foundation models. 131 132 You can use it to embed user query and send it to an embedding Retriever. 133 134 ### Usage example 135 136 ```python 137 from haystack_integrations.components.embedders.watsonx.text_embedder import WatsonxTextEmbedder 138 139 text_to_embed = "I love pizza!" 140 141 text_embedder = WatsonxTextEmbedder( 142 model="ibm/slate-30m-english-rtrvr-v2", 143 api_key=Secret.from_env_var("WATSONX_API_KEY"), 144 api_base_url="https://us-south.ml.cloud.ibm.com", 145 project_id=Secret.from_env_var("WATSONX_PROJECT_ID"), 146 ) 147 148 print(text_embedder.run(text_to_embed)) 149 150 # {'embedding': [0.017020374536514282, -0.023255806416273117, ...], 151 # 'meta': {'model': 'ibm/slate-30m-english-rtrvr-v2', 152 # 'truncated_input_tokens': 3}} 153 ``` 154 155 #### __init__ 156 157 ```python 158 __init__( 159 *, 160 model: str = "ibm/slate-30m-english-rtrvr-v2", 161 api_key: Secret = Secret.from_env_var("WATSONX_API_KEY"), 162 api_base_url: str = "https://us-south.ml.cloud.ibm.com", 163 project_id: Secret = Secret.from_env_var("WATSONX_PROJECT_ID"), 164 truncate_input_tokens: int | None = None, 165 prefix: str = "", 166 suffix: str = "", 167 timeout: float | None = None, 168 max_retries: int | None = None 169 ) 170 ``` 171 172 Creates an WatsonxTextEmbedder component. 173 174 **Parameters:** 175 176 - **model** (<code>str</code>) – The name of the IBM watsonx model to use for calculating embeddings. 177 Default is "ibm/slate-30m-english-rtrvr-v2". 178 - **api_key** (<code>Secret</code>) – The WATSONX API key. Can be set via environment variable WATSONX_API_KEY. 179 - **api_base_url** (<code>str</code>) – The WATSONX URL for the watsonx.ai service. 180 Default is "https://us-south.ml.cloud.ibm.com". 181 - **project_id** (<code>Secret</code>) – The ID of the Watson Studio project. 182 Can be set via environment variable WATSONX_PROJECT_ID. 183 - **truncate_input_tokens** (<code>int | None</code>) – Maximum number of tokens to use from the input text. 184 If set to `None` (or not provided), the full input text is used, up to the model's maximum token limit. 185 - **prefix** (<code>str</code>) – A string to add at the beginning of each text to embed. 186 - **suffix** (<code>str</code>) – A string to add at the end of each text to embed. 187 - **timeout** (<code>float | None</code>) – Timeout for API requests in seconds. 188 - **max_retries** (<code>int | None</code>) – Maximum number of retries for API requests. 189 190 #### to_dict 191 192 ```python 193 to_dict() -> dict[str, Any] 194 ``` 195 196 Serialize the component to a dictionary. 197 198 **Returns:** 199 200 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 201 202 #### from_dict 203 204 ```python 205 from_dict(data: dict[str, Any]) -> WatsonxTextEmbedder 206 ``` 207 208 Deserializes the component from a dictionary. 209 210 **Parameters:** 211 212 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 213 214 **Returns:** 215 216 - <code>WatsonxTextEmbedder</code> – The deserialized component instance. 217 218 #### run 219 220 ```python 221 run(text: str) -> dict[str, list[float] | dict[str, Any]] 222 ``` 223 224 Embeds a single string. 225 226 **Parameters:** 227 228 - **text** (<code>str</code>) – Text to embed. 229 230 **Returns:** 231 232 - <code>dict\[str, list\[float\] | dict\[str, Any\]\]</code> – A dictionary with: 233 - 'embedding': The embedding of the input text 234 - 'meta': Information about the model usage 235 236 ## haystack_integrations.components.generators.watsonx.chat.chat_generator 237 238 ### WatsonxChatGenerator 239 240 Enables chat completions using IBM's watsonx.ai foundation models. 241 242 This component interacts with IBM's watsonx.ai platform to generate chat responses using various foundation 243 models. It supports the [ChatMessage](https://docs.haystack.deepset.ai/docs/chatmessage) format for both input 244 and output, including multimodal inputs with text and images. 245 246 The generator works with IBM's foundation models that are listed 247 [here](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models.html?context=wx&audience=wdp). 248 249 You can customize the generation behavior by passing parameters to the watsonx.ai API through the 250 `generation_kwargs` argument. These parameters are passed directly to the watsonx.ai inference endpoint. 251 252 For details on watsonx.ai API parameters, see 253 [IBM watsonx.ai documentation](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-parameters.html). 254 255 ### Usage example 256 257 ```python 258 from haystack_integrations.components.generators.watsonx.chat.chat_generator import WatsonxChatGenerator 259 from haystack.dataclasses import ChatMessage 260 from haystack.utils import Secret 261 262 messages = [ChatMessage.from_user("Explain quantum computing in simple terms")] 263 264 client = WatsonxChatGenerator( 265 api_key=Secret.from_env_var("WATSONX_API_KEY"), 266 model="ibm/granite-4-h-small", 267 project_id=Secret.from_env_var("WATSONX_PROJECT_ID"), 268 ) 269 response = client.run(messages) 270 print(response) 271 ``` 272 273 ### Multimodal usage example 274 275 ```python 276 from haystack.dataclasses import ChatMessage, ImageContent 277 278 # Create an image from file path or base64 279 image_content = ImageContent.from_file_path("path/to/your/image.jpg") 280 281 # Create a multimodal message with both text and image 282 messages = [ChatMessage.from_user(content_parts=["What's in this image?", image_content])] 283 284 # Use a multimodal model 285 client = WatsonxChatGenerator( 286 api_key=Secret.from_env_var("WATSONX_API_KEY"), 287 model="meta-llama/llama-3-2-11b-vision-instruct", 288 project_id=Secret.from_env_var("WATSONX_PROJECT_ID"), 289 ) 290 response = client.run(messages) 291 print(response) 292 ``` 293 294 #### SUPPORTED_MODELS 295 296 ```python 297 SUPPORTED_MODELS: list[str] = [ 298 "ibm/granite-3-1-8b-base", 299 "ibm/granite-3-8b-instruct", 300 "ibm/granite-4-h-small", 301 "ibm/granite-8b-code-instruct", 302 "ibm/granite-guardian-3-8b", 303 "meta-llama/llama-3-1-70b-gptq", 304 "meta-llama/llama-3-1-8b", 305 "meta-llama/llama-3-2-11b-vision-instruct", 306 "meta-llama/llama-3-2-90b-vision-instruct", 307 "meta-llama/llama-3-3-70b-instruct", 308 "meta-llama/llama-3-405b-instruct", 309 "meta-llama/llama-4-maverick-17b-128e-instruct-fp8", 310 "meta-llama/llama-guard-3-11b-vision", 311 "mistral-large-2512", 312 "mistralai/mistral-medium-2505", 313 "mistralai/mistral-small-3-1-24b-instruct-2503", 314 "openai/gpt-oss-120b", 315 ] 316 317 ``` 318 319 A non-exhaustive list of models supported by this component. 320 321 See https://www.ibm.com/docs/en/watsonx/saas?topic=solutions-supported-foundation-models for the 322 full list of models and up-to-date model IDs. 323 324 #### __init__ 325 326 ```python 327 __init__( 328 *, 329 api_key: Secret = Secret.from_env_var("WATSONX_API_KEY"), 330 model: str = "ibm/granite-4-h-small", 331 project_id: Secret = Secret.from_env_var("WATSONX_PROJECT_ID"), 332 api_base_url: str = "https://us-south.ml.cloud.ibm.com", 333 generation_kwargs: dict[str, Any] | None = None, 334 timeout: float | None = None, 335 max_retries: int | None = None, 336 verify: bool | str | None = None, 337 streaming_callback: StreamingCallbackT | None = None, 338 tools: ToolsType | None = None 339 ) -> None 340 ``` 341 342 Creates an instance of WatsonxChatGenerator. 343 344 Before initializing the component, you can set environment variables: 345 346 - `WATSONX_TIMEOUT` to override the default timeout 347 - `WATSONX_MAX_RETRIES` to override the default retry count 348 349 **Parameters:** 350 351 - **api_key** (<code>Secret</code>) – IBM Cloud API key for watsonx.ai access. 352 Can be set via `WATSONX_API_KEY` environment variable or passed directly. 353 - **model** (<code>str</code>) – The model ID to use for completions. Defaults to "ibm/granite-4-h-small". 354 Available models can be found in your IBM Cloud account. 355 - **project_id** (<code>Secret</code>) – IBM Cloud project ID 356 - **api_base_url** (<code>str</code>) – Custom base URL for the API endpoint. 357 Defaults to "https://us-south.ml.cloud.ibm.com". 358 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional parameters to control text generation. 359 These parameters are passed directly to the watsonx.ai inference endpoint. 360 Supported parameters include: 361 - `temperature`: Controls randomness (lower = more deterministic) 362 - `max_new_tokens`: Maximum number of tokens to generate 363 - `min_new_tokens`: Minimum number of tokens to generate 364 - `top_p`: Nucleus sampling probability threshold 365 - `top_k`: Number of highest probability tokens to consider 366 - `repetition_penalty`: Penalty for repeated tokens 367 - `length_penalty`: Penalty based on output length 368 - `stop_sequences`: List of sequences where generation should stop 369 - `random_seed`: Seed for reproducible results 370 - **timeout** (<code>float | None</code>) – Timeout in seconds for API requests. 371 Defaults to environment variable `WATSONX_TIMEOUT` or 30 seconds. 372 - **max_retries** (<code>int | None</code>) – Maximum number of retry attempts for failed requests. 373 Defaults to environment variable `WATSONX_MAX_RETRIES` or 5. 374 - **verify** (<code>bool | str | None</code>) – SSL verification setting. Can be: 375 - True: Verify SSL certificates (default) 376 - False: Skip verification (insecure) 377 - Path to CA bundle for custom certificates 378 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function for streaming responses. 379 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls. 380 381 #### to_dict 382 383 ```python 384 to_dict() -> dict[str, Any] 385 ``` 386 387 Serialize the component to a dictionary. 388 389 **Returns:** 390 391 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 392 393 #### from_dict 394 395 ```python 396 from_dict(data: dict[str, Any]) -> WatsonxChatGenerator 397 ``` 398 399 Deserialize this component from a dictionary. 400 401 **Parameters:** 402 403 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 404 405 **Returns:** 406 407 - <code>WatsonxChatGenerator</code> – The deserialized component instance. 408 409 #### run 410 411 ```python 412 run( 413 *, 414 messages: list[ChatMessage], 415 generation_kwargs: dict[str, Any] | None = None, 416 streaming_callback: StreamingCallbackT | None = None, 417 tools: ToolsType | None = None 418 ) -> dict[str, list[ChatMessage]] 419 ``` 420 421 Generate chat completions synchronously. 422 423 **Parameters:** 424 425 - **messages** (<code>list\[ChatMessage\]</code>) – A list of ChatMessage instances representing the input messages. 426 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for text generation. These parameters will potentially override the parameters 427 passed in the `__init__` method. 428 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 429 If provided this will override the `streaming_callback` set in the `__init__` method. 430 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls. 431 If set, it will override the `tools` parameter provided during initialization. 432 433 **Returns:** 434 435 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following key: 436 - `replies`: A list containing the generated responses as ChatMessage instances. 437 438 #### run_async 439 440 ```python 441 run_async( 442 *, 443 messages: list[ChatMessage], 444 generation_kwargs: dict[str, Any] | None = None, 445 streaming_callback: StreamingCallbackT | None = None, 446 tools: ToolsType | None = None 447 ) -> dict[str, list[ChatMessage]] 448 ``` 449 450 Generate chat completions asynchronously. 451 452 **Parameters:** 453 454 - **messages** (<code>list\[ChatMessage\]</code>) – A list of ChatMessage instances representing the input messages. 455 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for text generation. These parameters will potentially override the parameters 456 passed in the `__init__` method. 457 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 458 If provided this will override the `streaming_callback` set in the `__init__` method. 459 - **tools** (<code>ToolsType | None</code>) – A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls. 460 If set, it will override the `tools` parameter provided during initialization. 461 462 **Returns:** 463 464 - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following key: 465 - `replies`: A list containing the generated responses as ChatMessage instances. 466 467 ## haystack_integrations.components.generators.watsonx.generator 468 469 ### WatsonxGenerator 470 471 Bases: <code>WatsonxChatGenerator</code> 472 473 Enables text completions using IBM's watsonx.ai foundation models. 474 475 This component extends WatsonxChatGenerator to provide the standard Generator interface that works with prompt 476 strings instead of ChatMessage objects. 477 478 The generator works with IBM's foundation models that are listed 479 [here](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-models.html?context=wx&audience=wdp). 480 481 You can customize the generation behavior by passing parameters to the watsonx.ai API through the 482 `generation_kwargs` argument. These parameters are passed directly to the watsonx.ai inference endpoint. 483 484 For details on watsonx.ai API parameters, see 485 [IBM watsonx.ai documentation](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-parameters.html). 486 487 ### Usage example 488 489 ```python 490 from haystack_integrations.components.generators.watsonx.generator import WatsonxGenerator 491 from haystack.utils import Secret 492 493 generator = WatsonxGenerator( 494 api_key=Secret.from_env_var("WATSONX_API_KEY"), 495 model="ibm/granite-4-h-small", 496 project_id=Secret.from_env_var("WATSONX_PROJECT_ID"), 497 ) 498 499 response = generator.run( 500 prompt="Explain quantum computing in simple terms", 501 system_prompt="You are a helpful physics teacher.", 502 ) 503 print(response) 504 ``` 505 506 Output: 507 508 ``` 509 { 510 "replies": ["Quantum computing uses quantum-mechanical phenomena like...."], 511 "meta": [ 512 { 513 "model": "ibm/granite-4-h-small", 514 "project_id": "your-project-id", 515 "usage": { 516 "prompt_tokens": 12, 517 "completion_tokens": 45, 518 "total_tokens": 57, 519 }, 520 } 521 ], 522 } 523 ``` 524 525 #### SUPPORTED_MODELS 526 527 ```python 528 SUPPORTED_MODELS: list[str] = [ 529 "ibm/granite-3-1-8b-base", 530 "ibm/granite-3-8b-instruct", 531 "ibm/granite-4-h-small", 532 "ibm/granite-8b-code-instruct", 533 "ibm/granite-guardian-3-8b", 534 "meta-llama/llama-3-1-70b-gptq", 535 "meta-llama/llama-3-1-8b", 536 "meta-llama/llama-3-2-11b-vision-instruct", 537 "meta-llama/llama-3-2-90b-vision-instruct", 538 "meta-llama/llama-3-3-70b-instruct", 539 "meta-llama/llama-3-405b-instruct", 540 "meta-llama/llama-4-maverick-17b-128e-instruct-fp8", 541 "meta-llama/llama-guard-3-11b-vision", 542 "mistral-large-2512", 543 "mistralai/mistral-medium-2505", 544 "mistralai/mistral-small-3-1-24b-instruct-2503", 545 "openai/gpt-oss-120b", 546 ] 547 548 ``` 549 550 A non-exhaustive list of models supported by this component. 551 552 See https://www.ibm.com/docs/en/watsonx/saas?topic=solutions-supported-foundation-models for the 553 full list of models and up-to-date model IDs. 554 555 #### __init__ 556 557 ```python 558 __init__( 559 *, 560 api_key: Secret = Secret.from_env_var("WATSONX_API_KEY"), 561 model: str = "ibm/granite-4-h-small", 562 project_id: Secret = Secret.from_env_var("WATSONX_PROJECT_ID"), 563 api_base_url: str = "https://us-south.ml.cloud.ibm.com", 564 system_prompt: str | None = None, 565 generation_kwargs: dict[str, Any] | None = None, 566 timeout: float | None = None, 567 max_retries: int | None = None, 568 verify: bool | str | None = None, 569 streaming_callback: StreamingCallbackT | None = None 570 ) -> None 571 ``` 572 573 Creates an instance of WatsonxGenerator. 574 575 Before initializing the component, you can set environment variables: 576 577 - `WATSONX_TIMEOUT` to override the default timeout 578 - `WATSONX_MAX_RETRIES` to override the default retry count 579 580 **Parameters:** 581 582 - **api_key** (<code>Secret</code>) – IBM Cloud API key for watsonx.ai access. 583 Can be set via `WATSONX_API_KEY` environment variable or passed directly. 584 - **model** (<code>str</code>) – The model ID to use for completions. Defaults to "ibm/granite-4-h-small". 585 Available models can be found in your IBM Cloud account. 586 - **project_id** (<code>Secret</code>) – IBM Cloud project ID 587 - **api_base_url** (<code>str</code>) – Custom base URL for the API endpoint. 588 Defaults to "https://us-south.ml.cloud.ibm.com". 589 - **system_prompt** (<code>str | None</code>) – The system prompt to use for text generation. 590 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional parameters to control text generation. 591 These parameters are passed directly to the watsonx.ai inference endpoint. 592 Supported parameters include: 593 - `temperature`: Controls randomness (lower = more deterministic) 594 - `max_new_tokens`: Maximum number of tokens to generate 595 - `min_new_tokens`: Minimum number of tokens to generate 596 - `top_p`: Nucleus sampling probability threshold 597 - `top_k`: Number of highest probability tokens to consider 598 - `repetition_penalty`: Penalty for repeated tokens 599 - `length_penalty`: Penalty based on output length 600 - `stop_sequences`: List of sequences where generation should stop 601 - `random_seed`: Seed for reproducible results 602 - **timeout** (<code>float | None</code>) – Timeout in seconds for API requests. 603 Defaults to environment variable `WATSONX_TIMEOUT` or 30 seconds. 604 - **max_retries** (<code>int | None</code>) – Maximum number of retry attempts for failed requests. 605 Defaults to environment variable `WATSONX_MAX_RETRIES` or 5. 606 - **verify** (<code>bool | str | None</code>) – SSL verification setting. Can be: 607 - True: Verify SSL certificates (default) 608 - False: Skip verification (insecure) 609 - Path to CA bundle for custom certificates 610 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function for streaming responses. 611 612 #### to_dict 613 614 ```python 615 to_dict() -> dict[str, Any] 616 ``` 617 618 Serialize the component to a dictionary. 619 620 **Returns:** 621 622 - <code>dict\[str, Any\]</code> – The serialized component as a dictionary. 623 624 #### from_dict 625 626 ```python 627 from_dict(data: dict[str, Any]) -> WatsonxGenerator 628 ``` 629 630 Deserialize this component from a dictionary. 631 632 **Parameters:** 633 634 - **data** (<code>dict\[str, Any\]</code>) – The dictionary representation of this component. 635 636 **Returns:** 637 638 - <code>WatsonxGenerator</code> – The deserialized component instance. 639 640 #### run 641 642 ```python 643 run( 644 *, 645 prompt: str, 646 system_prompt: str | None = None, 647 streaming_callback: StreamingCallbackT | None = None, 648 generation_kwargs: dict[str, Any] | None = None 649 ) -> dict[str, Any] 650 ``` 651 652 Generate text completions synchronously. 653 654 **Parameters:** 655 656 - **prompt** (<code>str</code>) – The input prompt string for text generation. 657 - **system_prompt** (<code>str | None</code>) – An optional system prompt to provide context or instructions for the generation. 658 If not provided, the system prompt set in the `__init__` method will be used. 659 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 660 If provided, this will override the `streaming_callback` set in the `__init__` method. 661 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for text generation. These parameters will potentially override the parameters 662 passed in the `__init__` method. Supported parameters include temperature, max_new_tokens, top_p, etc. 663 664 **Returns:** 665 666 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 667 - `replies`: A list of generated text completions as strings. 668 - `meta`: A list of metadata dictionaries containing information about each generation, 669 including model name, finish reason, and token usage statistics. 670 671 #### run_async 672 673 ```python 674 run_async( 675 *, 676 prompt: str, 677 system_prompt: str | None = None, 678 streaming_callback: StreamingCallbackT | None = None, 679 generation_kwargs: dict[str, Any] | None = None 680 ) -> dict[str, Any] 681 ``` 682 683 Generate text completions asynchronously. 684 685 **Parameters:** 686 687 - **prompt** (<code>str</code>) – The input prompt string for text generation. 688 - **system_prompt** (<code>str | None</code>) – An optional system prompt to provide context or instructions for the generation. 689 - **streaming_callback** (<code>StreamingCallbackT | None</code>) – A callback function that is called when a new token is received from the stream. 690 If provided, this will override the `streaming_callback` set in the `__init__` method. 691 - **generation_kwargs** (<code>dict\[str, Any\] | None</code>) – Additional keyword arguments for text generation. These parameters will potentially override the parameters 692 passed in the `__init__` method. Supported parameters include temperature, max_new_tokens, top_p, etc. 693 694 **Returns:** 695 696 - <code>dict\[str, Any\]</code> – A dictionary with the following keys: 697 - `replies`: A list of generated text completions as strings. 698 - `meta`: A list of metadata dictionaries containing information about each generation, 699 including model name, finish reason, and token usage statistics.