{ "data": { "node": { "template": { "_type": "Component", "api_key": { "load_from_db": true, "required": true, "placeholder": "", "show": true, "name": "api_key", "value": "WATSONX_API_KEY", "display_name": "Watsonx API Key", "advanced": false, "input_types": [], "dynamic": false, "info": "The API Key to use for the model.", "title_case": false, "password": true, "type": "str", "_input_type": "SecretStrInput" }, "code": { "type": "code", "required": true, "placeholder": "", "list": false, "show": true, "multiline": true, "value": "from typing import Any\n\nimport requests\nfrom ibm_watsonx_ai import APIClient, Credentials\nfrom ibm_watsonx_ai.metanames import EmbedTextParamsMetaNames\nfrom langchain_ibm import WatsonxEmbeddings\nfrom pydantic.v1 import SecretStr\n\nfrom lfx.base.embeddings.model import LCEmbeddingsModel\nfrom lfx.field_typing import Embeddings\nfrom lfx.io import BoolInput, DropdownInput, IntInput, SecretStrInput, StrInput\nfrom lfx.log.logger import logger\nfrom lfx.schema.dotdict import dotdict\n\n\nclass WatsonxEmbeddingsComponent(LCEmbeddingsModel):\n display_name = \"IBM watsonx.ai Embeddings\"\n description = \"Generate embeddings using IBM watsonx.ai models.\"\n icon = \"WatsonxAI\"\n name = \"WatsonxEmbeddingsComponent\"\n\n # models present in all the regions\n _default_models = [\n \"sentence-transformers/all-minilm-l12-v2\",\n \"ibm/slate-125m-english-rtrvr-v2\",\n \"ibm/slate-30m-english-rtrvr-v2\",\n \"intfloat/multilingual-e5-large\",\n ]\n\n inputs = [\n DropdownInput(\n name=\"url\",\n display_name=\"watsonx API Endpoint\",\n info=\"The base URL of the API.\",\n value=None,\n options=[\n \"https://us-south.ml.cloud.ibm.com\",\n \"https://eu-de.ml.cloud.ibm.com\",\n \"https://eu-gb.ml.cloud.ibm.com\",\n \"https://au-syd.ml.cloud.ibm.com\",\n \"https://jp-tok.ml.cloud.ibm.com\",\n \"https://ca-tor.ml.cloud.ibm.com\",\n ],\n real_time_refresh=True,\n ),\n StrInput(\n name=\"project_id\",\n display_name=\"watsonx project id\",\n info=\"The project ID or deployment space ID that is associated with the foundation model.\",\n required=True,\n ),\n SecretStrInput(\n name=\"api_key\",\n display_name=\"Watsonx API Key\",\n info=\"The API Key to use for the model.\",\n required=True,\n ),\n DropdownInput(\n name=\"model_name\",\n display_name=\"Model Name\",\n options=[],\n value=None,\n dynamic=True,\n required=True,\n ),\n IntInput(\n name=\"truncate_input_tokens\",\n display_name=\"Truncate Input Tokens\",\n advanced=True,\n value=200,\n ),\n BoolInput(\n name=\"input_text\",\n display_name=\"Include the original text in the output\",\n value=True,\n advanced=True,\n ),\n ]\n\n @staticmethod\n def fetch_models(base_url: str) -> list[str]:\n \"\"\"Fetch available models from the watsonx.ai API.\"\"\"\n try:\n endpoint = f\"{base_url}/ml/v1/foundation_model_specs\"\n params = {\n \"version\": \"2024-09-16\",\n \"filters\": \"function_embedding,!lifecycle_withdrawn:and\",\n }\n response = requests.get(endpoint, params=params, timeout=10)\n response.raise_for_status()\n data = response.json()\n models = [model[\"model_id\"] for model in data.get(\"resources\", [])]\n return sorted(models)\n except Exception: # noqa: BLE001\n logger.exception(\"Error fetching models\")\n return WatsonxEmbeddingsComponent._default_models\n\n def update_build_config(self, build_config: dotdict, field_value: Any, field_name: str | None = None):\n \"\"\"Update model options when URL or API key changes.\"\"\"\n logger.debug(\n \"Updating build config. Field name: %s, Field value: %s\",\n field_name,\n field_value,\n )\n\n if field_name == \"url\" and field_value:\n try:\n models = self.fetch_models(base_url=build_config.url.value)\n build_config.model_name.options = models\n if build_config.model_name.value:\n build_config.model_name.value = models[0]\n info_message = f\"Updated model options: {len(models)} models found in {build_config.url.value}\"\n logger.info(info_message)\n except Exception: # noqa: BLE001\n logger.exception(\"Error updating model options.\")\n\n def build_embeddings(self) -> Embeddings:\n credentials = Credentials(\n api_key=SecretStr(self.api_key).get_secret_value(),\n url=self.url,\n )\n\n api_client = APIClient(credentials)\n\n params = {\n EmbedTextParamsMetaNames.TRUNCATE_INPUT_TOKENS: self.truncate_input_tokens,\n EmbedTextParamsMetaNames.RETURN_OPTIONS: {\"input_text\": self.input_text},\n }\n\n return WatsonxEmbeddings(\n model_id=self.model_name,\n params=params,\n watsonx_client=api_client,\n project_id=self.project_id,\n )\n", "fileTypes": [], "file_path": "", "password": false, "name": "code", "advanced": true, "dynamic": true, "info": "", "load_from_db": false, "title_case": false }, "input_text": { "tool_mode": false, "trace_as_metadata": true, "list": false, "list_add_label": "Add More", "required": false, "placeholder": "", "show": true, "name": "input_text", "value": true, "display_name": "Include the original text in the output", "advanced": true, "dynamic": false, "info": "", "title_case": false, "type": "bool", "_input_type": "BoolInput" }, "model_name": { "tool_mode": false, "trace_as_metadata": true, "options": [], "options_metadata": [], "combobox": false, "dialog_inputs": {}, "toggle": false, "required": true, "placeholder": "", "show": true, "name": "model_name", "display_name": "Model Name", "advanced": false, "dynamic": true, "info": "", "title_case": false, "external_options": {}, "type": "str", "_input_type": "DropdownInput" }, "project_id": { "tool_mode": false, "trace_as_metadata": true, "load_from_db": true, "list": false, "list_add_label": "Add More", "required": true, "placeholder": "", "show": true, "name": "project_id", "value": "WATSONX_PROJECT_ID", "display_name": "watsonx project id", "advanced": false, "dynamic": false, "info": "The project ID or deployment space ID that is associated with the foundation model.", "title_case": false, "type": "str", "_input_type": "StrInput" }, "truncate_input_tokens": { "tool_mode": false, "trace_as_metadata": true, "list": false, "list_add_label": "Add More", "required": false, "placeholder": "", "show": true, "name": "truncate_input_tokens", "value": 200, "display_name": "Truncate Input Tokens", "advanced": true, "dynamic": false, "info": "", "title_case": false, "type": "int", "_input_type": "IntInput" }, "url": { "tool_mode": false, "trace_as_metadata": true, "options": [ "https://us-south.ml.cloud.ibm.com", "https://eu-de.ml.cloud.ibm.com", "https://eu-gb.ml.cloud.ibm.com", "https://au-syd.ml.cloud.ibm.com", "https://jp-tok.ml.cloud.ibm.com", "https://ca-tor.ml.cloud.ibm.com" ], "options_metadata": [], "combobox": false, "dialog_inputs": {}, "toggle": false, "required": false, "placeholder": "", "show": true, "name": "url", "display_name": "watsonx API Endpoint", "advanced": false, "dynamic": false, "info": "The base URL of the API.", "real_time_refresh": true, "title_case": false, "external_options": {}, "type": "str", "_input_type": "DropdownInput" } }, "description": "Generate embeddings using IBM watsonx.ai models.", "icon": "WatsonxAI", "base_classes": ["Embeddings"], "display_name": "IBM watsonx.ai Embeddings", "documentation": "", "minimized": false, "custom_fields": {}, "output_types": [], "pinned": false, "conditional_paths": [], "frozen": false, "outputs": [ { "types": ["Embeddings"], "selected": "Embeddings", "name": "embeddings", "display_name": "Embedding Model", "method": "build_embeddings", "value": "__UNDEFINED__", "cache": true, "allows_loop": false, "group_outputs": false, "tool_mode": true } ], "field_order": [ "url", "project_id", "api_key", "model_name", "truncate_input_tokens", "input_text" ], "beta": false, "legacy": false, "edited": false, "metadata": { "module": "lfx.components.ibm.watsonx_embeddings.WatsonxEmbeddingsComponent", "code_hash": "ffded413ea90", "dependencies": { "total_dependencies": 5, "dependencies": [ { "name": "requests", "version": "2.32.5" }, { "name": "ibm_watsonx_ai", "version": "1.3.34" }, { "name": "langchain_ibm", "version": "0.3.16" }, { "name": "pydantic", "version": "2.10.6" }, { "name": "lfx", "version": null } ] } }, "tool_mode": false, "official": false }, "showNode": true, "type": "WatsonxEmbeddingsComponent", "id": "WatsonxEmbeddingsComponent-q67FN" }, "id": "WatsonxEmbeddingsComponent-q67FN", "position": { "x": 0, "y": 0 }, "type": "genericNode" }