> ## Documentation Index
> Fetch the complete documentation index at: https://doc.lucidworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lucidworks AI Prediction index stage

> Lucidworks AI

export const schema = {
  "type": "object",
  "title": "LWAI Prediction",
  "description": "General purpose integration between Fusion and Lucidworks AI.  By default, this stage is asynchronous, meaning that it will not block the pipeline while waiting for a response from Lucidworks AI.  This default asynchronous configuration may benefit from the use of other stages, e.g. 'Solr Asynchronous Query' to incrementally handle the response from Lucidworks AI.",
  "required": ["accountName", "useCase", "modelName", "inputContextVariable", "outputContextVariable"],
  "properties": {
    "skip": {
      "type": "boolean",
      "title": "Skip This Stage",
      "description": "Set to true to skip this stage.",
      "default": false,
      "hints": ["advanced"]
    },
    "label": {
      "type": "string",
      "title": "Label",
      "description": "A unique label for this stage.",
      "hints": ["advanced"],
      "maxLength": 255
    },
    "condition": {
      "type": "string",
      "title": "Condition",
      "description": "Define a conditional script that must result in true or false. This can be used to determine if the stage should process or not.",
      "hints": ["code", "code/javascript", "advanced"]
    },
    "accountName": {
      "type": "string",
      "title": "Account Name",
      "description": "Lucidworks AI API Account Name as defined in AI Gateway Service.  This entry should match the account name set in the AI Gateway.",
      "hints": ["enumUrl:/api/query-stages/lwai-accounts"]
    },
    "useCase": {
      "type": "string",
      "title": "Use Case",
      "description": "Lucidworks AI Use Case as defined in documentation",
      "hints": ["enumUrl:/api/query-stages/lwai-usecase?account=${accountName}&caller=index"]
    },
    "modelName": {
      "type": "string",
      "title": "Model",
      "description": "Lucidworks AI Model as defined in documentation",
      "hints": ["enumUrl:/api/query-stages/lwai-model?account=${accountName}&useCase=${useCase}"]
    },
    "inputContextVariable": {
      "type": "string",
      "title": "Input context variable",
      "description": "Name of the variable in context to be used as input. Supports template expressions."
    },
    "outputContextVariable": {
      "type": "string",
      "title": "Destination Field Name & Context Output",
      "description": "The name here is used to populate two things with the prediction results:  1) The field name in the document that will contain the prediction, and 2) The name of the context variable that will contain the prediction."
    },
    "useCaseConfig": {
      "type": "array",
      "title": "Use Case Configuration",
      "description": "Additional Use Case keys and values to be sent to Lucidworks AI",
      "minItems": 0,
      "items": {
        "type": "object",
        "required": ["key"],
        "properties": {
          "key": {
            "type": "string",
            "title": "Parameter Name"
          },
          "value": {
            "type": "string",
            "title": "Parameter Value"
          }
        }
      }
    },
    "modelConfig": {
      "type": "array",
      "title": "Model Configuration",
      "description": "Additional Model configuration parameters to be sent to Lucidworks AI",
      "minItems": 0,
      "items": {
        "type": "object",
        "required": ["key"],
        "properties": {
          "key": {
            "type": "string",
            "title": "Parameter Name"
          },
          "value": {
            "type": "string",
            "title": "Parameter Value"
          }
        }
      }
    },
    "apiKey": {
      "type": "string",
      "title": "API Key",
      "description": "Secret associated with the model.  For example, for OpenAI models, the value would start with 'sk-'.",
      "hints": ["secret"]
    },
    "callAsynchronously": {
      "type": "boolean",
      "title": "Call Asynchronously?",
      "description": "Flag to indicate if this stage should call the Lucidworks AI API asynchronously or synchronously. Defaults to true.",
      "default": true
    },
    "maxTries": {
      "type": "integer",
      "title": "Maximum Asynchronous Call Tries",
      "description": "The maximum number of attempts to issue an asynchronous Lucidworks AI API call",
      "default": 1,
      "minimum": 1,
      "exclusiveMinimum": false
    },
    "failOnError": {
      "type": "boolean",
      "title": "Fail on Error",
      "description": "Flag to indicate if this stage should throw an exception if an error occurs while generating a prediction for a document.",
      "default": false
    }
  },
  "category": "AI",
  "categoryPriority": 10,
  "unsafe": false
};

