Skip to content

Commit

Permalink
llm: add openai client
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed May 11, 2024
1 parent 59204f2 commit 6cc9f2a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ require (
github.com/getsavvyinc/upgrade-cli v0.3.0
github.com/muesli/cancelreader v0.2.2
github.com/muesli/termenv v0.15.2
github.com/sashabaranov/go-openai v1.23.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.6.0
golang.org/x/term v0.16.0
)

Expand All @@ -35,7 +37,6 @@ require (
github.com/rivo/uniseg v0.4.6 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/sashabaranov/go-openai v1.23.1 h1:b2IsEG9+BdJ3f6G3gGu9Lon2Mw/C0aYqME3YzwBHcls=
github.com/sashabaranov/go-openai v1.23.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
45 changes: 45 additions & 0 deletions ingest/llm/llm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package llm

import (
"context"
"fmt"

"github.com/sashabaranov/go-openai"
)

type Client interface {
CreateEmbeddings(ctx context.Context, input string) ([]float32, error)
}

const (
embeddingModelName = "text-embedding-3-small"
embeddingDimensions = 512
)

type openAI struct {
client *openai.Client
}

func NewOpenAIClient(authToken string) Client {
client := openai.NewClient(authToken)
return &openAI{
client: client,
}
}

func (o *openAI) CreateEmbeddings(ctx context.Context, input string) ([]float32, error) {
resp, err := o.client.CreateEmbeddings(ctx, openai.EmbeddingRequest{
Model: embeddingModelName,
Input: input,
Dimensions: 512,
EncodingFormat: openai.EmbeddingEncodingFormatFloat,
})
if err != nil {
return nil, err
}

if len(resp.Data) != 1 {
return nil, fmt.Errorf("expected 1 embedding, got %d", len(resp.Data))
}
return resp.Data[0].Embedding, nil
}

0 comments on commit 6cc9f2a

Please sign in to comment.