Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
feat(openai): use go-openai client (#295)
Browse files Browse the repository at this point in the history
Because

- Originally, we used `httpclient` to connect directly to OpenAI. Since
we are introducing the streaming feature, using the `go-openai` SDK will
make the codebase easier to maintain.

This commit

- Switches to using the `go-openai` client.
  • Loading branch information
donch1989 authored Aug 22, 2024
1 parent e26ecef commit aa20a16
Show file tree
Hide file tree
Showing 20 changed files with 1,437 additions and 664 deletions.
7 changes: 4 additions & 3 deletions ai/fireworksai/v0/fireworks_client_interface_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions ai/groq/v0/groq_client_interface_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 5 additions & 53 deletions ai/openai/v0/audio_transcriptions.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
package openai

import (
"bytes"
"fmt"
"mime/multipart"

"github.com/instill-ai/component/internal/util"
)

const (
transcriptionsPath = "/v1/audio/transcriptions"
)

type AudioTranscriptionInput struct {
Audio string `json:"audio"`
Model string `json:"model"`
Prompt *string `json:"prompt,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
Language *string `json:"language,omitempty"`
}

type AudioTranscriptionReq struct {
File []byte `json:"file"`
Model string `json:"model"`
Prompt *string `json:"prompt,omitempty"`
Language *string `json:"language,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
}

type AudioTranscriptionResp struct {
Text string `json:"text"`
Duration float32 `json:"duration"`
}

func getBytes(req AudioTranscriptionReq) (*bytes.Reader, string, error) {
data := &bytes.Buffer{}
writer := multipart.NewWriter(data)
err := util.WriteFile(writer, "file", req.File)
if err != nil {
return nil, "", err
}
util.WriteField(writer, "model", req.Model)
util.WriteField(writer, "response_format", req.ResponseFormat)
if req.Prompt != nil {
util.WriteField(writer, "prompt", *req.Prompt)
}
if req.Language != nil {
util.WriteField(writer, "language", *req.Language)
}
if req.Temperature != nil {
util.WriteField(writer, "temperature", fmt.Sprintf("%f", *req.Temperature))
}
writer.Close()
return bytes.NewReader(data.Bytes()), writer.FormDataContentType(), nil
Audio string `json:"audio"`
Model string `json:"model"`
Prompt string `json:"prompt,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
Language string `json:"language,omitempty"`
}
39 changes: 6 additions & 33 deletions ai/openai/v0/client.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
package openai

import (
"github.com/instill-ai/component/internal/util/httpclient"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/structpb"
)

func newClient(setup *structpb.Struct, logger *zap.Logger) *httpclient.Client {
c := httpclient.New("OpenAI", getBasePath(setup),
httpclient.WithLogger(logger),
httpclient.WithEndUserError(new(errBody)),
)
openaiclient "github.com/sashabaranov/go-openai"
)

c.SetAuthToken(getAPIKey(setup))
func newClient(setup *structpb.Struct, logger *zap.Logger) *openaiclient.Client {

cfg := openaiclient.DefaultConfig(getAPIKey(setup))
org := getOrg(setup)
if org != "" {
c.SetHeader("OpenAI-Organization", org)
cfg.OrgID = org
}

return c
}

type errBody struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}

func (e errBody) Message() string {
return e.Error.Message
}

// getBasePath returns OpenAI's API URL. This configuration param allows us to
// override the API the connector will point to. It isn't meant to be exposed
// to users. Rather, it can serve to test the logic against a fake server.
// TODO instead of having the API value hardcoded in the codebase, it should be
// read from a setup file or environment variable.
func getBasePath(setup *structpb.Struct) string {
v, ok := setup.GetFields()["base-path"]
if !ok {
return host
}
return v.GetStringValue()
return openaiclient.NewClientWithConfig(cfg)
}

func getAPIKey(setup *structpb.Struct) string {
Expand Down
Loading

0 comments on commit aa20a16

Please sign in to comment.