Skip to content

Commit

Permalink
Add data support to upsert/query/fetch operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdogan committed Oct 30, 2024
1 parent bdd081c commit 9176c94
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache-dependency-path: 'go.sum'
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ upserts := []vector.Upsert{
Id: "1",
Vector: []float32{0.0, 1.0},
Metadata: map[string]any{"foo": "bar"}, // optional metadata
Data: "vector data", // optional data
},
}

Expand Down Expand Up @@ -205,12 +206,15 @@ When `IncludeVectors` is `true`, the response will contain the vector values.
When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.
When `IncludeData` is `true`, the response will contain the data of the vectors, if any.
```go
scores, err := index.Query(vector.Query{
Vector: []float32{0.0, 1.0},
TopK: 2,
IncludeVectors: false,
IncludeMetadata: false,
IncludeData: false,
})
```
Expand All @@ -224,6 +228,7 @@ scores, err := index.Query(vector.Query{
TopK: 2,
IncludeVectors: false,
IncludeMetadata: false,
IncludeData: false,
Filter: `foo = 'bar'`
})
```
Expand All @@ -240,12 +245,15 @@ When `IncludeVectors` is `true`, the response will contain the vector values.
When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.
When `IncludeData` is `true`, the response will contain the data of the vectors, if any.
```go
scores, err := index.QueryData(vector.QueryData{
Data: "Where is the capital of Turkey?",
TopK: 2,
IncludeVectors: false,
IncludeMetadata: false,
IncludeData: false,
Filter: `foo = 'bar'`
})
```
Expand All @@ -259,11 +267,14 @@ When `IncludeVectors` is `true`, the response will contain the vector values.
When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.
When `IncludeData` is `true`, the response will contain the data of the vectors, if any.
```go
vectors, err := index.Fetch(vector.Fetch{
Ids: []string{"0", "1"},
IncludeVectors: false,
IncludeMetadata: false,
IncludeData: false,
})
```
Expand Down Expand Up @@ -294,12 +305,15 @@ When `IncludeVectors` is `true`, the response will contain the vector values.
When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.
When `IncludeData` is `true`, the response will contain the data of the vectors, if any.
```go
vectors, err := index.Range(vector.Range{
Cursor: "0",
Limit: 10,
IncludeVectors: false,
IncludeMetadata: false,
IncludeData: false,
})

for vectors.NextCursor != "" {
Expand All @@ -313,6 +327,7 @@ for vectors.NextCursor != "" {
Limit: 10,
IncludeVectors: false,
IncludeMetadata: false,
IncludeData: false,
})
}
```
Expand Down Expand Up @@ -349,4 +364,4 @@ The default namespaces cannot be deleted.
```go
err := index.Namespace("ns").DeleteNamespace()
```
```
7 changes: 6 additions & 1 deletion embedding_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package vector

import (
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestEmbedding(t *testing.T) {
Expand Down Expand Up @@ -62,18 +63,21 @@ func TestEmbedding(t *testing.T) {
Data: "Which country's capital is Ankara?",
TopK: 1,
IncludeMetadata: true,
IncludeData: true,
})
require.NoError(t, err)
require.Equal(t, 1, len(scores))
require.Equal(t, id0, scores[0].Id)
require.Equal(t, map[string]any{"country": "tr", "capital": "Ankara"}, scores[0].Metadata)
require.Equal(t, "Capital of Türkiye is Ankara.", scores[0].Data)
})

