-
Notifications
You must be signed in to change notification settings - Fork 22
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
How? some basic questions (get an specific ID) #95
Comments
Hi 👋 , I didn't have the use case for it yet, so I didn't implement any Short-term: One (ugly) workaround for now could be that you add the documents with the Long-term: But really there should be a separate If you don't have the capacity to work on this, I can do it as well. |
PS: Also not recommended, but if you need it ASAP and can't wait for the package chromem_test
import (
"context"
"fmt"
"reflect"
"testing"
"unsafe"
"github.com/philippgille/chromem-go"
)
func TestCollection_Reflection(t *testing.T) {
// Create collection
db := chromem.NewDB()
name := "test"
metadata := map[string]string{"foo": "bar"}
vectors := []float32{-0.40824828, 0.40824828, 0.81649655} // normalized version of `{-0.1, 0.1, 0.2}`
embeddingFunc := func(_ context.Context, _ string) ([]float32, error) {
return vectors, nil
}
c, err := db.CreateCollection(name, metadata, embeddingFunc)
if err != nil {
t.Fatal("expected no error, got", err)
}
if c == nil {
t.Fatal("expected collection, got nil")
}
// Add documents
ids := []string{"1", "2"}
metadatas := []map[string]string{{"foo": "bar"}, {"a": "b"}}
contents := []string{"hello world", "hallo welt"}
err = c.Add(context.Background(), ids, nil, metadatas, contents)
if err != nil {
t.Fatal("expected nil, got", err)
}
// Access the collection's internal documents map via reflection
docMapField := reflect.ValueOf(c).Elem().FieldByName("documents")
if !docMapField.IsValid() {
t.Fatal("expected to be able to access the documents map via reflection")
}
if docMapField.Kind() != reflect.Map {
t.Fatal("documents field is not a map")
}
// Use unsafe operations to access the unexported field
docMapPtr := unsafe.Pointer(docMapField.UnsafeAddr())
docMapVal := reflect.NewAt(docMapField.Type(), docMapPtr).Elem()
docMap, ok := docMapVal.Interface().(map[string]*chromem.Document)
if !ok {
t.Fatal("expected to be able to do type assertion on the documents map")
}
if len(docMap) != 2 {
t.Fatalf("expected 2 documents, got %d", len(docMap))
}
fmt.Printf("Got by ID: %v\n", docMap["1"].Content) // Running the test with `-v` flag prints "Got by ID: hello world"
} Running the test with |
I went with just Hope that works for you! Otherwise let me know and I can adapt it. |
How I can get an specific ID? I have the ID in the metadata of another item.
doc.Query(ctx, "", 1, map[string]string{"id": x.Metadata["reference"]}, nil)
Or I need to add the ID on metadatas?
Currenly I add the data with:
c.Add(ctx, []string{chunk.ID}, embeddings, []map[string]string{addToMapString(map[string]string{"source": fmt.Sprintf("%s:%d", source.Name, i)}, chunk.Metadata)}, []string{chunk.Content})
Thanks
The text was updated successfully, but these errors were encountered: