Skip to content

Commit

Permalink
Rename Client to Index
Browse files Browse the repository at this point in the history
This is to align go sdk with py and js.
  • Loading branch information
mdogan committed Jan 30, 2024
1 parent 35cfdac commit 049521f
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 55 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ go get github.com/upstash/vector-go

### Initializing the client

There are two pieces of configuration required to use the Upstash vector client: an REST token and REST URL.
There are two pieces of configuration required to use the Upstash Vector index client: an REST token and REST URL.
Find your configuration values in the console dashboard at [https://console.upstash.com/](https://console.upstash.com/).

```go
Expand All @@ -25,7 +25,7 @@ import (
)

func main() {
client := vector.NewClient("<UPSTASH_VECTOR_REST_URL>", "<UPSTASH_VECTOR_REST_TOKEN>")
index := vector.NewIndex("<UPSTASH_VECTOR_REST_URL>", "<UPSTASH_VECTOR_REST_TOKEN>")
}
```

Expand All @@ -36,15 +36,15 @@ UPSTASH_VECTOR_REST_URL="your_rest_url"
UPSTASH_VECTOR_REST_TOKEN="your_rest_token"
```

and then create client by using:
and then create index client by using:

```go
import (
"github.com/upstash/vector-go"
)

func main() {
client := vector.NewClientFromEnv()
index := vector.NewIndexFromEnv()
}
```

Expand All @@ -67,7 +67,7 @@ func main() {
Token: "<UPSTASH_VECTOR_REST_TOKEN>",
Client: &http.Client{},
}
client := vector.NewClientWith(opts)
index := vector.NewIndexWith(opts)
}
```

Expand Down Expand Up @@ -97,13 +97,13 @@ upserts := []vector.Upsert{
},
}

