Repository navigation
Replies: 2 comments
|
This is where I've gotten with trying to rewrite the core |
|
Your draft registers import llm
def similar_since(collection, text, since, number=10):
vector = collection.model().embed(text)
def distance_score(encoded):
return llm.cosine_similarity(llm.decode(encoded), vector)
collection.db.register_function(distance_score, replace=True)
return list(collection.db.query("""
select id, content, metadata, distance_score(embedding) as score
from embeddings
where collection_id = :collection_id
and json_extract(metadata, '$.date') >= :since
order by score desc
limit :number
""", {"collection_id": collection.id, "since": since, "number": number}))
rows = similar_since(collection, "your search text", "2024-10-01")This returns row dictionaries; This scans the matching rows and computes cosine scores in Python, like the existing similarity implementation. It uses your collection's embedding model for the query; no need to embed the stored documents again. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm using llm to create an embeddings collection for a large group of documents, including JSON metadata (e.g., a date) in the
metadatacolumn. Usingcollection.similar()works to query the collection as a whole, but I'd like to be able to query just a subset — e.g., all records with a metadata date before or after an arbitrary point in time. Is that possible, either directly with llm, or by using sqlite-utils behind the scenes somehow?All reactions