UNPKG

weaviate-client

Version:
37 lines (36 loc) • 1.89 kB
import { textAnalyzerConfigToWire } from '../collections/config/utils.js'; import { WeaviateInvalidInputError } from '../errors.js'; import { parseResult } from './util.js'; const tokenize = (connection, dbVersionSupport) => { return { text: (text, tokenization, opts) => { if (opts?.stopwords !== undefined && opts?.stopwordPresets !== undefined) { return Promise.reject(new WeaviateInvalidInputError('stopwords and stopwordPresets are mutually exclusive; pass at most one')); } const hasStopwordsField = opts?.stopwords !== undefined || opts?.stopwordPresets !== undefined; return dbVersionSupport .supportsTokenize() .then(({ supports, message }) => (supports ? Promise.resolve() : Promise.reject(new Error(message)))) .then(() => hasStopwordsField ? dbVersionSupport .supportsTokenizeStopwords() .then(({ supports, message }) => supports ? Promise.resolve() : Promise.reject(new Error(message))) : Promise.resolve()) .then(() => connection .postReturn('/tokenize', { text, tokenization, analyzerConfig: textAnalyzerConfigToWire(opts?.analyzerConfig), stopwords: opts?.stopwords, stopwordPresets: opts?.stopwordPresets, }) .then(parseResult)); }, forProperty: (collection, property, text) => dbVersionSupport .supportsTokenize() .then(({ supports, message }) => (supports ? Promise.resolve() : Promise.reject(new Error(message)))) .then(() => connection.postReturn(`/schema/${collection}/properties/${property}/tokenize`, { text })) .then(parseResult), }; }; export default tokenize;