-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_knowledge_base.py
More file actions
50 lines (31 loc) · 1.45 KB
/
Copy pathbuild_knowledge_base.py
File metadata and controls
50 lines (31 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import chromadb
from chromadb.utils.embedding_functions.ollama_embedding_function import (
OllamaEmbeddingFunction,
)
# Load the profile document
with open("profile.txt", "r") as f:
text = f.read()
# Split into chunks by paragraph - each blank line becomes a split point
# strip() removes extra whitespace, and the if-check skips empty chunks
chunks = [chunk.strip() for chunk in text.split("\n\n") if chunk.strip()]
print(f"Loaded {len(chunks)} chunks from profile.txt")
# Initialize ChromaDB - PersistentClient saves data to disk so it survives restarts
client = chromadb.PersistentClient(path="./chroma_db")
# Connect to Ollama's embedding model to convert text into vectors
ef = OllamaEmbeddingFunction(
model_name="nomic-embed-text",
url="http://localhost:11434", # Ollama's default local address
)
# Create (or reuse) a collection - like a table in a database
collection = client.get_or_create_collection(
name="personal_profile",
embedding_function=ef, # Tells ChromaDB how to convert text to vectors
)
# Add chunks to the collection - ChromaDB automatically generates embeddings
collection.add(
ids=[f"chunk{i}" for i in range(len(chunks))], # Unique ID for each chunk
documents=chunks, # The actual text content
metadatas=[{"source": "profile", "chunk_index": i} for i in range(len(chunks))],
)
print(f"Added {len(chunks)} chunks to the 'personal_profile' collection.")
print("Knowledge base built successfully!")