Skip to content

Commit c880ff8

Browse files
committed
Cleanup
* updated README * added more linters * NewClient no longer returns error * updated godoc comments Signed-off-by: Milos Gajdos <[email protected]>
1 parent 17a63cc commit c880ff8

File tree

18 files changed

+104
-62
lines changed

18 files changed

+104
-62
lines changed

.golangci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ linters:
1212
- errcheck
1313
- goimports
1414
- prealloc
15+
- gosec
16+
- goconst
17+
- gosimple
18+
- errname
19+
- gocritic
20+
- tparallel
21+
- tenv
22+
- unconvert
23+
- whitespace
1524

1625
run:
1726
deadline: 2m

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
[![License: Apache-2.0](https://img.shields.io/badge/License-Apache--2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
66

77
This project provides an implementation for fetching embeddings from various LLMs.
8+
9+
Currently supported APIs:
10+
* [x] [OpenAI](https://platform.openai.com/docs/api-reference/embeddings)
11+
* [x] [Cohere AI](https://docs.cohere.com/reference/embed)
12+
* [x] [Google Vertex AI](https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings)
13+
14+
There are also simple command line tools provided by this project that let you query the APIs for text embeddings passed in via cli flags.

cmd/cohere/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ func init() {
2626
func main() {
2727
flag.Parse()
2828

29-
c, err := cohere.NewClient()
30-
if err != nil {
31-
log.Fatal(err)
32-
}
29+
c := cohere.NewClient()
3330

3431
embReq := &cohere.EmbeddingRequest{
3532
Texts: []string{input},

cmd/openai/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ func init() {
2424
func main() {
2525
flag.Parse()
2626

27-
c, err := openai.NewClient()
28-
if err != nil {
29-
log.Fatal(err)
30-
}
27+
c := openai.NewClient()
3128

3229
embReq := &openai.EmbeddingRequest{
3330
Input: input,

cmd/vertexai/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ func main() {
3636
log.Fatalf("token source: %v", err)
3737
}
3838

39-
c, err := vertexai.NewClient()
40-
if err != nil {
41-
log.Fatal(err)
42-
}
43-
c.WithTokenSrc(ts)
44-
c.WithModelID(model)
39+
c := vertexai.NewClient().
40+
WithTokenSrc(ts).
41+
WithModelID(model)
4542

4643
embReq := &vertexai.EmbeddingRequest{
4744
Instances: []vertexai.Instance{

cohere/client.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ type Client struct {
2525
hc *http.Client
2626
}
2727

28-
// NewClient creates a new HTTP client and returns it.
29-
// It reads the Cohere API key from COHERE_API_KEY env var
30-
// and uses the default Go http.Client.
31-
// You can override the default options by using the
32-
// client methods.
33-
func NewClient() (*Client, error) {
28+
// NewClient creates a new HTTP API client and returns it.
29+
// By default it reads the Cohere API key from COHERE_API_KEY
30+
// env var and uses the default Go http.Client for making API requests.
31+
// You can override the default options via the client methods.
32+
func NewClient() *Client {
3433
return &Client{
3534
apiKey: os.Getenv("COHERE_API_KEY"),
3635
baseURL: BaseURL,
3736
version: EmbedAPIVersion,
3837
hc: &http.Client{},
39-
}, nil
38+
}
4039
}
4140

4241
// WithAPIKey sets the API key.

cohere/cohere.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cohere
22

3-
// Model is embedding model.
3+
// Model is an embedding model.
44
type Model string
55

66
const (
@@ -13,7 +13,7 @@ const (
1313
MultiLingV2 Model = "embed-multilingual-v2.0"
1414
)
1515

16-
// InputType is embedding input type.
16+
// InputType is an embedding input type.
1717
type InputType string
1818

1919
const (
@@ -24,6 +24,8 @@ const (
2424
)
2525

2626
// Truncate controls input truncating.
27+
// It controls how the API handles inputs
28+
// longer than the maximum token length (recommended: <512)
2729
type Truncate string
2830

2931
const (

cohere/embedding.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"net/url"
1010
)
1111

12-
// Embedding is cohere API vector embedding.
12+
// Embedding is vector embedding.
1313
type Embedding struct {
1414
Vector []float64 `json:"vector"`
1515
}
@@ -22,13 +22,13 @@ type EmbeddingRequest struct {
2222
Truncate Truncate `json:"truncate,omitempty"`
2323
}
2424

25-
// EmbedddingResponse received from API endpoint.
25+
// EmbedddingResponse received from API.
2626
type EmbedddingResponse struct {
2727
Embeddings [][]float64 `json:"embeddings"`
2828
Meta *Meta `json:"meta,omitempty"`
2929
}
3030

31-
// Meta stores API response metadata
31+
// Meta stores API response metadata.
3232
type Meta struct {
3333
APIVersion *APIVersion `json:"api_version,omitempty"`
3434
}
@@ -38,7 +38,9 @@ type APIVersion struct {
3838
Version string `json:"version"`
3939
}
4040

41-
func ToEmbeddings(r io.Reader) ([]*Embedding, error) {
41+
// toEmbeddings decodes the raw API response,
42+
// parses it into a slice of embeddings and returns it.
43+
func toEmbeddings(r io.Reader) ([]*Embedding, error) {
4244
var resp EmbedddingResponse
4345
if err := json.NewDecoder(r).Decode(&resp); err != nil {
4446
return nil, err
@@ -80,5 +82,5 @@ func (c *Client) Embeddings(ctx context.Context, embReq *EmbeddingRequest) ([]*E
8082
}
8183
defer resp.Body.Close()
8284

83-
return ToEmbeddings(resp.Body)
85+
return toEmbeddings(resp.Body)
8486
}

cohere/error.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cohere
22

33
import "encoding/json"
44

5+
// APIError is Cohere API error.
56
type APIError struct {
67
Message string `json:"message"`
78
}
89

10+
// Error implements errors interface.
911
func (e APIError) Error() string {
1012
b, err := json.Marshal(e)
1113
if err != nil {

openai/client.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
const (
1414
// BaseURL is OpenAI HTTP API base URL.
1515
BaseURL = "https://api.openai.com"
16-
// EmbedAPIVersion is the latest stable embedding API version.
16+
// EmbedAPIVersion is the latest stable embeddings API version.
1717
EmbedAPIVersion = "v1"
18-
// Org header
18+
// OrgHeader is an Organization header
1919
OrgHeader = "OpenAI-Organization"
2020
)
2121

22-
// Client is OpenAI HTTP API client.
22+
// Client is an OpenAI HTTP API client.
2323
type Client struct {
2424
apiKey string
2525
baseURL string
@@ -28,19 +28,18 @@ type Client struct {
2828
hc *http.Client
2929
}
3030

31-
// NewClient creates a new HTTP client and returns it.
32-
// It reads the OpenAI API key from OPENAI_API_KEY env var
33-
// and uses the default Go http.Client.
34-
// You can override the default options by using the
35-
// client methods.
36-
func NewClient() (*Client, error) {
31+
// NewClient creates a new HTTP API client and returns it.
32+
// By default it reads the OpenAI API key from OPENAI_API_KEY
33+
// env var and uses the default Go http.Client for making API requests.
34+
// You can override the default options via the client methods.
35+
func NewClient() *Client {
3736
return &Client{
3837
apiKey: os.Getenv("OPENAI_API_KEY"),
3938
baseURL: BaseURL,
4039
version: EmbedAPIVersion,
4140
orgID: "",
4241
hc: &http.Client{},
43-
}, nil
42+
}
4443
}
4544

4645
// WithAPIKey sets the API key.

0 commit comments

Comments
 (0)