본문 바로가기
프로그램 개발해서 돈벌기/python

chroma 디비에서 http와 로컬 데이터 조회

by ubmuhan 2025. 4. 22.
반응형

이전에 추가한 데이터를 기반으로 설명하겠습니다.

 

collection.add(
    documents=["This is document1", "This is document2"], # we embed for you, or bring your own
    metadatas=[{"source": "notion"}, {"source": "google-docs"}], # filter on arbitrary metadata!
    ids=["doc1", "doc2"], # must be unique for each doc
)

 

http 방식으로 데이터를 쿼리는 다음과 같이 합니다.

 

import chromadb

client = chromadb.HttpClient()
collection = client.get_or_create_collection("sample_collection")
# Add docs to the collection. Can also update and delete. Row-based API coming soon!

results = collection.query(
    query_texts=["This is document1"],
    n_results=2,
    # where={"metadata_field": "is_equal_to_this"}, # optional filter
    # where_document={"$contains":"search_string"}  # optional filter
)

print(results)

 

 

로컬 방식으로 데이터를 쿼리는 다음과 같습니다.

 

import chromadb

client = chromadb.PersistentClient(path="./chroma_db")
collection = client.get_or_create_collection("sample_collection")

results = collection.query(
    query_texts=["This is document1"],
    n_results=2
)

print(results)

 

둘 다 결과는 동일합니다.

 

{'ids': [['doc1', 'doc2']], 'distances': [[0.0, 0.2221482790326065]], 'embeddings': None, 'metadatas': [[{'source': 'notion'}, {'source': 'google-docs'}]], 'documents': [['This is document1', 'This is document2']], 'uris': None, 'data': None, 'included': ['distances', 'documents', 'metadatas']}

 

트리 형태로 보면 다음과 같습니다.

 

 

 

distances 값이 적은 것이 정확도가 높은 겁니다.

 

collection 전체는 다음과 같이 가져옵니다.

 

import chromadb

client = chromadb.PersistentClient(path="./chroma_db")
collection = client.get_or_create_collection("sample_collection")

# 전체 문서 조회
all_docs = collection.get()

print(all_docs)

 

간단하죠? 현재 추가한 데이터는 위 2개가 전부여서 결과는 같습니다.

 

반응형

댓글