Skip to main content
Weaviate Docs (migrated from docs.weaviate.io) Docs

Search documentation

Type to search this documentation.

On this pageOverview

Contextionary Vectorizer

The text2vec-contextionary module enables Weaviate to obtain vectors locally using a lightweight model.

Key notes:

  • This module is not available on Weaviate Cloud (WCD).
  • Enabling this module will enable the nearText search operator.
  • This module is based on FastText and uses a weighted mean of word embeddings (WMOWE) to produce the vector.
  • Available for multiple languages

To use text2vec-contextionary, you must enable it in your Docker Compose file (e.g. docker-compose.yml).

Weaviate:

  • ENABLE_MODULES (Required): The modules to enable. Include text2vec-contextionary to enable the module.
  • DEFAULT_VECTORIZER_MODULE (Optional): The default vectorizer module. You can set this to text2vec-contextionary to make it the default for all classes.

Contextionary:

  • EXTENSIONS_STORAGE_MODE: Location of storage for extensions to the Contextionary
  • EXTENSIONS_STORAGE_ORIGIN: The host of the custom extension storage
  • NEIGHBOR_OCCURRENCE_IGNORE_PERCENTILE: this can be used to hide very rare words. If you set it to '5', this means the 5th percentile of words by occurrence are removed in the nearestNeighbor search (for example used in the GraphQL _additional { nearestNeighbors } feature).
  • ENABLE_COMPOUND_SPLITTING: see here.

This configuration enables text2vec-contextionary, sets it as the default vectorizer, and sets the parameters for the Contextionary Docker container.

YAML
---services:  weaviate:    command:    - --host    - 0.0.0.0    - --port    - '8080'    - --scheme    - http    image: cr.weaviate.io/semitechnologies/weaviate:1.38.2    ports:    - 8080:8080    - 50051:50051    restart: on-failure:0    environment:      CONTEXTIONARY_URL: contextionary:9999      QUERY_DEFAULTS_LIMIT: 25      AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'true'      PERSISTENCE_DATA_PATH: '/var/lib/weaviate'      ENABLE_MODULES: 'text2vec-contextionary'      CLUSTER_HOSTNAME: 'node1'  contextionary:    environment:      OCCURRENCE_WEIGHT_LINEAR_FACTOR: 0.75      EXTENSIONS_STORAGE_MODE: weaviate      EXTENSIONS_STORAGE_ORIGIN: http://weaviate:8080      NEIGHBOR_OCCURRENCE_IGNORE_PERCENTILE: 5      ENABLE_COMPOUND_SPLITTING: 'false'    image: cr.weaviate.io/semitechnologies/contextionary:en0.16.0-v1.2.1    ports:    - 9999:9999...

You can configure how the module will behave in each class through the collection configuration.

You can set vectorizer behavior using the moduleConfig section under each class and property:

  • vectorizer - what module to use to vectorize the data.
  • vectorizeClassName – whether to vectorize the class name. Default: true.
  • skip – whether to skip vectorizing the property altogether. Default: false
  • vectorizePropertyName – whether to vectorize the property name. Default: false
JSON
{  "classes": [    {      "class": "Document",      "description": "A class called document",      "vectorizer": "text2vec-contextionary",      "moduleConfig": {        "text2vec-contextionary": {          "vectorizeClassName": false        }      },      "properties": [        {          "name": "content",          "dataType": [            "text"          ],          "description": "Content that will be vectorized",          "moduleConfig": {            "text2vec-contextionary": {              "skip": false,              "vectorizePropertyName": false            }          }        }      ],    }  ]}

If you are using this module and are vectorizing the class or property name, the name(s) must be a part of the text2vec-contextionary.

To use multiple words as a class or property definition, concatenate them as:

  • camel case (e.g. bornIn) for class or property names, or
  • snake case (e.g. born_in) for property names.

For example, the following are acceptable:

YAML
Publication
  name
  hasArticles
Article
  title
  summary
  wordCount
  url
  hasAuthors
  inPublication        # CamelCase (all versions)
  publication_date     # snake_case (from v1.7.2 on)
Author
  name
  wroteArticles
  writesFor

This is an example of a nearText query with text2vec-contextionary.

Python
# START-ANY
import weaviate
import weaviate.classes as wvc
from weaviate.classes.query import Move
import os

client = weaviate.connect_to_local()

# END-ANY

# Actual client instantiation
client.close()

from weaviate.classes.init import Auth

# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_api_key = os.environ["WEAVIATE_API_KEY"]
openai_api_key = os.environ["OPENAI_API_KEY"]

client = weaviate.connect_to_weaviate_cloud(
    cluster_url=weaviate_url,
    auth_credentials=Auth.api_key(weaviate_api_key),
    headers={
        "X-OpenAI-Api-Key": openai_api_key,
    }
)

# START-ANY
try:
# END-ANY

# START-ANY
    publications = client.collections.use("Publication")

    response = publications.query.near_text(
        query="fashion",
        distance=0.6,
        move_to=Move(force=0.85, concepts="haute couture"),
        move_away=Move(force=0.45, concepts="finance"),
        return_metadata=wvc.query.MetadataQuery(distance=True),
        limit=2
    )

    for o in response.objects:
        print(o.properties)
        print(o.metadata)
# END-ANY

# START-ANY

finally:
    client.close()
# END-ANY
Go
package main

import (
  "context"
  "fmt"

  "github.com/weaviate/weaviate-go-client/v5/weaviate"
  "github.com/weaviate/weaviate-go-client/v5/weaviate/graphql"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client, err := weaviate.NewClient(cfg)
  if err != nil {
    panic(err)
  }

  className := "Publication"

  name := graphql.Field{Name: "name"}
  _additional := graphql.Field{
    Name: "_additional", Fields: []graphql.Field{
      {Name: "certainty"}, // only supported if distance==cosine
      {Name: "distance"},  // always supported
    },
  }

  concepts := []string{"fashion"}
  distance := float32(0.6)
  moveAwayFrom := &graphql.MoveParameters{
    Concepts: []string{"finance"},
    Force:    0.45,
  }
  moveTo := &graphql.MoveParameters{
    Concepts: []string{"haute couture"},
    Force:    0.85,
  }
  nearText := client.GraphQL().NearTextArgBuilder().
    WithConcepts(concepts).
    WithDistance(distance). // use WithCertainty(certainty) prior to v1.14
    WithMoveTo(moveTo).
    WithMoveAwayFrom(moveAwayFrom)

  ctx := context.Background()

  result, err := client.GraphQL().Get().
    WithClassName(className).
    WithFields(name, _additional).
    WithNearText(nearText).
    Do(ctx)

  if err != nil {
    panic(err)
  }
  fmt.Printf("%v", result)
}
Curl
# Note: Under nearText, use `certainty` instead of distance prior to v1.14
# Under _additional, `certainty` is only supported if distance==cosine, but `distance` is always supported
echo '{
  "query": "{
    Get {
      Publication(
        nearText: {
          concepts: [\"fashion\"],
          distance: 0.6,
          moveAwayFrom: {
            concepts: [\"finance\"],
            force: 0.45
          },
          moveTo: {
            concepts: [\"haute couture\"],
            force: 0.85
          }
        }
      ) {
        name
        _additional {
          certainty
          distance
        }
      }
    }
  }"
}' | curl \
    -X POST \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer learn-weaviate' \
    -H "X-OpenAI-Api-Key: $OPENAI_API_KEY" \
    -d @- \
    https://edu-demo.weaviate.network/v1/graphql
GraphQL
{
  Get{
    Publication(
      nearText: {
        concepts: ["fashion"],
        distance: 0.6 # prior to v1.14 use "certainty" instead of "distance"
        moveAwayFrom: {
          concepts: ["finance"],
          force: 0.45
        },
        moveTo: {
          concepts: ["haute couture"],
          force: 0.85
        }
      }
    ){
      name
      _additional {
        certainty # only supported if distance==cosine.
        distance  # always supported
      }
    }
  }
}

To find concepts or words or to check if a concept is part of the Contextionary, use the v1/modules/text2vec-contextionary/concepts/<concept> endpoint.

JavaScript
GET /v1/modules/text2vec-contextionary/concepts/<concept>

The only parameter concept is a string that should be camelCased in case of compound words or a list of words.

The result contains the following fields:

  • "individualWords": a list of the results of individual words or concepts in the query, which contains:
    • "word": a string of requested concept or single word from the concept.
    • "present": a boolean value which is true if the word exists in the Contextionary.
    • "info": an object with the following fields:
      • ""nearestNeighbors": a list with the nearest neighbors, containing "word" and "distance" (between the two words in the high dimensional space). Note that "word" can also be a data object.
      • "vector": the raw 300-long vector value.
    • "concatenatedWord": an object of the concatenated concept.
      • "concatenatedWord": the concatenated word if the concept given is a camelCased word.
        • "singleWords": a list of the single words in the concatenated concept.
        • "concatenatedVector": a list of vector values of the concatenated concept.
        • "concatenatedNearestNeighbors": a list with the nearest neighbors, containing "word" and "distance" (between the two words in the high dimensional space). Note that "word" can also be a data object.
Bash
curl http://localhost:8080/v1/modules/text2vec-contextionary/concepts/magazine

or (note the camelCased compound concept)

Python
import weaviate

client = weaviate.Client("http://localhost:8080")

concept_info = client.contextionary.get_concept_vector("fashionMagazine")
print(concept_info)
Go
package main

import (
  "context"
  "fmt"

  "github.com/weaviate/weaviate-go-client/v5/weaviate"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client, err := weaviate.NewClient(cfg)
  if err != nil {
    panic(err)
  }

  concept, err := client.C11y().
    ConceptsGetter().
    WithConcept("fashionMagazine").
    Do(context.Background())

  if err != nil {
    panic(err)
  }

  fmt.Printf("%v", concept)
}
Curl
curl http://localhost:8080/v1/modules/text2vec-contextionary/concepts/fashionMagazine

with a result similar to:

JSON
{
  "individualWords": [
    {
      "inC11y": true,
      "info": {
        "nearestNeighbors": [
          {
            "word": "magazine"
          },
          {
            "distance": 6.186641,
            "word": "editorial"
          },
          {
            "distance": 6.372504,
            "word": "featured"
          },
          {
            "distance": 6.5695524,
            "word": "editor"
          },
          {
            "distance": 7.0328364,
            "word": "titled"
          },
          ...
        ],
        "vector": [
          0.136228,
          0.706469,
          -0.073645,
          -0.099225,
          0.830348,
          ...
        ]
      },
      "word": "magazine"
    }
  ]
}

text2vec-contextionary (Contextionary) is Weaviate's own language vectorizer that is trained using fastText on Wiki and CommonCrawl data.

The text2vec-contextionary model outputs a 300-dimensional vector. This vector is computed by using a Weighted Mean of Word Embeddings (WMOWE) technique.

The vector is calculated based on the centroid of the words weighted by the occurrences of the individual words in the original training text-corpus (e.g., the word "has" is seen as less important than the word "apples").

data to vector with contextionary

Contextionary models are available for the following languages:

  • Trained with on CommonCrawl and Wiki, using GloVe
    • English
    • Dutch
    • German
    • Czech
    • Italian
  • Trained on Wiki
    • English
    • Dutch

Custom words or abbreviations (i.e., "concepts") can be added to text2vec-contextionary through the v1/modules/text2vec-contextionary/extensions/ endpoint.

Using this endpoint will enrich the Contextionary with your own words, abbreviations or concepts in context by transfer learning. Using the v1/modules/text2vec-contextionary/extensions/ endpoint adds or updates the concepts in real-time.

Note that you need to introduce the new concepts in to Weaviate before adding the data, as this will note cause Weaviate to automatically update the vectors.

A body (in JSON or YAML) with the extension word or abbreviation you want to add to the Contextionary with the following fields includes a:

  • "concept": a string with the word, compound word or abbreviation
  • "definition": a clear description of the concept, which will be used to create the context of the concept and place it in the high dimensional Contextionary space.
  • "weight": a float with the relative weight of the concept (default concepts in the Contextionary have a weight of 1.0)

The same fields as the input parameters will be in the response body if the extension was successful.

Let's add the concept "weaviate" to the Contextionary.

Python
import weaviate

client = weaviate.Client("http://localhost:8080")

client.contextionary.extend("weaviate", "Open source cloud native real time vector database", 1.0)
Go
package main

import (
  "context"

  "github.com/weaviate/weaviate-go-client/v5/weaviate"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client, err := weaviate.NewClient(cfg)
  if err != nil {
    panic(err)
  }

  err := client.C11y().ExtensionCreator().
    WithConcept("weaviate").
    WithDefinition("Open source cloud native real time vector database").
    WithWeight(1.0).
    Do(context.Background())

  if err != nil {
    panic(err)
  }
}
Curl
curl \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
    "concept": "weaviate",
    "definition": "Open source cloud native real time vector database",
    "weight": 1
  }' \
  http://localhost:8080/v1/modules/text2vec-contextionary/extensions