err := client.UpsertMany(upserts)
err := index.UpsertMany(upserts)
```

#### Upsert One

```go
err := client.Upsert(vector.Upsert{
err := index.Upsert(vector.Upsert{
Id: "2",
Vector: []float32{1.0, 0.0},
})
Expand All @@ -122,7 +122,7 @@ When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.

```go
scores, err := client.Query(vector.Query{
scores, err := index.Query(vector.Query{
Vector: []float32{0.0, 1.0},
TopK: 2,
IncludeVectors: false,
Expand All @@ -140,7 +140,7 @@ When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.

```go
vectors, err := client.Fetch(vector.Fetch{
vectors, err := index.Fetch(vector.Fetch{
Ids: []string{"0", "1"},
IncludeVectors: false,
IncludeMetadata: false,
Expand All @@ -154,13 +154,13 @@ Vectors can be deleted from the index.
#### Delete many

```go
count, err := client.DeleteMany([]string{"0", "999"})
count, err := index.DeleteMany([]string{"0", "999"})
```

#### Delete One

```go
ok, err := client.Delete("2")
ok, err := index.Delete("2")
```

### Scanning the Vectors
Expand All @@ -175,7 +175,7 @@ When `IncludeMetadata` is `true`, the response will contain the metadata of the
vectors, if any.

```go
vectors, err := client.Range(vector.Range{
vectors, err := index.Range(vector.Range{
Cursor: "0",
Limit: 10,
IncludeVectors: false,
Expand All @@ -188,7 +188,7 @@ for vectors.NextCursor != "" {
}

// Fetch the next range batch
vectors, err = client.Range(vector.Range{
vectors, err = index.Range(vector.Range{
Cursor: vectors.NextCursor,
Limit: 10,
IncludeVectors: false,
Expand All @@ -202,11 +202,11 @@ for vectors.NextCursor != "" {
Reset will delete all the vectors and reset the index to its initial state.

```go
err := client.Reset()
err := index.Reset()
```

### Getting Index Information

```go
info, err := client.Info()
info, err := index.Info()
```
8 changes: 4 additions & 4 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const deletePath = "/delete"
// Delete deletes the vector with the given id and reports
// whether the vector is deleted. If a vector with the given
// id is not found, Delete returns false.
func (c *Client) Delete(id string) (ok bool, err error) {
func (ix *Index) Delete(id string) (ok bool, err error) {
// workaround for server that does not accept string bodies
data, err := c.sendJson(deletePath, []string{id})
data, err := ix.sendJson(deletePath, []string{id})
if err != nil {
return
}
Expand All @@ -19,8 +19,8 @@ func (c *Client) Delete(id string) (ok bool, err error) {

// DeleteMany deletes the vectors with the given ids and reports
// how many of them are deleted.
func (c *Client) DeleteMany(ids []string) (count int, err error) {
data, err := c.sendJson(deletePath, ids)
func (ix *Index) DeleteMany(ids []string) (count int, err error) {
data, err := ix.sendJson(deletePath, ids)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const fetchPath = "/fetch"
// When f.IncludeVectors is true, values of the vectors are also
// returned. When f.IncludeMetadata is true, metadata of the vectors
// are also returned, if any.
func (c *Client) Fetch(f Fetch) (vectors []Vector, err error) {
data, err := c.sendJson(fetchPath, f)
func (ix *Index) Fetch(f Fetch) (vectors []Vector, err error) {
data, err := ix.sendJson(fetchPath, f)
if err != nil {
return
}
Expand Down
37 changes: 19 additions & 18 deletions client.go → index.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,60 @@ func (o *Options) init() {
}
}

// NewClient returns a client to be used with Upstash Vector
// NewIndex returns an index client to be used with Upstash Vector
// with the given url and token.
func NewClient(url string, token string) *Client {
return NewClientWith(Options{
func NewIndex(url string, token string) *Index {
return NewIndexWith(Options{
Url: url,
Token: token,
})
}

// NewClientFromEnv returns a client to be used with Upstash Vector
// NewIndexFromEnv returns an index client to be used with Upstash Vector
// by reading URL and token from the environment variables.
func NewClientFromEnv() *Client {
return NewClientWith(Options{
func NewIndexFromEnv() *Index {
return NewIndexWith(Options{
Url: os.Getenv(UrlEnvProperty),
Token: os.Getenv(TokenEnvProperty),
})
}

// NewClientWith returns a client to be used with Upstash Vector
// NewIndexWith returns an index client to be used with Upstash Vector
// with the given options.
func NewClientWith(options Options) *Client {
func NewIndexWith(options Options) *Index {
options.init()
return &Client{
return &Index{
url: options.Url,
token: options.Token,
client: options.Client,
}
}

type Client struct {
// Index is a client for Upstash Vector index.
type Index struct {
url string
token string
client *http.Client
}

func (c *Client) sendJson(path string, obj any) (data []byte, err error) {
func (ix *Index) sendJson(path string, obj any) (data []byte, err error) {
if data, err = json.Marshal(obj); err != nil {
return
}
return c.sendBytes(path, data)
return ix.sendBytes(path, data)
}

func (c *Client) sendBytes(path string, obj []byte) (data []byte, err error) {
return c.send(path, bytes.NewReader(obj))
func (ix *Index) sendBytes(path string, obj []byte) (data []byte, err error) {
return ix.send(path, bytes.NewReader(obj))
}

func (c *Client) send(path string, r io.Reader) (data []byte, err error) {
request, err := http.NewRequest("POST", c.url+path, r)
func (ix *Index) send(path string, r io.Reader) (data []byte, err error) {
request, err := http.NewRequest("POST", ix.url+path, r)
if err != nil {
return
}
request.Header.Add("Authorization", "Bearer "+c.token)
response, err := c.client.Do(request)
request.Header.Add("Authorization", "Bearer "+ix.token)
response, err := ix.client.Do(request)
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions client_test.go → index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/stretchr/testify/require"
)

func newTestClient() (*Client, error) {
client := NewClient(
func newTestClient() (*Index, error) {
client := NewIndex(
os.Getenv(UrlEnvProperty),
os.Getenv(TokenEnvProperty),
)
Expand All @@ -22,14 +22,14 @@ func newTestClient() (*Client, error) {
return client, nil
}

func newTestClientWith(client *http.Client) (*Client, error) {
func newTestClientWith(client *http.Client) (*Index, error) {
opts := Options{
Url: os.Getenv(UrlEnvProperty),
Token: os.Getenv(TokenEnvProperty),
Client: client,
}

c := NewClientWith(opts)
c := NewIndexWith(opts)

err := c.Reset()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const infoPath = "/info"
// vector count, vectors pending for indexing, size of the
// index on disk in bytes, dimension of the index, and
// the name of the similarity function used.
func (c *Client) Info() (info IndexInfo, err error) {
data, err := c.sendJson(infoPath, nil)
func (ix *Index) Info() (info IndexInfo, err error) {
data, err := ix.sendJson(infoPath, nil)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const queryPath = "/query"
// given query vector. When q.IncludeVectors is true, values of the vectors are
// also returned. When q.IncludeMetadata is true, metadata of the vectors are
// also returned, if any.
func (c *Client) Query(q Query) (scores []VectorScore, err error) {
data, err := c.sendJson(queryPath, q)
func (ix *Index) Query(q Query) (scores []VectorScore, err error) {
data, err := ix.sendJson(queryPath, q)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const rangePath = "/range"
// When r.IncludeVectors is true, values of the vectors are also returned.
// When r.IncludeMetadata is true, metadata of the vectors are also returned,
// if any.
func (c *Client) Range(r Range) (vectors RangeVectors, err error) {
data, err := c.sendJson(rangePath, r)
func (ix *Index) Range(r Range) (vectors RangeVectors, err error) {
data, err := ix.sendJson(rangePath, r)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const resetPath = "/reset"

// Reset deletes all the vectors in the index and resets
// it to initial state.
func (c *Client) Reset() (err error) {
data, err := c.send(resetPath, nil)
func (ix *Index) Reset() (err error) {
data, err := ix.send(resetPath, nil)
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const upsertPath = "/upsert"

// Upsert updates or inserts a vector to the index.
// Additional metadata can also be provided while upserting the vector.
func (c *Client) Upsert(u Upsert) (err error) {
data, err := c.sendJson(upsertPath, u)
func (ix *Index) Upsert(u Upsert) (err error) {
data, err := ix.sendJson(upsertPath, u)
if err != nil {
return
}
Expand All @@ -15,8 +15,8 @@ func (c *Client) Upsert(u Upsert) (err error) {

// UpsertMany updates or inserts some vectors to the index.
// Additional metadata can also be provided for each vector.
func (c *Client) UpsertMany(u []Upsert) (err error) {
data, err := c.sendJson(upsertPath, u)
func (ix *Index) UpsertMany(u []Upsert) (err error) {
data, err := ix.sendJson(upsertPath, u)
if err != nil {
return
}
Expand Down

0 comments on commit 049521f

Please sign in to comment.