t.Run("with metadata filtering", func(t *testing.T) {
query := QueryData{
Data: "Where is the capital of France?",
TopK: 1,
IncludeMetadata: true,
IncludeData: true,
Filter: `country = 'fr'`,
}

Expand All @@ -82,6 +86,7 @@ func TestEmbedding(t *testing.T) {
require.Equal(t, 1, len(scores))
require.Equal(t, id3, scores[0].Id)
require.Equal(t, map[string]any{"country": "fr", "capital": "Paris"}, scores[0].Metadata)
require.Equal(t, "Capital of France is Paris.", scores[0].Data)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/upstash/vector-go

go 1.20
go 1.22

require (
github.com/joho/godotenv v1.5.1
Expand Down
12 changes: 11 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package vector

import (
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestQuery(t *testing.T) {
Expand All @@ -21,10 +22,12 @@ func TestQuery(t *testing.T) {
Id: id0,
Vector: []float32{0, 1},
Metadata: map[string]any{"foo": "bar"},
Data: "vector0 data",
},
{
Id: id1,
Vector: []float32{5, 10},
Data: "vector1 data",
},
{
Id: id2,
Expand Down Expand Up @@ -58,16 +61,20 @@ func TestQuery(t *testing.T) {
TopK: 2,
IncludeMetadata: true,
IncludeVectors: true,
IncludeData: true,
})
require.NoError(t, err)
require.Equal(t, 2, len(scores))
require.Equal(t, id0, scores[0].Id)
require.Equal(t, float32(1.0), scores[0].Score)
require.Equal(t, map[string]any{"foo": "bar"}, scores[0].Metadata)
require.Equal(t, "vector0 data", scores[0].Data)
require.Equal(t, []float32{0, 1}, scores[0].Vector)

require.Equal(t, id2, scores[1].Id)
require.Equal(t, []float32{0.01, 1.01}, scores[1].Vector)
require.Equal(t, map[string]any{"foo": "nay"}, scores[1].Metadata)
require.Empty(t, scores[1].Data)
})

t.Run("with metadata filtering", func(t *testing.T) {
Expand All @@ -76,6 +83,7 @@ func TestQuery(t *testing.T) {
TopK: 10,
IncludeMetadata: true,
IncludeVectors: true,
IncludeData: true,
Filter: `foo = 'bar'`,
}

Expand All @@ -85,6 +93,7 @@ func TestQuery(t *testing.T) {
require.Equal(t, id0, scores[0].Id)
require.Equal(t, float32(1.0), scores[0].Score)
require.Equal(t, map[string]any{"foo": "bar"}, scores[0].Metadata)
require.Equal(t, "vector0 data", scores[0].Data)
require.Equal(t, []float32{0, 1}, scores[0].Vector)

query.Filter = `foo = 'nay'`
Expand All @@ -94,6 +103,7 @@ func TestQuery(t *testing.T) {
require.Equal(t, id2, scores[0].Id)
require.Equal(t, map[string]any{"foo": "nay"}, scores[0].Metadata)
require.Equal(t, []float32{0.01, 1.01}, scores[0].Vector)
require.Empty(t, scores[0].Data)
})
})
}
Expand Down
21 changes: 21 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ type Upsert struct {
// Vector values.
Vector []float32 `json:"vector"`

// Optional data of the vector.
Data string `json:"data,omitempty"`

// Optional metadata of the vector.
Metadata map[string]any `json:"metadata,omitempty"`
}
Expand Down Expand Up @@ -37,6 +40,9 @@ type Query struct {
// Whether to include metadata in the query response, if any.
IncludeMetadata bool `json:"includeMetadata,omitempty"`

// Whether to include data in the query response, if any.
IncludeData bool `json:"includeData,omitempty"`

// Query filter
Filter any `json:"filter,omitempty"`
}
Expand All @@ -56,6 +62,9 @@ type QueryData struct {
// Whether to include metadata in the query response, if any.
IncludeMetadata bool `json:"includeMetadata,omitempty"`

// Whether to include data in the query response, if any.
IncludeData bool `json:"includeData,omitempty"`

// Query filter
Filter any `json:"filter,omitempty"`
}
Expand All @@ -69,6 +78,9 @@ type Vector struct {

// Optional metadata of the vector, if any.
Metadata map[string]any `json:"metadata,omitempty"`

// Optional data of the vector.
Data string `json:"data,omitempty"`
}

type VectorScore struct {
Expand All @@ -84,6 +96,9 @@ type VectorScore struct {

// Optional metadata of the vector, if any.
Metadata map[string]any `json:"metadata,omitempty"`

// Optional data of the vector.
Data string `json:"data,omitempty"`
}

type Fetch struct {
Expand All @@ -95,6 +110,9 @@ type Fetch struct {

// Whether to include metadata in the fetch response, if any.
IncludeMetadata bool `json:"includeMetadata,omitempty"`

// Whether to include data in the query response, if any.
IncludeData bool `json:"includeData,omitempty"`
}

type Range struct {
Expand All @@ -110,6 +128,9 @@ type Range struct {

// Whether to include metadata in the fetch response, if any.
IncludeMetadata bool `json:"includeMetadata,omitempty"`

// Whether to include data in the query response, if any.
IncludeData bool `json:"includeData,omitempty"`
}

type RangeVectors struct {
Expand Down
6 changes: 5 additions & 1 deletion upsert_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package vector

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestUpsert(t *testing.T) {
Expand Down Expand Up @@ -57,19 +58,22 @@ func TestUpsert(t *testing.T) {
Id: id,
Vector: []float32{0, 1},
Metadata: map[string]any{"foo": "bar"},
Data: "some data",
})
require.NoError(t, err)

vectors, err := client.Fetch(Fetch{
Ids: []string{id},
IncludeMetadata: true,
IncludeVectors: true,
IncludeData: true,
})
require.NoError(t, err)
require.Equal(t, 1, len(vectors))
require.Equal(t, id, vectors[0].Id)
require.Equal(t, map[string]any{"foo": "bar"}, vectors[0].Metadata)
require.Equal(t, []float32{0, 1}, vectors[0].Vector)
require.Equal(t, "some data", vectors[0].Data)
})
})
}
Expand Down

0 comments on commit 9176c94

Please sign in to comment.