How can I do recommendations with Marqo?

57 views Asked by At

I would like to use Marqo to get recommendations of a similar nature to the query from the database.

Instead of querying the index with text, I want to search with an existing document from the index. The idea is I can get similar documents (i.e recommendations).

I have downloaded the marqo package in python and followed through the docs, but I do not know how to query with an existing document from the index.

1

There are 1 answers

0
crabdog On

You can implement a recommendation system in Marqo by searching with the vector of something that’s already in your index. This will return neighbouring products, you can also combine this with search terms to combine a search and recommendation effect into one action. In code this pattern would look like:

# get the document with the embedding for the item you want to recommend based off
item_with_facets = mq.index("my-index").get_document(
    document_id=item_id, expose_facets=True
)
# get the vector for the item
vec = item_with_facets["_tensor_facets"][0]["_embedding"]
# search with the vector
result = mq.index("my-index").search(
    q=None,
    context={"tensor": [{"vector": vec, "weight": 1}]},
    filter_string=f"NOT _id:{item_id}", # filter out the item so it doesn't recommend itself
    limit=num_results,
)

You can also include a search term like so:

result = mq.index("my-index").search(
    q={"shirt": 1.0},
    context={"tensor": [{"vector": vec, "weight": 0.2}]},
    filter_string=f"NOT _id:{item_id}",
    limit=num_results,
)

This with search for "shirt" and the vector of item_id with a weighted mean using the weights of 1 and 0.2.