Getting Started — 설치부터 쿼리까지

Getting Started — 설치부터 쿼리까지

Chroma는 설치해서 몇 줄이면 첫 컬렉션을 만들고 검색까지 할 수 있어요. 이 가이드는 이페머럴(ephemeral) 클라이언트를 써서 간단하게 시작해 볼게요.

출처: https://docs.trychroma.com/getting-started

1. Install

pip install chromadb

2. Create a Chroma Client

import chromadb
chroma_client = chromadb.Client()

3. Create a collection

컬렉션은 임베딩·문서·추가 메타데이터를 저장하는 곳이에요. 컬렉션이 벡터와 문서를 인덱싱해서 효율적인 검색·필터링이 가능하게 해요.

collection = chroma_client.create_collection(name="my_collection")

4. Add some text documents

Chroma가 텍스트를 저장하고 임베딩·인덱싱을 자동 처리해요. 임베딩 모델도 커스터마이즈할 수 있어요. 문서에는 고유한 문자열 ID가 필요해요.

collection.add(
    ids=["id1", "id2"],
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges"
    ]
)

5. Query the collection

쿼리 텍스트 목록으로 검색하면 n개의 유사 결과를 반환해요.

results = collection.query(
    query_texts=["This is a query document about hawaii"],  # Chroma will embed this for you
    n_results=2  # how many results to return
)
print(results)

n_results를 지정하지 않으면 기본으로 10개를 반환해요. 여기서는 문서가 2개뿐이라 n_results=2로 설정했어요.

같은 문서를 계속 넣지 않으려면 add 대신 upsert를 써요.

collection.upsert(
    documents=[
        "This is a document about pineapple",
        "This is a document about oranges"
    ],
    ids=["id1", "id2"]
)

여기서 쓴 이페머럴 클라이언트는 Chroma 서버를 인메모리로 띄우기 때문에 프로그램이 끝나면 데이터가 사라져요. 데이터 유지가 필요하면 persistent 클라이언트나 클라이언트-서버 모드로 실행하면 돼요.

더 알아보기