You can always check if the new concept exists in the Contextionary:

Bash
curl http://localhost:8080/v1/modules/text2vec-contextionary/concepts/weaviate

Note that it is not (yet) possible to extend the Contextionary with concatenated words or concepts consisting of more than one word.

You can also overwrite current concepts with this endpoint. Let's say you are using the abbreviation API for Academic Performance Index instead of Application Programming Interface, and you want to reposition this concept in the Contextionary:

Bash
curl \
  -X POST \
  -H 'Content-Type: application/json' \
  -d '{
    "concept": "api",
    "definition": "Academic Performance Index a measurement of academic performance and progress of individual schools in California",
    "weight": 1
  }' \
  http://localhost:8080/v1/modules/text2vec-contextionary/extensions

The meaning of the concept API has now changed in your Weaviate setting.

Note that stopwords are automatically removed from camelCased and CamelCased names.

Stopwords can be useful, so we don't want to encourage you to leave them out completely. Instead Weaviate will remove them during vectorization.

In most cases you won't even notice that this happens in the background, however, there are a few edge cases that might cause a validation error:

  • If your camelCased class or property name consists only of stopwords, validation will fail. Example: TheInA is not a valid class name, however, TheCarInAField is (and would internally be represented as CarField).

  • If your keyword list contains stop words, they will be removed. However, if every single keyword is a stop word, validation will fail.

