Skip to main content
The Machine Learning query pipeline stage uses a trained machine learning model to analyze a field or fields of a Request object and stores the results of analysis in a new field added to either the Request or the Context object. In order to use the Machine Learning Stage, you must train a machine learning model. There are two different ways to train a model:
  • Use a Fusion job that trains a model, like Classification
  • Develop and Deploy a Machine Learning Model
This tutorial walks you through deploying your own model to Fusion with Seldon Core.

Prerequisites

  • A Fusion instance with an app and indexed data
  • An understanding of Python and the ability to write Python code
  • Docker installed locally, plus a private or public Docker repository
  • Seldon-core installed locally: pip install seldon-core
  • Code editor; you can use any editor, but Visual Studio Code is used in the example
  • Model: paraphrase-multilingual-MiniLM-L12-v2 from Hugging Face
  • Docker image: example_sbert_model

Tips

  • Always test your Python code locally before uploading to Docker and then Fusion. This simplifies troubleshooting significantly.
  • Once you’ve created your Docker you can also test locally by doing docker run with a specified port, like 9000, which you can then curl to confirm functionality in Fusion. See the testing example below.

Intro to Machine Learning in Fusion

The course for Intro to Machine Learning in Fusion focuses on using machine learning to infer the goals of customers and users in order to deliver a more sophisticated search experience.

Local testing example

The examples in this section use the following models:
  1. Docker command:
  2. Curl to hit Docker:
  3. Curl model in Fusion:
  4. See all your deployed models:

Download the model

This tutorial uses the paraphrase-multilingual-MiniLM-L12-v2 model from Hugging Face, but any pre-trained model from https://huggingface.co will work with this tutorial.If you want to use your own model instead, you can do so, but your model must have been trained and then saved though a function similar to the PyTorch’s torch.save(model, PATH) function. See Saving and Loading Models in the PyTorch documentation.

Format a Python class

The next step is to format a Python class which will be invoked by Fusion to get the results from your model. The skeleton below represents the format that you should follow. See also Packaging a Python model for Seldon Core using Docker in the Seldon Core documentation.
A real instance of this class with the Paraphrase Multilingual MiniLM L12 v2 model is as follows:
In the above code, an additional function has been added in the class; this is completely fine to do. Logging has also been added for debugging purposes.Two functions are non-negotiable:
  • init: The init function is where models, tokenizers, vectorizers, and the like should be set to self for invoking.
    It is recommended that you include your model’s trained parameters directly into the Docker container rather than reaching out to external storage inside init.
  • predict: The predict function processes the field or query that Fusion passes to the model.
    The predict function must be able to handle any text processing needed for the model to accept input invoked in its model.evaluate(), model.predict(), or equivalent function to get the expected model result.
    If the output needs additional manipulation, that should be done before the result is returned.
    For embedding models the return value must have the shape of (1, DIM), where DIM (dimension) is a consistent integer, to enable Fusion to handle the vector encoding into Milvus or Solr.
Use the exact name of the class when naming this file.
For the example, above the Python file is named mini.py and the class name is mini().

Create a Dockerfile

The next step is to create a Dockerfile. The Dockerfile should follow this general outline; read the comments for additional details:

Create a requirements file

The requirements.txt file is a list of installs for the Dockerfile to run to ensure the Docker container has the right resources to run the model.
For the Paraphrase Multilingual MiniLM L12 v2 model, the requirements are as follows:
In general, if an item was used in an import statement in your Python file, it should be included in the requirements file.An easy way to populate the requirements is by using in the following command in the terminal, inside the directory that contains your code:
If you use pip freeze, you must manually add seldon-core to the requirements file because it is not invoked in the Python file but is required for containerization.

Build and push the Docker image

After creating the <your_model>.py, Dockerfile, and requirements.txt files, you need to run a few Docker commands. Run the commands below in order:
Using the example model, the terminal commands would be as follows:
This repository is public and you can visit it here: example_sbert_model

Deploy the model in Fusion

Now you can go to Fusion to deploy your model.
  1. In Fusion, navigate to Collections > Jobs.
  2. Add a job by clicking the Add+ Button and selecting Create Seldon Core Model Deployment.
  3. Fill in each of the text fields: Create a Seldon Core model deployment job
  4. Click Save, then Run and Start. Start a Seldon Core model deployment job When the job finishes successfully, you can proceed to the next section.
Now that the model is in Fusion, it can be utilized in either index or query pipelines, depending on the model’s purpose. In this case the model is a word vectorizer or semantic vector search implementation, so both pipelines must invoke the model.

Apply an API key to the deployment

These steps are only needed if your model utilizes any kind of secret, such as an API key. If not, skip this section and proceed to the next.
  1. Create and modify a <seldon_model_name>_sdep.yaml file.
    In the first line, kubectl get sdep gets the details for the currently running Seldon Deployment job and saves those details to a YAML file. kubectl apply -f open_sdep.yaml adds the key to the Seldon Deployment job the next time it launches.
  2. Delete sdep before redeploying the model. The currently running Seldon Deployment job does not have the key applied to it. Delete it before redeploying and the new job will have the key.
  3. Lastly, you can encode into Milvus.

Create a Milvus collection

  1. In Fusion, navigate to Collections > Jobs.
  2. Click the Add+ Button and select Create Collections in Milvus.
    This job creates a collection in Milvus for storing the vectors sent to it. The job is needed because a collection does not automatically spawn at indexing or query time if it does not already exist.
  3. Name the job and the collection.
  4. Click Add on the right side of the job panel.
    The key to creating the collection is the Dimension text field; this must exactly match the shape value your output prediction has.
    In our example the shape is (1,384), so 384 will be in the collections Dimension field: Create a Milvus collection The Metric field should typically be left at the default of Inner Product, but this also depends on use case and model type.
  5. Click Save, then Run and Start.

Configure the Fusion pipelines

Your real-world pipeline configuration depends on your use case and model, but for our example we will configure the index pipeline and then the query pipeline.Configure the index pipeline
  1. Create a new index pipeline or load an existing one for editing.
  2. Click Add a Stage and then Encode to Milvus.
  3. In the new stage, fill in these fields:
    • The name of your model
    • The output name you have for your model job
    • The field you’d like to encode
    • The collection name
  4. Save the stage in the pipeline and index your data with it.
Configure the query pipeline
  1. Create a new query pipeline or load an existing one for editing.
  2. Click Add a Stage and then Milvus Query.
  3. Fill in the configuration fields, then save the stage.
  4. Add a Milvus Ensemble Query stage.
    This stage is necessary to have the Milvus collection scores taken into account in ranking and to weight multiple collections. The Milvus Results Context Key from the Milvus Query Stage is used in this stage to preform math on the Milvus result scores. One (1) is a typical multiplier for the Milvus results but any number can be used.
  5. Save the stage and then run a query by typing a search term.
  6. To verify the Milvus results are correct, use the Compare+ button to see another pipeline without the model implementation and compare the number of results.
You have now successfully uploaded a Seldon Core model to Fusion and deployed it.
This stage requires that you use JavaScript to construct a model input object from the Request and/or Context. This JavaScript is defined in the “Model input transformation script” property. This script must construct a HashMap containing fields and values to be sent to the model. The field names and values will depend on the input schema of the model. Value types supported are:
  • String
  • Double
  • String[]
  • double[]
  • List<String>
  • List<Number>
The JavaScript interpreter that executes the script will have the following variables available in scope: The last line of the script must be a reference to the HashMap object you created. Example 1: Single string parameter from request to modelInput HashMap
Example 2: List of strings from request to modelInput HashMap
Example 3: List of numeric values from request to modelInput HashMap
Similarly, you will need to use JavaScript to store the predictions into the Request and/or Context from the model output object. The model output object is a HashMap containing fields and values produced by the model. The JavaScript interpreter that executes the script will have the following variables available in scope: Example: Place predictedLabel (string) on request

Intro to Machine Learning in Fusion

The course for Intro to Machine Learning in Fusion focuses on using machine learning to infer the goals of customers and users in order to deliver a more sophisticated search experience.

Query pipeline stage condition examples

Stages can be triggered conditionally when a script in the Condition field evaluates to true. Some examples are shown below. Run this stage only for mobile clients:
Run this stage when debugging is enabled:
Run this stage when the query includes a specific term:
Run this stage when multiple conditions are met:
The first condition checks that the request parameter “fusion-user-name” is present and has the value “SuperUser”. The second condition checks that the request parameter “isFusionPluginQuery” is not present.

Configuration