Files
langchain/docs/extras/integrations/text_embedding/spacy_embedding.ipynb
T
Leonid Ganeline fdba711d28 docs integrations/embeddings consistency (#10302)
Updated `integrations/embeddings`: fixed titles; added links,
descriptions
Updated `integrations/providers`.
2023-09-07 19:53:33 -07:00

3.4 KiB

SpaCy

spaCy is an open-source software library for advanced natural language processing, written in the programming languages Python and Cython.

Installation and Setup

In [ ]:
#!pip install spacy

Import the necessary classes

In [ ]:
from langchain.embeddings.spacy_embeddings import SpacyEmbeddings

Example

Initialize SpacyEmbeddings.This will load the Spacy model into memory.

In [ ]:
embedder = SpacyEmbeddings()

Define some example texts . These could be any documents that you want to analyze - for example, news articles, social media posts, or product reviews.

In [ ]:
texts = [
    "The quick brown fox jumps over the lazy dog.",
    "Pack my box with five dozen liquor jugs.",
    "How vexingly quick daft zebras jump!",
    "Bright vixens jump; dozy fowl quack.",
]

Generate and print embeddings for the texts . The SpacyEmbeddings class generates an embedding for each document, which is a numerical representation of the document's content. These embeddings can be used for various natural language processing tasks, such as document similarity comparison or text classification.

In [ ]:
embeddings = embedder.embed_documents(texts)
for i, embedding in enumerate(embeddings):
    print(f"Embedding for document {i+1}: {embedding}")

Generate and print an embedding for a single piece of text. You can also generate an embedding for a single piece of text, such as a search query. This can be useful for tasks like information retrieval, where you want to find documents that are similar to a given query.

In [ ]:
query = "Quick foxes and lazy dogs."
query_embedding = embedder.embed_query(query)
print(f"Embedding for query: {query_embedding}")