Last active
August 24, 2023 21:10
-
-
Save jeffcogswell/501536e4b3987b31f28dbde671fb1fac to your computer and use it in GitHub Desktop.
chromadb example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import chromadb | |
chroma_client = chromadb.Client() | |
collection = chroma_client.create_collection(name="wiki") | |
def load_txt_files(directory): | |
txt_files = [] | |
names = [] | |
for filename in os.listdir(directory): | |
if filename.endswith(".txt"): | |
names.append(filename) | |
file_path = os.path.join(directory, filename) | |
with open(file_path, 'r') as file: | |
txt_files.append(file.read()) | |
return (txt_files, names) | |
# Replace 'directory_path' with the path of the directory containing your .txt files | |
docs, ids = load_txt_files('./wiki') # todo - move into a config file | |
collection.add( | |
documents=docs, | |
ids=ids | |
) | |
# First let's look for an exact match | |
results = collection.query( | |
query_texts = ['geckos are usually nocturnal'], | |
n_results = 1 | |
) | |
print(results['ids']) | |
results = collection.query( | |
query_texts = ['geckos are nocturnal'], | |
n_results = 1 | |
) | |
print(results['ids']) | |
results = collection.query( | |
query_texts = ['geckos are nighttime'], | |
n_results = 1 | |
) | |
print(results['ids']) | |
results = collection.query( | |
query_texts = ['geckos prefer nighttime'], | |
n_results = 1 | |
) | |
print(results['ids']) | |
print(results['ids']) | |
results = collection.query( | |
query_texts = ['lizards prefer nighttime'], | |
n_results = 1 | |
) | |
print(results['ids']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment