Skip to main content

No project description provided

Project description

Mars Similarity Tools

A small tools library for getting vector similarity measurement working in no time.

Example

Here's a basic similarity search and vectorization example. We instantiate a VectorSimilarityService which needs an Augmentor of some kind. The Augmentor should be responsible for taking objects inherited by SimilarityObject class and return vectorized grouped objects (as a VectorGroup object). Before a SimilarityObject can be transformed into a VectorGroup it will pass the GroupParser first. That one will rearrange the properties of the objects into groups, which is given in the parser. We need to do this since multiple properties of an object should in the end be represented by one vector together.

# First things first. Create a similarity model we want to measure similarity between
# And yes! You could create a seperate Color class that holds name and description for Color.
@dataclass(frozen=True) # Important!
class Bicycle(SimilarityObject):

    id: str
    color_name: str
    color_description: str
    wheel_size: int
    model: str

# Then create the parser, vectorizer, augmentor and service.
service = VectorSimilarityService(
    augmentor=ItemVectorizer(
        # NOTE! The default Vectorizer only returns random vectors.
        # So don't trust the similarity result here ;)
        vectorizer=Vectorizer(),
        parser=GroupParser(
            name=Bicycle.__class__.__name__, 
            children=[
                GroupParser(
                    name="color",
                    children=[
                        PropertyParser(
                            name="color name",
                            dtype=str,
                            path=["color_name"]
                        ),
                        PropertyParser(
                            name="color description",
                            dtype=str,
                            path=["color_description"]
                        ),
                    ],
                ),
                PropertyParser(
                    name="wheel_size",
                    dtype=int,
                    path=["wheel_size"]
                ),
                PropertyParser(
                    name="model",
                    dtype=str,
                    path=["model"]
                ),
            ]
        ),
    )
)

# Now we can create a namespace and add objects to that namespace.
namespace = "bicycles"
objects = [
    Bicycle(
        id="1",
        color_name="red",
        color_description="A red bicycle",
        wheel_size=26,
        model="mountain"
    ),
    Bicycle(
        id="2",
        color_name="blue",
        color_description="A blue bicycle",
        wheel_size=26,
        model="mountain"
    ),
    Bicycle(
        id="3",
        color_name="green",
        color_description="A green bicycle",
        wheel_size=28,
        model="racer"
    ),
]

# Create the namespace and add the objects to that namespace.
# It is because when we perform a similarity search, we need to know the objects to compare and
# so we bind the objects to a namespace.
service.create_namespace(namespace, objects)

# Now we can perform a similarity search.
similarity_result = service.similarity_search(
    namespace, 
    Bicycle(
        id="4",
        color_name="yellow",
        color_description="A yellow bicycle",
        wheel_size=28,
        model="racer"
    ), 
    top=2
)

# We could also do a similarity search including some bias to the search.
# For instance, we might want to find a similar bicycle but we want to bias the search
# towards the color.
biased_similarity_result = service.similarity_search(
    namespace, 
    Bicycle(
        id="4",
        color_name="yellow",
        color_description="A yellow bicycle",
        wheel_size=28,
        model="racer"
    ), 
    top=2,
    # Note here that we just say "color", so we select the common vector
    # for colors which from the group is both name and description for the color
    bias={"color": 1.2, "wheel_size": 0.2, "model": 0.2}
)

Project details


Download files

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

Source Distribution

mars_similarity_tools-0.2.102.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mars_similarity_tools-0.2.102-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file mars_similarity_tools-0.2.102.tar.gz.

File metadata

  • Download URL: mars_similarity_tools-0.2.102.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.18 Darwin/23.1.0

File hashes

Hashes for mars_similarity_tools-0.2.102.tar.gz
Algorithm Hash digest
SHA256 a250351cd33f8e94bf82b1110871425eaab45d18aa70765772be1b7bb5bc0b0b
MD5 3672d5e4259b154d1f7f59cc05e9b295
BLAKE2b-256 86d4a5d63775858bf1cf8ecb8daf9f3b8d78e57fb822b75e7292c8625b1402cf

See more details on using hashes here.

File details

Details for the file mars_similarity_tools-0.2.102-py3-none-any.whl.

File metadata

File hashes

Hashes for mars_similarity_tools-0.2.102-py3-none-any.whl
Algorithm Hash digest
SHA256 541e702fb570da2361f58cd3409893a9c2d1c9b37d422d0fc7b0476edf3035a4
MD5 f16d8364ed611f2d903c7323d99ef079
BLAKE2b-256 05a6980b74ef85020cafc9f9033acab8c8f48e0b269131795e0d5efa7e581308

See more details on using hashes here.

Supported by

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