SemaDBSemaFind의 제품으로, AI 애플리케이션 구축을 위한 간편한 벡터 유사도 데이터베이스입니다. 호스팅되는 SemaDB Cloud는 시작하기 쉬운 개발자 경험을 제공합니다.
API의 전체 문서와 예제, 인터랙티브 플레이그라운드는 RapidAPI에서 확인할 수 있습니다. 이 노트북은 SemaDB Cloud vector store의 사용법을 보여줍니다. 이 integration을 사용하려면 pip install -qU langchain-communitylangchain-community를 설치해야 합니다

document embedding 로드하기

로컬에서 실행하기 위해, 문장 임베딩에 일반적으로 사용되는 Sentence Transformers를 사용합니다. LangChain이 제공하는 모든 embedding model을 사용할 수 있습니다.
pip install -qU  sentence_transformers
from langchain_huggingface import HuggingFaceEmbeddings

model_name = "sentence-transformers/all-mpnet-base-v2"
embeddings = HuggingFaceEmbeddings(model_name=model_name)
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter

loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=400, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
print(len(docs))
114

SemaDB에 연결하기

SemaDB Cloud는 인증을 위해 RapidAPI keys를 사용합니다. 무료 RapidAPI 계정을 생성하여 키를 얻을 수 있습니다.
import getpass
import os

if "SEMADB_API_KEY" not in os.environ:
    os.environ["SEMADB_API_KEY"] = getpass.getpass("SemaDB API Key:")
SemaDB API Key: ········
from langchain_community.vectorstores import SemaDB
from langchain_community.vectorstores.utils import DistanceStrategy
SemaDB vector store의 매개변수는 API를 직접 반영합니다:
  • “mycollection”: 이 벡터들을 저장할 collection 이름입니다.
  • 768: 벡터의 차원입니다. 우리의 경우, sentence transformer embedding은 768차원 벡터를 생성합니다.
  • API_KEY: RapidAPI 키입니다.
  • embeddings: document, text, query의 embedding이 어떻게 생성될지를 나타냅니다.
  • DistanceStrategy: 사용되는 거리 메트릭입니다. wrapper는 COSINE이 사용될 경우 자동으로 벡터를 정규화합니다.
db = SemaDB("mycollection", 768, embeddings, DistanceStrategy.COSINE)

# Create collection if running for the first time. If the collection
# already exists this will fail.
db.create_collection()
True
SemaDB vector store wrapper는 나중에 수집하기 위해 document text를 point metadata로 추가합니다. 큰 텍스트 청크를 저장하는 것은 권장되지 않습니다. 대규모 collection을 인덱싱하는 경우, 외부 Id와 같은 document에 대한 참조를 저장하는 것을 권장합니다.
db.add_documents(docs)[:2]
['813c7ef3-9797-466b-8afa-587115592c6c',
 'fc392f7f-082b-4932-bfcc-06800db5e017']
가장 유사한 문장을 검색하기 위해 기본 LangChain similarity search interface를 사용합니다.
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query)
print(docs[0].page_content)
And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
docs = db.similarity_search_with_score(query)
docs[0]
(Document(page_content='And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'source': '../../how_to/state_of_the_union.txt', 'text': 'And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.'}),
 0.42369342)

정리하기

모든 데이터를 제거하기 위해 collection을 삭제할 수 있습니다.
db.delete_collection()
True

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I