How to pass multiple parameters to a Model API

akshay.ambekar
akshay.ambekar Moderator, Domino Posts: 25 mod

This scenario can come up if you have a lot of parameters (say 20+ predictors) that you want to pass to your Model API and don't want to explicitly list those out to your Model API function. Here's how you can set this up:

Sample model code:

import pandas as pd
 
def predict(multiparams):
    input_df = pd.DataFrame.from_dict(multiparams, orient='index')
    '''
    code to parse input dataframe and utilize the parameter values
    '''  
    return input_df.to_json()

In the above sample code, multiparams is a python dict that can essentially hold all/any number of parameters, be passed to the Model API via JSON (in the below form) and converted to a Pandas DataFrame using the from_dict method for further use.

Deploy the above code as a Model API and test it in the tester block on the published Model API page by

(a) passing just 2 params

{
  "data": {
    "multiparams": {
      "first": 100,
      "second": 200
    }
  }
}

response received:

(b) passing 3 params

{
  "data": {
    "multiparams": {
      "first": 100,
      "second": 200,
      "third": 300
    }
  }
}

response received:

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!