From b172263f9f27a99e7137f398e8b28027a7e35c9f Mon Sep 17 00:00:00 2001 From: Bogdan Buduroiu Date: Thu, 29 Aug 2024 19:30:48 +0800 Subject: [PATCH] fix: ignore missing namespaces --- pinecone/handler.go | 15 +++------------ pinecone/index.go | 33 ++++++++++++--------------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/pinecone/handler.go b/pinecone/handler.go index 044b53e..89fcc3d 100644 --- a/pinecone/handler.go +++ b/pinecone/handler.go @@ -137,14 +137,9 @@ func (h *Handler) FetchVectors(w http.ResponseWriter, r *http.Request) { ids := params["ids"] namespace := params.Get("namespace") - vectors, err := h.Index.Fetch(namespace, ids) + vectors := h.Index.Fetch(namespace, ids) vectorResponse := make(map[string]*Vector, len(vectors)) - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } - for _, vector := range vectors { vectorResponse[vector.ID] = vector } @@ -167,7 +162,7 @@ func (h *Handler) UpdateVector(w http.ResponseWriter, r *http.Request) { return } - vector, err := h.Index.GetVector(vectorUpdate.Namespace, vectorUpdate.ID) + vector := h.Index.GetVector(vectorUpdate.Namespace, vectorUpdate.ID) if vector == nil { w.WriteHeader(http.StatusNotFound) @@ -213,11 +208,7 @@ func (h *Handler) ListVectorIDs(w http.ResponseWriter, r *http.Request) { prefix := params.Get("prefix") namespace := params.Get("namespace") - vectors, err := h.Index.ListVectorIDs(namespace, prefix) - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } + vectors := h.Index.ListVectorIDs(namespace, prefix) response := make(map[string]any) response["vectors"] = vectors diff --git a/pinecone/index.go b/pinecone/index.go index f89518a..1d96a2d 100644 --- a/pinecone/index.go +++ b/pinecone/index.go @@ -1,8 +1,6 @@ package pinecone import ( - "errors" - "fmt" "strings" ) @@ -26,16 +24,14 @@ func (i *Index) CreateNamespace(name string) { i.Namespaces[name] = make(map[string]*Vector) } -func (i *Index) UpsertVector(namespace string, vector *Vector) error { +func (i *Index) UpsertVector(namespace string, vector *Vector) { ns, exists := i.Namespaces[namespace] - var err error if !exists { - err = errors.New(fmt.Sprintf("Namespace not found: %s", namespace)) + ns = map[string]*Vector{vector.ID: vector} } else { ns[vector.ID] = vector } - - return err + i.Namespaces[namespace] = ns } func (i *Index) Query(namespace string, topK int) ([]*Vector, error) { @@ -68,7 +64,7 @@ func (i *Index) Query(namespace string, topK int) ([]*Vector, error) { return vectors, nil } -func (i *Index) Fetch(namespace string, ids []string) ([]*Vector, error) { +func (i *Index) Fetch(namespace string, ids []string) []*Vector { vectors := make([]*Vector, 0) if namespace == "" { @@ -83,7 +79,7 @@ func (i *Index) Fetch(namespace string, ids []string) ([]*Vector, error) { } else { ns, exists := i.Namespaces[namespace] if !exists { - return nil, errors.New(fmt.Sprintf("Namespace %s does not exist")) + return vectors } else { for _, id := range ids { vector, exists := ns[id] @@ -94,22 +90,17 @@ func (i *Index) Fetch(namespace string, ids []string) ([]*Vector, error) { } } - return vectors, nil + return vectors } -func (i *Index) GetVector(namespace string, id string) (*Vector, error) { +func (i *Index) GetVector(namespace string, id string) *Vector { ns, exists := i.Namespaces[namespace] if !exists { - err := errors.New(fmt.Sprintf("Namespace not found: %s", namespace)) - return nil, err + return &Vector{} } vector, exists := ns[id] - if !exists { - return nil, nil - } - - return vector, nil + return vector } func (i *Index) DeleteVector(vectorDelete VectorDeleteQuery) { @@ -134,13 +125,13 @@ func (i *Index) DeleteVector(vectorDelete VectorDeleteQuery) { } } -func (i *Index) ListVectorIDs(namespace string, prefix string) ([]map[string]string, error) { +func (i *Index) ListVectorIDs(namespace string, prefix string) []map[string]string { vectors := make([]map[string]string, 0) if namespace != "" { ns, exists := i.Namespaces[namespace] if !exists { - return nil, errors.New(fmt.Sprintf("Namespace %s does not exist")) + return vectors } for id, _ := range ns { if strings.HasPrefix(id, prefix) { @@ -158,5 +149,5 @@ func (i *Index) ListVectorIDs(namespace string, prefix string) ([]map[string]str } - return vectors, nil + return vectors }