tensorflow / recommenders

TensorFlow Recommenders is a library for building recommender system models using TensorFlow.
Apache License 2.0
1.82k stars 273 forks source link

[Question] Serving a Retrieval Model with multiple signatures #692

Closed ericy51 closed 1 year ago

ericy51 commented 1 year ago

I believe I've been able to successfully leverage the signature parameter when I save my retrieval model and am using this to filter out certain recommendations which I'm planning to use for different page contexts on an ecommerce site.

RecLookup = tfrs.layers.factorized_top_k.BruteForce(model.query_model, k =10)
#recommends products out of the entire product dataset.
RecLookup.index_from_dataset(
  tf.data.Dataset.zip((product_lookup.batch(100), products.batch(100).map(model.candidate_model)))
)
RecLookup.compile()

#Need to call it to set the shapes.
query = dict(df_temp.iloc[0].map(lambda x: tf.expand_dims(x,axis=0)))
_, titles= RecLookup(query)

#adds signature for variable return length
recommendations_at_10 = tf.function(lambda query: RecLookup(query, k=10))
a,b = recommendations_at_10(query)

#adds signature for variable exclusions
excluded_recoms = tf.function(lambda query:
                              RecLookup.query_with_exclusions(query, k=10, exclusions=excl))
c,d = excluded_recoms(query)`

#SavedModel
tf.keras.models.save_model(RecLookup
                           ,model_path
                           ,signatures={"serving_default": recommendations_at_10.get_concrete_function(tensor_specs)
                                        ,"excl_10": excluded_recoms.get_concrete_function(tensor_specs)}
                          )

I'm able to reference these signatures locally when I load my model

loaded = tf.keras.models.load_model(path)
loaded.signatures["excl_10"]

However I want to be able to serve these results via an inference endpoint in Sagemaker. Is there a way to reference saved model signatures with custom headers or any other parameters via an HTTP request? Or possibly this is the wrong approach altogether? Below is the request code for Sagemaker but it doesn't look like there's any ability to add additional parameters?

client = boto3.client('sagemaker-runtime')

endpoint_name = endpoint_name    
content_type = accept = 'application/json' 
payload = json_data      

response = client.invoke_endpoint(
    EndpointName=endpoint_name, 
    ContentType=content_type,
    Accept=accept,
    Body=payload    )
ericy51 commented 1 year ago

I was finally able to get this working with the help of this documentation:

https://www.tensorflow.org/tfx/serving/api_rest

For my specific setup this is the structure of the payload that got me results:

{"signature_name": "excl_10", "inputs":{"id":["am_183827917602"] ,"beauty": [0.85] ,"fashion": [0.04] ,"wellness": [0.0] , "home":[0.09]}}