-- Enable the extensions (if not already done)
CREATEEXTENSIONIFNOTEXISTSvchord_bm25;CREATEEXTENSIONIFNOTEXISTSpg_tokenizer;-- Update search_path for the first time
ALTERSYSTEMSETsearch_pathTO"$user",public,tokenizer_catalog,bm25_catalog;SELECTpg_reload_conf();-- Create a tokenizer configuration using the LLMLingua2 model
SELECTcreate_tokenizer('llm_tokenizer',$$model="llmlingua2"$$);-- Tokenize some English text
SELECTtokenize('PostgreSQL is a powerful, open source database.','llm_tokenizer');-- Output: {2795,7134,158897,83,10,113138,4,9803,31344,63399,5} -- Example token IDs
-- Tokenize some Spanish text (LLMLingua2 handles multiple languages)
SELECTtokenize('PostgreSQL es una potente base de datos de código abierto.','llm_tokenizer');-- Output: {2795,7134,158897,198,220,105889,3647,8,13084,8,55845,118754,5} -- Example token IDs
-- Integrate with a table
CREATETABLEdocuments(idSERIALPRIMARYKEY,passageTEXT,embeddingbm25vector);INSERTINTOdocuments(passage)VALUES('PostgreSQL is a powerful, open source database.');UPDATEdocumentsSETembedding=tokenize(passage,'llm_tokenizer')WHEREid=1;-- Or process the whole table
CREATETABLEarticles(idSERIALPRIMARYKEY,contentTEXT,embeddingbm25vector);-- Define a custom text analysis pipeline
SELECTcreate_text_analyzer('english_analyzer',$$pre_tokenizer="unicode_segmentation"#Basicwordsplitting[[character_filters]]to_lowercase={}#Lowercaseeverything[[character_filters]]unicode_normalization="nfkd"#NormalizeUnicode[[token_filters]]skip_non_alphanumeric={}#Removepunctuation-onlytokens[[token_filters]]stopwords="nltk_english"#Usebuilt-inEnglishstopwords[[token_filters]]stemmer="english_porter2"#ApplyPorter2stemming$$);-- Create tokenizer, custom model based on 'articles.content', and trigger
SELECTcreate_custom_model_tokenizer_and_trigger(tokenizer_name=>'custom_english_tokenizer',model_name=>'article_model',text_analyzer_name=>'english_analyzer',table_name=>'articles',source_column=>'content',target_column=>'embedding');-- Now, inserts automatically generate tokens
INSERTINTOarticles(content)VALUES('VectorChord-BM25 provides advanced ranking features for PostgreSQL users.');SELECTembeddingFROMarticlesWHEREid=1;-- Output: {1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1}
-- Bm25vector based on the custom model and pipeline
3. 使用 Jieba 处理中文文本
CREATETABLEchinese_docs(idSERIALPRIMARYKEY,passageTEXT,embeddingbm25vector);-- Define a text analyzer using the Jieba pre-tokenizer
SELECTcreate_text_analyzer('jieba_analyzer',$$[pre_tokenizer.jieba]#OptionalJiebaconfigurationscangohere$$);-- Create tokenizer, custom model, and trigger for Chinese text
SELECTcreate_custom_model_tokenizer_and_trigger(tokenizer_name=>'chinese_tokenizer',model_name=>'chinese_model',text_analyzer_name=>'jieba_analyzer',table_name=>'chinese_docs',source_column=>'passage',target_column=>'embedding');-- Insert Chinese text
INSERTINTOchinese_docs(passage)VALUES('红海早过了,船在印度洋面上开驶着。');-- Example sentence
SELECTembeddingFROMchinese_docsWHEREid=1;-- Output: {1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1, 10:1, 11:1, 12:1}
-- Bm25vector based on Jieba segmentation