How does Weaviate decide whether a word is a stop word or not?

Section titled “How does Weaviate decide whether a word is a stop word or not?”

The list of stopwords is derived from the Contextionary version used and is published alongside the Contextionary files.

Sometimes Weaviate's Contextionary does not understand words which are compounded out of words it would otherwise understand. This impact is far greater in languages that allow for arbitrary compounding (such as Dutch or German) than in languages where compounding is not very common (such as English).

Imagine you import an object of class Post with content This is a thunderstormcloud. The arbitrarily compounded word thunderstormcloud is not present in the Contextionary. So your object's position will be made up of the only words it recognizes: "post", "this" ("is" and "a" are removed as stopwords).

If you check how this content was vectorized using the _interpretation feature, you will see something like the following:

JSON
"_interpretation": {
  "source": [
    {
      "concept": "post",
      "occurrence": 62064610,
      "weight": 0.3623903691768646
    },
    {
      "concept": "this",
      "occurrence": 932425699,
      "weight": 0.10000000149011612
    }
  ]
}

To overcome this limitation the optional Compound Splitting Feature can be enabled in the Contextionary. It will understand the arbitrary compounded word and interpret your object as follows:

JSON
"_interpretation": {
"source": [
  {
    "concept": "post",
    "occurrence": 62064610,
    "weight": 0.3623903691768646
  },
  {
    "concept": "this",
    "occurrence": 932425699,
    "weight": 0.10000000149011612
  },
  {
    "concept": "thunderstormcloud (thunderstorm, cloud)",
    "occurrence": 5756775,
    "weight": 0.5926488041877747
  }
]
}

Note that the newly found word (made up of the parts thunderstorm and cloud has the highest weight in the vectorization. So this meaning, which would have been lost without Compound Splitting, can now be recognized.

You can enable Compound Splitting in the Docker Compose file of the text2vec-contextionary. See how this is done here.

Compound Splitting runs an any word that is otherwise not recognized. Depending on your dataset, this can lead to a significantly longer import time (up to 100% longer). Therefore, you should carefully evaluate whether the higher precision in recognition or the faster import times are more important to your use case. As the benefit is larger in some languages (e.g. Dutch, German) than in others (e.g. English) this feature is turned off by default.

So called "noise words" are concatenated words of random words with no easily recognizable meaning. These words are present in the Contextionary training space, but are extremely rare and therefore distributed seemingly randomly. As a consequence, an "ordinary" result of querying features relying on nearest neighbors (additional properties nearestNeighbors or semanticPath) might contain such noise words as immediate neighbors.

To combat this noise, a neighbor filtering feature was introduced in the contextionary, which ignores words of the configured bottom percentile - ranked by occurrence in the respective training set. By default this value is set to the bottom 5th percentile. This setting can be overridden. To set another value, e.g. to ignore the bottom 10th percentile, provide the environment variable NEIGHBOR_OCCURRENCE_IGNORE_PERCENTILE=10 to the text2vec-contextionary container, in the Docker Compose file.

The text2vec-contextionary module is based on the fastText library, which is released under the MIT license. See the license file for more information.

It is your responsibility to evaluate whether the terms of its license(s), if any, are appropriate for your intended use.

Have a question or feedback? Here's how to reach us.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu