-
-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vectorstores: add mongovector #1005
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
898ae09
GODRIVER-3305 POC
prestonvasquez fb3cf39
GODRIVER-3305 Extend test seed to be full doc
prestonvasquez ea4cd0b
GODRIVER-3305 Cont. w/ tests
prestonvasquez daff915
GODRIVER-3305 Cleanup comments and add setup test flag
prestonvasquez c5e1e20
GODRIVER-3305 Fix lintin errors
prestonvasquez 657a618
GODRIVER-3305 Fix linting errors
prestonvasquez 850fe06
GODRIVER-3305 Cont. Cleanup
prestonvasquez 922aae5
GODRIVER-3305 Update for free tier
prestonvasquez 1e99999
mongovector: Add doc.go
tmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package mongovector | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"fmt" | ||
"math/big" | ||
"time" | ||
|
||
"github.com/tmc/langchaingo/embeddings" | ||
"github.com/tmc/langchaingo/schema" | ||
"github.com/tmc/langchaingo/vectorstores" | ||
) | ||
|
||
type mockEmbedder struct { | ||
dim int | ||
query string // query that will be used in the search | ||
queryVector []float32 | ||
docSet map[string]schema.Document // pageContent to expected doc | ||
flushedDocSet map[string][]float32 | ||
} | ||
|
||
var _ embeddings.Embedder = &mockEmbedder{} | ||
|
||
func newMockEmbedder(dim int, query string) *mockEmbedder { | ||
emb := &mockEmbedder{ | ||
dim: dim, | ||
query: query, | ||
docSet: make(map[string]schema.Document), | ||
} | ||
|
||
return emb | ||
} | ||
|
||
// Store a document that will be returned by a similarity search on the provided | ||
// query with the specific score. | ||
func (emb *mockEmbedder) addDocument(doc schema.Document) error { | ||
if emb.flushedDocSet != nil { | ||
return fmt.Errorf("cannot make new queries after flushing") | ||
} | ||
|
||
emb.docSet[doc.PageContent] = doc | ||
|
||
return nil | ||
} | ||
|
||
func (emb *mockEmbedder) flush(ctx context.Context, store Store) error { | ||
// Create a vector for each document such that all vectors are linearly | ||
// independent. Leave one space at teh end for scaling. | ||
vectors := makeLinearlyIndependentVectors(len(emb.docSet), emb.dim) | ||
|
||
// Create a linearly independent query vector. | ||
emb.queryVector = makeOrthogonalVector(emb.dim, vectors...) | ||
|
||
// For each pageContent + score combo, update the corresponding vector with | ||
// a final element so that it's dot product with queryVector is 2S - 1 | ||
// where S is the desired simlarity score. | ||
emb.flushedDocSet = make(map[string][]float32) | ||
docs := make([]schema.Document, 0, len(emb.docSet)) | ||
|
||
count := 0 | ||
for pageContent, doc := range emb.docSet { | ||
emb.flushedDocSet[pageContent] = makeScoreVector(doc.Score, emb.queryVector, vectors[count]) | ||
docs = append(docs, doc) | ||
|
||
count++ | ||
} | ||
|
||
_, err := store.AddDocuments(ctx, docs, vectorstores.WithEmbedder(emb)) | ||
if err != nil { | ||
return fmt.Errorf("failed to add documents: %w", err) | ||
} | ||
|
||
// The read consistency for vector search isn't automatic. | ||
time.Sleep(1 * time.Second) | ||
|
||
return nil | ||
} | ||
|
||
func (emb *mockEmbedder) EmbedDocuments(ctx context.Context, texts []string) ([][]float32, error) { | ||
vectors := make([][]float32, len(texts)) | ||
for i := range vectors { | ||
var ok bool | ||
|
||
vectors[i], ok = emb.flushedDocSet[texts[i]] | ||
if !ok { | ||
vectors[i] = makeVector(emb.dim) | ||
} | ||
} | ||
|
||
return vectors, nil | ||
} | ||
|
||
func (emb *mockEmbedder) EmbedQuery(ctx context.Context, text string) ([]float32, error) { | ||
return emb.queryVector, nil | ||
} | ||
|
||
// newNormalizedFloat32 will generate a random float32 in [-1, 1]. | ||
func newNormalizedFloat32() (float32, error) { | ||
max := big.NewInt(1 << 24) | ||
|
||
n, err := rand.Int(rand.Reader, max) | ||
if err != nil { | ||
return 0.0, fmt.Errorf("failed to normalize float32") | ||
} | ||
|
||
return 2.0*(float32(n.Int64())/float32(1<<24)) - 1.0, nil | ||
} | ||
|
||
// dotProduct will return the dot product between two slices of f32. | ||
func dotProduct(v1, v2 []float32) (sum float32) { | ||
for i := range v1 { | ||
sum += v1[i] * v2[i] | ||
} | ||
|
||
return | ||
} | ||
|
||
// linearlyIndependent true if the vectors are linearly independent | ||
func linearlyIndependent(v1, v2 []float32) bool { | ||
var ratio float32 | ||
|
||
for i := range v1 { | ||
if v1[i] != 0 { | ||
r := v2[i] / v1[i] | ||
|
||
if ratio == 0 { | ||
ratio = r | ||
|
||
continue | ||
} | ||
|
||
if r == ratio { | ||
continue | ||
} | ||
|
||
return true | ||
} | ||
|
||
if v2[i] != 0 { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// Update the basis vector such that qvector * basis = 2S - 1. | ||
func makeScoreVector(S float32, qvector []float32, basis []float32) []float32 { | ||
var sum float32 | ||
|
||
// Populate v2 upto dim-1. | ||
for i := 0; i < len(qvector)-1; i++ { | ||
sum += qvector[i] * basis[i] | ||
} | ||
|
||
// Calculate v_{2, dim} such that v1 * v2 = 2S - 1: | ||
basis[len(basis)-1] = (2*S - 1 - sum) / qvector[len(qvector)-1] | ||
|
||
// If the vectors are linearly independent, regenerate the dim-1 elements | ||
// of v2. | ||
if !linearlyIndependent(qvector, basis) { | ||
return makeScoreVector(S, qvector, basis) | ||
} | ||
|
||
return basis | ||
} | ||
|
||
// makeVector will create a vector of values beween [-1, 1] of the specified | ||
// size. | ||
func makeVector(dim int) []float32 { | ||
vector := make([]float32, dim) | ||
for i := range vector { | ||
vector[i], _ = newNormalizedFloat32() | ||
} | ||
|
||
return vector | ||
} | ||
|
||
// Use Gram Schmidt to return a vector orthogonal to the basis, so long as | ||
// the vectors in the basis are linearly independent. | ||
func makeOrthogonalVector(dim int, basis ...[]float32) []float32 { | ||
candidate := makeVector(dim) | ||
|
||
for _, b := range basis { | ||
dp := dotProduct(candidate, b) | ||
basisNorm := dotProduct(b, b) | ||
|
||
for i := range candidate { | ||
candidate[i] -= (dp / basisNorm) * b[i] | ||
} | ||
} | ||
|
||
return candidate | ||
} | ||
|
||
// Make n linearly independent vectors of size dim. | ||
func makeLinearlyIndependentVectors(n int, dim int) [][]float32 { | ||
vectors := [][]float32{} | ||
|
||
for i := 0; i < n; i++ { | ||
v := makeOrthogonalVector(dim, vectors...) | ||
|
||
vectors = append(vectors, v) | ||
} | ||
|
||
return vectors | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mongovector | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tmc/langchaingo/embeddings" | ||
) | ||
|
||
// mockLLM will create consistent text embeddings mocking the OpenAI | ||
// text-embedding-3-small algorithm. | ||
type mockLLM struct { | ||
seen map[string][]float32 | ||
dim int | ||
} | ||
|
||
var _ embeddings.EmbedderClient = &mockLLM{} | ||
|
||
// createEmbedding will return vector embeddings for the mock LLM, maintaining | ||
// consitency. | ||
func (emb *mockLLM) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error) { | ||
if emb.seen == nil { | ||
emb.seen = map[string][]float32{} | ||
} | ||
|
||
vectors := make([][]float32, len(texts)) | ||
for i, text := range texts { | ||
if f32s := emb.seen[text]; len(f32s) > 0 { | ||
vectors[i] = f32s | ||
|
||
continue | ||
} | ||
|
||
vectors[i] = makeVector(emb.dim) | ||
emb.seen[text] = vectors[i] // ensure consistency | ||
} | ||
|
||
return vectors, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move these mocks into a separate subpackage (mocks? mongovectormocks?)