openrag/flows/components/watsonx_embedding.json
2025-09-22 12:58:03 -03:00

227 lines
11 KiB
JSON

{
"data": {
"id": "WatsonxEmbeddingsComponent-pJfXI",
"node": {
"base_classes": ["Embeddings"],
"beta": false,
"conditional_paths": [],
"custom_fields": {},
"description": "Generate embeddings using IBM watsonx.ai models.",
"display_name": "IBM watsonx.ai Embeddings",
"documentation": "",
"edited": false,
"field_order": [
"url",
"project_id",
"api_key",
"model_name",
"truncate_input_tokens",
"input_text"
],
"frozen": false,
"icon": "WatsonxAI",
"legacy": false,
"metadata": {
"code_hash": "b6c6d50cc7ed",
"dependencies": {
"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": "langflow",
"version": "1.5.0.post2"
}
],
"total_dependencies": 5
},
"module": "langflow.components.ibm.watsonx_embeddings.WatsonxEmbeddingsComponent"
},
"minimized": false,
"output_types": [],
"outputs": [
{
"allows_loop": false,
"cache": true,
"display_name": "Embedding Model",
"group_outputs": false,
"method": "build_embeddings",
"name": "embeddings",
"selected": "Embeddings",
"tool_mode": true,
"types": ["Embeddings"],
"value": "__UNDEFINED__"
}
],
"pinned": false,
"template": {
"_type": "Component",
"api_key": {
"_input_type": "SecretStrInput",
"advanced": false,
"display_name": "API Key",
"dynamic": false,
"info": "The API Key to use for the model.",
"input_types": [],
"load_from_db": true,
"name": "api_key",
"password": true,
"placeholder": "",
"required": true,
"show": true,
"title_case": false,
"type": "str",
"value": "WATSONX_API_KEY"
},
"code": {
"advanced": true,
"dynamic": true,
"fileTypes": [],
"file_path": "",
"info": "",
"list": false,
"load_from_db": false,
"multiline": true,
"name": "code",
"password": false,
"placeholder": "",
"required": true,
"show": true,
"title_case": false,
"type": "code",
"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 langflow.base.embeddings.model import LCEmbeddingsModel\nfrom langflow.field_typing import Embeddings\nfrom langflow.io import BoolInput, DropdownInput, IntInput, SecretStrInput, StrInput\nfrom langflow.logging.logger import logger\nfrom langflow.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=\"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"
},
"input_text": {
"_input_type": "BoolInput",
"advanced": true,
"display_name": "Include the original text in the output",
"dynamic": false,
"info": "",
"list": false,
"list_add_label": "Add More",
"name": "input_text",
"placeholder": "",
"required": false,
"show": true,
"title_case": false,
"tool_mode": false,
"trace_as_metadata": true,
"type": "bool",
"value": true
},
"model_name": {
"_input_type": "DropdownInput",
"advanced": false,
"combobox": false,
"dialog_inputs": {},
"display_name": "Model Name",
"dynamic": true,
"info": "",
"name": "model_name",
"options": [],
"options_metadata": [],
"placeholder": "",
"required": true,
"show": true,
"title_case": false,
"toggle": false,
"tool_mode": false,
"trace_as_metadata": true,
"type": "str"
},
"project_id": {
"_input_type": "StrInput",
"advanced": false,
"display_name": "watsonx project id",
"dynamic": false,
"info": "The project ID or deployment space ID that is associated with the foundation model.",
"list": false,
"list_add_label": "Add More",
"load_from_db": true,
"name": "project_id",
"placeholder": "",
"required": true,
"show": true,
"title_case": false,
"tool_mode": false,
"trace_as_metadata": true,
"type": "str",
"value": "WATSONX_PROJECT_ID"
},
"truncate_input_tokens": {
"_input_type": "IntInput",
"advanced": true,
"display_name": "Truncate Input Tokens",
"dynamic": false,
"info": "",
"list": false,
"list_add_label": "Add More",
"name": "truncate_input_tokens",
"placeholder": "",
"required": false,
"show": true,
"title_case": false,
"tool_mode": false,
"trace_as_metadata": true,
"type": "int",
"value": 200
},
"url": {
"_input_type": "DropdownInput",
"advanced": false,
"combobox": false,
"dialog_inputs": {},
"display_name": "watsonx API Endpoint",
"dynamic": false,
"info": "The base URL of the API.",
"name": "url",
"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": [],
"placeholder": "",
"real_time_refresh": true,
"required": false,
"show": true,
"title_case": false,
"toggle": false,
"tool_mode": false,
"trace_as_metadata": true,
"type": "str"
}
},
"tool_mode": false
},
"showNode": true,
"type": "WatsonxEmbeddingsComponent"
},
"id": "WatsonxEmbeddingsComponent-pJfXI",
"measured": {
"height": 467,
"width": 320
},
"position": {
"x": 999.129592360849,
"y": 753.2332292351236
},
"selected": false,
"type": "genericNode"
}