Skip to main content

Tensor search for humans

Project description

Marqo

Marqo

Tensor search for humans.

PyPI - Downloads from official pypistats

An open-source tensor search engine that seamlessly integrates with your applications, websites, and workflow.

What is tensor search?

Tensor search involves using deep-learning to transform documents, images and other data into collections of vectors called "tensors". Representing data as tensors allows us to match queries against documents with human-like understanding of the query and document's content. Tensor search can power a variety of use cases such as:

  • end user search and recommendations
  • multi-modal search (image-to-image, text-to-image, image-to-text)
  • chat bots and question and answer systems
  • text and image classification

Getting started

  1. Marqo requires docker. To install docker go to https://docs.docker.com/get-docker/
  2. Use docker to run Marqo (Mac users with M series chips will need to go here):
docker pull marqoai/marqo:0.0.3;
docker rm -f marqo;
docker run --name marqo -it --privileged -p 8882:8882 --add-host host.docker.internal:host-gateway marqoai/marqo:0.0.3
  1. Install the Marqo client:
pip install marqo
  1. Start indexing and searching! Let's look at a simple example below:
import marqo

mq = marqo.Client(url='http://localhost:8882')

mq.index("my-first-index").add_documents([
    {
        "Title": "The Travels of Marco Polo",
        "Description": "A 13th-century travelogue describing Polo's travels"
    }, 
    {
        "Title": "Extravehicular Mobility Unit (EMU)",
        "Description": "The EMU is a spacesuit that provides environmental protection, "
                       "mobility, life support, and communications for astronauts",
        "_id": "article_591"
    }]
)

results = mq.index("my-first-index").search(
    q="What is the best outfit to wear on the moon?"
)
  • mq is the client that wraps themarqo API
  • add_documents() takes a list of documents, represented as python dicts, for indexing
  • add_documents() creates an index with default settings, if one does not already exist
  • You can optionally set a document's ID with the special _id field. Otherwise, marqo will generate one.
  • If the index doesn't exist, Marqo will create it. If it exists then Marqo will add the documents to the index.

Let's have a look at the results:

# let's print out the results:
import pprint
pprint.pprint(results)

{
    'hits': [
        {   
            'Title': 'Extravehicular Mobility Unit (EMU)',
            'Description': 'The EMU is a spacesuit that provides environmental protection, mobility, life support, and' 
                           'communications for astronauts',
            '_highlights': {
                'Description': 'The EMU is a spacesuit that provides environmental protection, '
                               'mobility, life support, and communications for astronauts'
            },
            '_id': 'article_591',
            '_score': 0.61938936
        }, 
        {   
            'Title': 'The Travels of Marco Polo',
            'Description': "A 13th-century travelogue describing Polo's travels",
            '_highlights': {'Title': 'The Travels of Marco Polo'},
            '_id': 'e00d1a8d-894c-41a1-8e3b-d8b2a8fce12a',
            '_score': 0.60237324
        }
    ],
    'limit': 10,
    'processingTimeMs': 49,
    'query': 'What is the best outfit to wear on the moon?'
}
  • Each hit corresponds to a document that matched the search query
  • They are ordered from most to least matching
  • limit is the maximum number of hits to be returned. This can be set as a parameter during search
  • Each hit has a _highlights field. This was the part of the document that matched the query the best

Other basic operations

Get document

Retrieve a document by ID.

result = mq.index("my-first-index").get_document(document_id="article_591")

Note that by adding the document using add_documents again using the same _id will cause a document to be updated.

Get index stats

Get information about an index.

results = mq.index("my-first-index").get_stats()

Lexical search

Perform a keyword search.

result =  mq.index("my-first-index").search('marco polo', search_method=marqo.SearchMethods.LEXICAL)

Search specific fields

Using the default tensor search method

result = mq.index("my-first-index").search('adventure', searchable_attributes=['Title'])

Delete documents

Delete documents.

results = mq.index("my-first-index").delete_documents(ids=["article_591", "article_602"])

Delete index

Delete an index.

results = mq.index("my-first-index").delete()

Multi modal and cross modal search

