How to see the Embedding of the documents with Chroma (or any other DB) saved in Lang Chain?
Asked Answered
M

2

8

I can see everything but the Embedding of the documents when I used Chroma with Langchain and OpenAI embeddings. It always show me None for that

Here is the code:

for db_collection_name in tqdm(["class1-sub2-chap3", "class2-sub3-chap4"]):
    documents = []
    doc_ids = []

    for doc_index in range(3):
        cl, sub, chap = db_collection_name.split("-")
        content = f"This is {db_collection_name}-doc{doc_index}"
        doc = Document(page_content=content, metadata={"chunk_num": doc_index, "chapter":chap, "class":cl, "subject":sub})
        documents.append(doc)
        doc_ids.append(str(doc_index))


    # # Initialize a Chroma instance with the original document
    db = Chroma.from_documents(
         collection_name=db_collection_name,
         documents=documents, ids=doc_ids,
         embedding=embeddings, 
         persist_directory="./data")
    
     db.persist()

when I do db.get(), I see everything as expected except embedding is None.

{'ids': ['0', '1', '2'],
 'embeddings': None,
 'documents': ['This is class1-sub2-chap3-doc0',
  'This is class1-sub2-chap3-doc1',
  'This is class1-sub2-chap3-doc2'],
 'metadatas': [{'chunk_num': 0,
   'chapter': 'chap3',
   'class': 'class1',
   'subject': 'sub2'},
  {'chunk_num': 1, 'chapter': 'chap3', 'class': 'class1', 'subject': 'sub2'},
  {'chunk_num': 2, 'chapter': 'chap3', 'class': 'class1', 'subject': 'sub2'}]}

My embeddings is also working fine as it returns:

len(embeddings.embed_documents(["EMBED THIS"])[0])
>> 1536

also, in my ./data directory I have Embedding file as chroma-embeddings.parquet


I tried the example with example given in document but it shows None too

# Import Document class
from langchain.docstore.document import Document

# Initial document content and id
initial_content = "This is an initial document content"
document_id = "doc1"

# Create an instance of Document with initial content and metadata
original_doc = Document(page_content=initial_content, metadata={"page": "0"})

# Initialize a Chroma instance with the original document
new_db = Chroma.from_documents(
    collection_name="test_collection",
    documents=[original_doc],
    embedding=OpenAIEmbeddings(),  # using the same embeddings as before
    ids=[document_id],
)

Here also new_db.get() gives me None

Monomorphic answered 1/6, 2023 at 7:7 Comment(1)
Please see my response in this answer: #76185040Overblouse
T
20

You just need to specify that you want the embeddings as well when using .get

# Get all embeddings
db._collection.get(include=['embeddings'])

# Get embeddings by document_id
db._collection.get(ids=['doc0', ..., 'docN'], include=['embeddings'])
Triceratops answered 1/6, 2023 at 23:6 Comment(1)
It also works with query ie results = collection.query(include=['embeddings'])Chubby
S
0

Here is the solution

loader = DirectoryLoader("document", glob="**/*.*")
files = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size = 1500,
    chunk_overlap = 150
)
docs = text_splitter.split_documents(files)

documents = [Document(page_content=doc.page_content, metadata={"topic":f"John's story{i}"}) for i, doc in enumerate(docs)]
db = Chroma.from_documents(documents=documents, embedding=embedding, persist_directory="db")

Please try to run this code. I have checked that the metadata has added. Here is the result image. enter image description here

Simper answered 14/11, 2023 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.