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
nearTextsearch operator. - This module is based on FastText and uses a weighted mean of word embeddings (WMOWE) to produce the vector.
- Available for multiple languages
Weaviate instance configuration
Section titled “Weaviate instance configuration”Docker Compose file
Section titled “Docker Compose file”To use text2vec-contextionary, you must enable it in your Docker Compose file (e.g. docker-compose.yml).
Parameters
Section titled “Parameters”Weaviate:
ENABLE_MODULES(Required): The modules to enable. Includetext2vec-contextionaryto enable the module.DEFAULT_VECTORIZER_MODULE(Optional): The default vectorizer module. You can set this totext2vec-contextionaryto make it the default for all classes.
Contextionary:
EXTENSIONS_STORAGE_MODE: Location of storage for extensions to the ContextionaryEXTENSIONS_STORAGE_ORIGIN: The host of the custom extension storageNEIGHBOR_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.
Example
Section titled “Example”This configuration enables text2vec-contextionary, sets it as the default vectorizer, and sets the parameters for the Contextionary Docker container.
---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...Collection configuration
Section titled “Collection configuration”You can configure how the module will behave in each class through the collection configuration.
Vectorization settings
Section titled “Vectorization settings”You can set vectorizer behavior using the moduleConfig section under each class and property:
Class-level
Section titled “Class-level”vectorizer- what module to use to vectorize the data.vectorizeClassName– whether to vectorize the class name. Default:true.
Property-level
Section titled “Property-level”skip– whether to skip vectorizing the property altogether. Default:falsevectorizePropertyName– whether to vectorize the property name. Default:false
Example
Section titled “Example”{ "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 } } } ], } ]}Class/property names
Section titled “Class/property names”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:
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
writesForUsage example
Section titled “Usage example”This is an example of a nearText query with text2vec-contextionary.
# 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-ANYpackage 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)
}# 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{
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
}
}
}
}Additional information
Section titled “Additional information”Find concepts
Section titled “Find concepts”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.
GET /v1/modules/text2vec-contextionary/concepts/<concept>Parameters
Section titled “Parameters”The only parameter concept is a string that should be camelCased in case of compound words or a list of words.
Response
Section titled “Response”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 istrueif 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.
Example
Section titled “Example”curl http://localhost:8080/v1/modules/text2vec-contextionary/concepts/magazineor (note the camelCased compound concept)
import weaviate
client = weaviate.Client("http://localhost:8080")
concept_info = client.contextionary.get_concept_vector("fashionMagazine")
print(concept_info)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 http://localhost:8080/v1/modules/text2vec-contextionary/concepts/fashionMagazinewith a result similar to:
{
"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"
}
]
}Model details
Section titled “Model details”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
Available languages
Section titled “Available languages”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
Extending the Contextionary
Section titled “Extending the Contextionary”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.
Parameters
Section titled “Parameters”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)
Response
Section titled “Response”The same fields as the input parameters will be in the response body if the extension was successful.
Example
Section titled “Example”Let's add the concept "weaviate" to the Contextionary.
import weaviate
client = weaviate.Client("http://localhost:8080")
client.contextionary.extend("weaviate", "Open source cloud native real time vector database", 1.0)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 \
-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/extensionsYou can always check if the new concept exists in the Contextionary:
curl http://localhost:8080/v1/modules/text2vec-contextionary/concepts/weaviateNote 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:
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/extensionsThe meaning of the concept API has now changed in your Weaviate setting.
Stopwords
Section titled “Stopwords”Note that stopwords are automatically removed from camelCased and CamelCased names.
Vectorization behavior
Section titled “Vectorization behavior”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:
TheInAis not a valid class name, however,TheCarInAFieldis (and would internally be represented asCarField). -
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.
Compound splitting
Section titled “Compound splitting”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).
Effect
Section titled “Effect”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:
"_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:
"_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.
How to enable
Section titled “How to enable”You can enable Compound Splitting in the Docker Compose file of the text2vec-contextionary. See how this is done here.
Trade-Off Import speed vs Word recognition
Section titled “Trade-Off Import speed vs Word recognition”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.
Noise filtering
Section titled “Noise filtering”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.
Model license(s)
Section titled “Model license(s)”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.
Questions and feedback
Section titled “Questions and feedback”Have a question or feedback? Here's how to reach us.