export const SchemaParamFields = ({schema}) => {
  const sanitize = str => {
    if (typeof str !== "string") return str;
    return str.replace(/^"(.*)"$/s, "$1").replace(/\\/g, "").replace(/"/g, "'");
  };
  const formatDescription = str => {
    const s = sanitize(str);
    return (/[.!?]\)*$/).test(s) ? s : `${s}.`;
  };
  const {description, properties = {}, required: requiredProps = []} = schema;
  const visibleProps = useMemo(() => Object.entries(properties).filter(([, prop]) => !prop.hints?.includes("hidden")), [properties]);
  return <div>
      {description && <p>{formatDescription(description)}</p>}

      {visibleProps.map(([name, prop]) => {
    const isRequired = requiredProps.includes(name);
    const hasDefault = prop.default !== undefined;
    const rawDefault = prop.default;
    const isComplexDefault = hasDefault && (typeof rawDefault === "object" || typeof rawDefault === "string" && (rawDefault.length > 20 || rawDefault.includes('"')));
    const fieldProps = {
      key: name,
      body: prop.title || name,
      type: prop.type,
      ...prop.title && ({
        post: [<><span className="text-stone-400 dark:text-stone-500">API property: </span>{name}</>]
      }),
      ...isRequired && ({
        required: true
      }),
      ...!isComplexDefault && hasDefault ? {
        default: sanitize(String(rawDefault))
      } : {}
    };
    const isObject = prop.type === "object" && prop.properties;
    const isArrayOfObjects = prop.type === "array" && prop.items?.type === "object" && prop.items.properties;
    return <ParamField {...fieldProps}>
            {prop.description && <p>{formatDescription(prop.description)}</p>}

            {isComplexDefault && <div className="flex">
                <p>
                  <strong>Default:</strong>
                </p>
                <pre className="!my-0">
                  <code>
                    {JSON.stringify(rawDefault, null, 2)}
                  </code>
                </pre>
              </div>}

            {isArrayOfObjects && <div className="flex">
              <p>
                <strong>Object attributes:</strong>
              </p>
              <pre className="!my-0">
                <code>
                  {'{\n'}
                  {Object.entries(prop.items.properties).map(([iname, iprop]) => <>
                      {`  ${iname}`}
                      {prop.items?.required?.includes(iname) && <span style={{
      color: 'red'
    }}> required</span>}
                      {`: {\n    display name: ${sanitize(iprop.title || '')}\n    type: ${iprop.type}\n  }\n`}
                    </>)}
                  {'}'}
                </code>
              </pre>
              </div>}

            {isObject && <Expandable title="properties">
                <SchemaParamFields schema={{
      properties: prop.properties,
      required: prop.required
    }} />
              </Expandable>}
          </ParamField>;
  })}
    </div>;
};

export const LwTemplate = ({title = "Key questions to get you started", icon = "sparkles", cta = "Powered by Agent Studio", linkHref = "https://lucidworks.com/demo/?utm_source=docs&utm_medium=referral&utm_campaign=docs_cta_ai"}) => {
  const [isLoaded, setIsLoaded] = useState(false);
  useEffect(() => {
    const timer = setTimeout(() => {
      setIsLoaded(true);
    }, 500);
    return () => clearTimeout(timer);
  }, []);
  return <div className="lw-template-container">
      <Card title={title} icon={icon}>
        {isLoaded && <span dangerouslySetInnerHTML={{
    __html: `<lw-template id="a029c1a9-28be-427e-b0e1-5d918920246a"></lw-template
            >`
  }} />}
        <Link href={linkHref} className="agent-studio-link text-left text-gray-600 gap-2 dark:text-gray-400 text-sm font-medium flex flex-row items-center hover:text-primary dark:hover:text-primary-light group-hover:text-primary group-hover:dark:text-primary-light">Powered by Lucidworks Agent Studio</Link>
      </Card>
    </div>;
};

[localhost link]: http://localhost:3000/docs/lw-platform/lw-ai/lw-ai-stages/lucidworks-ai-prediction-index-stage

[mintlify link]: https://doc.lucidworks.com/docs/lw-platform/lw-ai/lw-ai-stages/lucidworks-ai-prediction-index-stage

[old doc.lw link]: https://doc.lucidworks.com/fusion/5.9/bzik28

The LWAI Prediction index stage is an integration between Fusion and Lucidworks AI to enrich your index with [Generative AI](/docs/lw-platform/lw-ai/lw-ai-generative-ai) predictions.

Generative AI predictions can enrich product descriptions and other metadata that improves search relevance and recommendations. In addition, this stage can return summaries of long documents or clarify descriptions and other data.

For example, if you are a B2B organization that provides industrial technology components and information, this stage can enhance product part descriptions, and category classification and tags that improve search relevance. For technical specifications and detailed instructional guides, the stage can summarize that information and enhance discoverability. In a similar fashion, the stage can enrich catalog items and information to improve relevance and customer search experiences for B2C organizations.

For additional details about configuration, see the [APIs](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-prediction-api/overview).

This stage can be run [asynchronously](/docs/lw-platform/lw-ai/lw-ai-stages/overview#run-stages-asynchronously), which does not block the pipeline while waiting for a response from Lucidworks AI.

<Accordion title="Configure the LWAI Prediction index Stage">
  The LWAI Prediction index stage is a Fusion index pipeline stage that enriches your index with [Generative AI](/docs/lw-platform/lw-ai/lw-ai-generative-ai) predictions.
  It defaults to asynchronous processing, which does not block the pipeline while waiting for a response from Lucidworks AI.

  For reference information, see [LWAI Prediction index stage](/docs/lucidworks-search/09-developer-documentation/config-specs/index-pipeline-stages/lucidworks-ai-prediction-index-stage).

  To use this stage, non-admin Fusion users must be granted the `PUT,POST,GET:/LWAI-ACCOUNT-NAME/**` permission in Fusion, which is the Lucidworks AI API Account Name defined in Lucidworks AI Gateway when this stage is configured.

  <Note>
    This section describes how to configure the stage when your organization generates and manages the authentication and API keys. If your organization has opted to contract with Lucidworks to provide the API keys to execute generative AI (Gen-AI) models, see [Generative AI using Lucidworks-managed keys](/docs/lw-platform/lw-ai/lw-ai-generative-ai-using-lw-keys).
  </Note>

  To configure this stage:

  1. Sign in to Fusion and click **Indexing > Index Pipelines**.

  2. Click **Add+** to add a new pipeline.

  3. Enter the name in **Pipeline ID**.

  4. Click **Add a new pipeline stage**.

  5. In the AI section, click **LWAI Prediction**.

  6. In the **Label** field, enter a unique identifier for this stage.

  7. In the **Condition** field, enter a script that results in true or false, which determines if the stage should process.

  8. In the **Account Name** field, select the Lucidworks AI API account name defined in [Lucidworks AI Gateway](/docs/lw-platform/lw-ai/lw-ai-gateway).

  9. In the **Use Case** field, select the Lucidworks AI use case to associate with this stage.
     * To generate a list of the use cases for your organization, see [Use Case API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-use-case-api).
     * If the **Call Asynchronously?** check box is selected, see available use cases described in [Async Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-async-prediction-api/overview).
     * If the **Call Asynchronously?** check box is *not* selected, see available use cases described in [Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-prediction-api/overview).

  10. In the **Model** field, select the Lucidworks AI model to associate with this stage.

      Your Fusion account name must match the name of the account that you selected in the **Account Name** dropdown.\
      For more information about models, see:

      * [Pre-trained embedding models](/docs/lw-platform/lw-ai/lw-ai-pre-trained-embedding-models)
      * [Custom embedding model training](/docs/lw-platform/lw-ai/lw-ai-custom-embedding-model-training/overview)
      * [Generative AI models](/docs/lw-platform/lw-ai/lw-ai-generative-ai)

  11. In the **Input context variable** variable field, enter the name of the variable in context to be used as input. Template expressions are supported.

  12. In the **Destination field name and context output** field, enter the name that will be used as both the field name in the document where the prediction is written and the context variable that contains the prediction.

      {/* // tag::lwai-prediction-index-stage[] */}

      * If the **Call Asynchronously?** check box is selected and a value is entered in this field:

      * `{destination name}_t` is the full response.

      * In the document:

        * `_lw_ai_properties_ss` contains the Lucidworks account, boolean setting for async, use case, input for the call, and the collection.

        * `_lw_ai_request_count` is the number of GET requests by `predictionId` and `_lw_ai_success_count` is the number of responses without errors. These two fields are used for debugging only. Based on the deployment, the most useful measure is the ratio of  `_lw_ai_success_count` divided by \`\_lw\_ai\_request\_count and then adjusting as much as possible to achieve 1.0.

        * `enriched_ss` contains the use case. This can be used as a boolean value to verify if the use case indexed successfully.

      * If the **Call Asynchronously?** check box is *not* selected and a value is entered in this field:

      * `{destination name}_t`  is the full response.

      * If *no* value is entered in this field (regardless of the **Call Asynchronously?** check box setting):

      * `_lw_ai_{use case}_t` is the `response.response` object, which is the raw model output.

      * `_lw_ai_{use case}_response_s` is the full response.

      {/* // end::lwai-prediction-index-stage[] */}

  13. In the **Use Case Configuration** section, click the **+** sign to enter the parameter name and value to send to Lucidworks AI. The `useCaseConfig` parameter is only applicable to certain use cases.
      * If the **Call Asynchronously?** check box is selected, `useCaseConfig` information for each applicable use case is described in [Async Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-async-prediction-api/overview).
      * If the **Call Asynchronously?** check box is *not* selected, `useCaseConfig` information for each applicable use case is described in [Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-prediction-api/overview).

  14. In the **Model Configuration** section, click the **+** sign to enter the parameter name and value to send to Lucidworks AI. Several `modelConfig` parameters are common to generative AI use cases.
      * If the **Call Asynchronously?** check box is selected, `modelConfig` information is described in [Async Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-async-prediction-api/overview).
      * If the **Call Asynchronously?** check box is *not* selected, `modelConfig` information is described in [Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-prediction-api/overview).

  15. In the **API Key** field, enter the secret value specified in the external model. For:

      * OpenAI models, `"apiKey"` is the value in the model’s `"[OPENAI_API_KEY]"` field. For more information, see [Authentication API keys](https://platform.openai.com/docs/api-reference/authentication).
      * Azure OpenAI models, `"apiKey"` is the value generated by Azure in either the model’s `"[KEY1 or KEY2]"` field. For requirements to use Azure models, see [Generative AI models](/docs/lw-platform/lw-ai/lw-ai-generative-ai).
      * Google VertexAI models, `"apiKey"` is the value in the model’s

      `"[BASE64_ENCODED_GOOGLE_SERVICE_ACCOUNT_KEY]"` field. For more information, see [Create and delete Google service account keys](https://cloud.google.com/iam/docs/keys-create-delete).

  16. To run the API call asynchronously, select the **Call Asynchronously?** check box to specify the stage is to use the [Lucidworks AI Async Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-async-prediction-api/overview) endpoints. If this is selected, the API call does not block the pipeline while waiting for a response from Lucidworks AI.

      If the check box is *not* selected, the API call uses the [Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-prediction-api/overview), which uses the pipeline until a response is received from Lucidworks AI. Performance of other API calls can be impacted.

  17. In the **Maximum Asynchronous Call Tries** field, enter the maximum number of times to send an asynchronous API call before the system generates a failure error.

  18. Select the **Fail on Error** checkbox to generate an exception if an error occurs while generating a prediction for a document.

  19. Click **Save**.

  <LwTemplate />

  ## Additional requirements

  Additional requirements to use async calls include:

  * Use a V2 connector. Only V2 connectors work for this task and not other options, such as PBL or V1 connectors.
  * Remove the `Apache Tika` stage from your parser because it can cause datasource failures with the following error: "The following components failed: \[class com.lucidworks.connectors.service.components.job.processor.DefaultDataProcessor : Only Tika Container parser can support Async Parsing.]"
  * Replace the `Solr Indexer` stage with the `Solr Partial Update Indexer` stage with the following settings:
    * `Enable Concurrency Control` set to off
    * `Reject Update if Solr Document is not Present` set to off
    * `Process All Pipeline Doc Fields` set to on
    * `Allow reserved fields` set to on
    * A parameter with `Update Type`, `Field Name` & `Value` in `Updates`
</Accordion>

By default, the Fusion `Call Asynchronously?` field is selected, which specifies this stage uses the [Lucidworks AI Async Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-async-prediction-api/overview) endpoints.

If the Fusion `Call Asynchronously?` field is *not* selected, this stage uses the [LWAI Prediction API](/docs/lw-platform/lw-ai/lw-ai-apis/lw-ai-prediction-api/overview) endpoints.

To use this stage, non-admin Fusion users must be granted the `PUT,POST,GET:/LWAI-ACCOUNT-NAME/**` permission in Fusion, which is the Lucidworks AI API Account Name defined in [Lucidworks AI Gateway](/docs/lw-platform/lw-ai/lw-ai-gateway) when this stage is configured.

## Configuration

<Tip>
  When entering configuration values in the UI, use *unescaped* characters, such as `\t` for the tab character. When entering configuration values in the API, use *escaped* characters, such as `\\t` for the tab character.
</Tip>

<SchemaParamFields schema={schema} />