To power image and text search, Marqo allows users to plug and play with CLIP models from HuggingFace. Note that if you do not configure multi modal search, image urls will be treated as strings. To start indexing and searching with images, first create an index with a CLIP configuration, as below:

settings = {
  "treat_urls_and_pointers_as_images":True,   # allows us to find an image file and index it 
  "model":"ViT-B/32"
}
response = mq.create_index("my-multimodal-index", **settings)

Images can then be added within documents as follows. You can use urls from the internet (for example S3) or from the disk of the machine:

response = mq.index("my-multimodal-index").add_documents([{
    "My Image": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/Portrait_Hippopotamus_in_the_water.jpg/440px-Portrait_Hippopotamus_in_the_water.jpg",
    "Description": "The hippopotamus, also called the common hippopotamus or river hippopotamus, is a large semiaquatic mammal native to sub-Saharan Africa",
    "_id": "hippo-facts"
}])

You can then search using text as usual. Both text and image fields will be searched:

results = mq.index("my-multimodal-index").search('animal')

Setting searchable_attributes to the image field ['My Image'] ensures only images are searched in this index:

results = mq.index("my-multimodal-index").search('animal',  searchable_attributes=['My Image'])

Searching using an image

Searching using an image can be achieved by providing the image link.

results = mq.index("my-multimodal-index").search('https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/Standing_Hippopotamus_MET_DP248993.jpg/440px-Standing_Hippopotamus_MET_DP248993.jpg')

Warning

Note that you should not run other applications on Marqo's Opensearch cluster as Marqo automatically changes and adapts the settings on the cluster.

M series Mac users

The backend, marqo-os (Marqo-OpenSearch) isn't yet supported for the arm64 architecture. This means that if you have an M series Mac, you will need to run OpenSearch locally. This unfortunately means that you won't be able to use the filtering feature for tensor search queries. We are working on an arm64 Marqo-OpenSearch build as a top priority.

To run Marqo on an M series Mac, follow the next steps.

  1. In one terminal run the following command to start opensearch:
docker rm -f marqo-os; docker run -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" opensearchproject/opensearch:2.2.1
  1. In another terminal run the following command to launch Marqo:
docker rm -f marqo; docker run --name marqo --privileged \
    -p 8882:8882 --add-host host.docker.internal:host-gateway \
    -e "OPENSEARCH_URL=https://localhost:9200" \
    marqoai/marqo:0.0.3

Contributors

Marqo is a community project with the goal of making tensor search accessible to the wider developer community. We are glad that you are interested in helping out! Please read this to get started

Dev set up

  1. Create a virtual env python -m venv ./venv
  2. Activate the virtual environment source ./venv/bin/activate
  3. Install requirements from the requirements file: pip install -r requirements.txt
  4. Run tests by running the tox file. CD into this dir and then run "tox"
  5. If you update dependencies, make sure to delete the .tox dir and rerun

Merge instructions:

  1. Run the full test suite (by using the command tox in this dir).
  2. Create a pull request with an attached github issue.

Support

  • Join our Slack community and chat with other community members about ideas.
  • Marqo community meetings (coming soon!)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marqo-0.1.16.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

marqo-0.1.16-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file marqo-0.1.16.tar.gz.

File metadata

  • Download URL: marqo-0.1.16.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for marqo-0.1.16.tar.gz
Algorithm Hash digest
SHA256 903211044715945f114f464d286bb8f4c10e2a1f65e979606ff7dba6624d0f79
MD5 cdb20e3349362e761bdbc6ce221c9dac
BLAKE2b-256 fabe1780df42a2302a30cf505bc42c89f0cbe6100ebe985bf24b9b99c0412c2f

See more details on using hashes here.

File details

Details for the file marqo-0.1.16-py3-none-any.whl.

File metadata

  • Download URL: marqo-0.1.16-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for marqo-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 5a39dce124ad287d08c88c3d506fe4309322919645027e74e3aab66e7cce05fd
MD5 fbbe72e081571df3c7f642f6aa866b36
BLAKE2b-256 ce96c9c9554e1ac03dac416a335328ab07967d1d0118e2537b9941d1069935f6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page