text2vec

repository·master·Indexed 26 days ago

https://github.com/shibing624/text2vec

A Python library for converting words, sentences, and paragraphs into vector embeddings. It supports multiple models including Word2Vec, SBERT, CoSENT, and BGE for semantic similarity, text matching, and semantic search. The library provides pre-trained models for general Chinese, sentence-to-sentence, sentence-to-paragraph, and multilingual matching, along with tools for training supervised and unsupervised models and deploying them via Jina or FastAPI.

Tokens
9.9K
Snippets
25
Records
53
Agent score
90%

What's inside text2vec

  1. Overview of text2vec features

    master

    text2vec is a library designed to convert text (including words, sentences, and paragraphs) into vector matrices (embeddings). It provides implementations for several text representation and similarity calculation models, including:

    • Word2Vec: Implements word vector retrieval using large-scale Chinese word vector data (e.g., Tencent AI Lab's lightweight version). It supports sentence representation by averaging word vectors.
    • SBERT (Sentence-BERT): A model that balances performance and efficiency by using supervised training on BERT and softmax classification functions. It uses cosine similarity on sentence vectors for text matching.
    • CoSENT (Cosine Sentence): A model utilizing a ranking loss function that makes the training process closer to prediction, offering better convergence and performance than Sentence-BERT.
    • BGE (BAAI General Embedding): A model pre-trained using the RetroMAE method and fine-tuned with contrastive learning.

    Detailed information on text vectorization methods can be found in the project wiki.

  2. Overview of text2vec models and features

    master

    text2vec is a library for converting text (words, sentences, paragraphs) into vector matrices. It implements several text representation and similarity calculation models:

    • Word2Vec: Provides word vector retrieval and sentence vector representation (via word vector average). It can use large-scale Chinese word vector data (e.g., Tencent AI Lab open source version).
    • SBERT (Sentence-BERT): A sentence vector representation model that balances performance and efficiency. It is implemented using PyTorch and can be used for direct sentence vector cosine similarity prediction.
    • CoSENT (Cosine Sentence): A model using a sorted loss function that typically offers better convergence speed and performance than SBERT for semantic matching tasks.

    Pre-trained models can often be called via the transformers library using their specific model names (e.g., --model_name hfl/chinese-macbert-base).

  3. Understand the CoSENT model approach

    master

    The CoSENT (Cosine Sentence) model is designed to address the 'collapse' phenomenon in BERT-based sentence representations, where sentence embeddings are often clustered in a small region of the vector space, leading to high similarity scores even for unrelated sentences.

    Unlike Sentence-BERT, which uses a classification-based training approach (concatenating $u$, $v$, and $|u-v|$), CoSENT optimizes the cosine similarity directly. It uses a ranking loss function to ensure that for any positive pair $(x^i, x^{i+})$ and negative pair $(x^i, x^{i-})$, the relationship $\cos(h^i, h^{i+}) > \cos(h^i, h^{i-})$ is maintained. This makes the training process more consistent with the actual prediction task (calculating cosine similarity).

  4. Select a Chinese text2vec model based on your task

    master

    The text2vec project provides several pre-trained CoSENT models optimized for different Chinese semantic matching tasks. Choose the model that best fits your use case:

    • General Semantic Matching: Use shibing624/text2vec-base-chinese for general-purpose Chinese semantic matching tasks. It is based on hfl/chinese-macbert-base.
    • Sentence-to-Sentence (s2s) Matching: Use shibing624/text2vec-base-chinese-sentence for s2s semantic matching. It is based on nghuyong/ernie-3.0-base-zh and trained on manually selected Chinese STS datasets.
    • Sentence-to-Paraphrase (s2p) Matching: Use shibing624/text2vec-base-chinese-paraphrase for s2p tasks or when you need enhanced representation for long texts. It is based on nghuyong/ernie-3.0-base-zh and includes s2p data to achieve SOTA performance on NLI datasets.
  5. Reproduce model evaluation results

    master

    To reproduce the Spearman correlation evaluation results reported in the documentation:

    1. Download the Chinese matching datasets to the examples/data directory.
    2. Run the evaluation script tests/model_spearman.py.
  6. Deploy models using Jina (gRPC/HTTP/WebSocket)

    master

    Jina provides a high-performance C/S mode deployment supporting Docker, Kubernetes, and multi-GPU processing.

    1. Install Jina:

    pip install jina

    2. Start the server: Use the following pattern in examples/jina_server_demo.py to start a service using the Text2vecEncoder from JinaHub:

    from jina import Flow
    
    port = 50001
    f = Flow(port=port).add(
        uses='jinahub://Text2vecEncoder',
        uses_with={'model_name': 'shibing624/text2vec-base-chinese'}
    )
    
    with f:
        f.block()

    3. Call the service:

    from jina import Client
    from docarray import Document, DocumentArray
    
    port = 50001
    c = Client(port=port)
    
    data = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
    r = c.post('/', inputs=DocumentArray([Document(text='如何更换花呗绑定银行卡'), Document(text='花呗更改绑定银行卡')]))
    print(r.embeddings)
    from jina import Flow
    
    port = 50001
    f = Flow(port=port).add(
        uses='jinahub://Text2vecEncoder',
        uses_with={'model_name': 'shibing624/text2vec-base-chinese'}
    )
    
    with f:
        f.block()
  7. Install text2vec

    master

    You can install text2vec using pip. It requires torch to be installed first.

    Option 1: Direct installation via PyPI

    pip install torch
    pip install -U text2vec

    Option 2: Installation from source

    pip install torch
    git clone https://github.com/shibing624/text2vec.git
    cd text2vec
    pip install --no-deps .
    pip install torch
    pip install -U text2vec
  8. Train a text2vec model using provided examples

    master

    You can train your own semantic matching models using the provided training scripts. The script to use depends on the training method and data format:

    • SBERT training: Use examples/training_sup_text_matching_model.py.
    • CoSENT training (JSONL data): Use examples/training_sup_text_matching_model_jsonl_data.py for models like text2vec-base-chinese-sentence or text2vec-base-chinese-paraphrase.
  9. Deploy text2vec using Jina (gRPC/HTTP/WebSocket)

    master

    Build high-performance services using Jina. This method supports C/S mode, docker cloud native deployment, gRPC/HTTP/WebSocket, simultaneous prediction of multiple models, and multi-GPU processing. The model executor is available on JinaHub.

    pip install jina

    Start the service

    from jina import Flow
    
    port = 50001
    f = Flow(port=port).add(
        uses='jinahub://Text2vecEncoder',
        uses_with={'model_name': 'shibing624/text2vec-base-chinese'}
    )
    
    with f:
        f.block()

    Call the service

    from jina import Client
    from docarray import Document, DocumentArray
    
    port = 50001
    c = Client(port=port)
    
    data = ['如何更换花呗绑定银行卡', '花呗更改绑定银行卡']
    r = c.post('/', inputs=DocumentArray([Document(text='如何更换花呗绑定银行卡'), Document(text='花呗更改绑定银行卡')]))
    print(r.embeddings)
  10. Deploy models using FastAPI (HTTP)

    master

    For a native HTTP service, use FastAPI.

    1. Install dependencies:

    pip install fastapi uvicorn

    2. Start the server:

    cd examples
    python fastapi_server_demo.py

    3. Call the service via curl:

    curl -X 'GET' \
      'http://0.0.0.0:8001/emb?q=hello' \
      -H 'accept: application/json'
    cd examples
    python fastapi_server_demo.py