Skip to content

Adds vector search capabilities to Orama#462

Merged
micheleriva merged 5 commits intomainfrom
feat/vectors
Aug 4, 2023
Merged

Adds vector search capabilities to Orama#462
micheleriva merged 5 commits intomainfrom
feat/vectors

Conversation

@micheleriva
Copy link
Copy Markdown
Contributor

This PR introduces vector search capabilities in Orama. This will be part of Orama v1.2.0, the next major release.

New Vector API

Orama adds support for Vector search by adding a new datatype: vector[<size>]:

import { create } from '@orama/orama'

const db = await create({
  schema: {
    text: 'string',
    embedding: 'vector[1536]' // <--- vector size is mandatory here. OpenAI embeddings, for instance, have 1536 dimensions. 
  }
})

After you create your Orama instance, you can insert and search for vectors using the new searchVector function:

import { create, insert, searchVector } from '@orama/orama'

const db = await create({
  schema: {
    text: 'string',
    myVector: 'vector[5]'
  }
})

await insert(db, { text: 'foo', myVector: [1, 0, 0, 0, 0] })
await insert(db, { text: 'bar', myVector: [0, 0, 0, 0, 0] })
await insert(db, { text: 'baz', myVector: [1, 1, 1, 1, 1] })

const results = await searchVector(db, {
  vector: [1, 0, 0, 0, 0], // Your input vector
  property: 'myVector' // Property to search through is mandatory with the "searchVector" function
})

@vercel
Copy link
Copy Markdown

vercel bot commented Aug 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orama-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 4, 2023 9:38pm

@gustavopch
Copy link
Copy Markdown

gustavopch commented Aug 5, 2023

Awesome addition! One question: have you tested the performance? Like, how much time would it take to search among 30k vectors, 100k vectors, etc.? 🤔

I was trying to write a vector search implementation this week and it was taking about 80ms to perform a search among 30k vectors (1536 dimensions) on my MacBook M1. I was also indexing the vectors with their precalculated magnitudes like you did. That performance worried me because in production it would make the server stuck, unable to process anything else while performing a search (and would probably be slower than on my M1).

I believe one way to improve the performance would be to reduce the number of dimensions. I was looking for how to do that and found this Gist: https://gist.github.com/sepans/419d413f786b27872b34. Not sure how much impact it has on the quality of the search though, but I'm leaving the link here so you can check if you want.

@micheleriva
Copy link
Copy Markdown
Contributor Author

Hey @gustavopch, thank you so much!

I share your concerns; I made some tests locally and found no significant performance problems. I am sure though that we will find some edge cases and will eventually implement something similar to the gist you shared if we see that it's becoming more and more challenging to scale performances.

With that being said, I'd like to highlight a couple of things:

  1. You may not need OpenAI embeddings (since you use 1536 dimensions, I guess you're talking about them). Let me share this great article by Supabase: https://supabase.com/blog/fewer-dimensions-are-better-pgvector
  2. Cosine similarity is incredibly easy to parallelize. We plan to write some platform-specific plugins (Node.js, browsers, etc.) to parallelize execution on large vector sets.

Hope that helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants