Skip to content

Commit

Permalink
openai add br encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Dec 10, 2024
1 parent 1dc7357 commit 9d9de6f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
49 changes: 49 additions & 0 deletions drivers/ai-provider/openAI/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package openAI

import (
"bytes"
"fmt"
"io"

"github.com/eolinker/eosc"

"github.com/andybalholm/brotli"
)

type IEncoder interface {
ToUTF8([]byte) ([]byte, error)
}

type EncoderManger struct {
encoders eosc.Untyped[string, IEncoder]
}

func NewEncoderManger() *EncoderManger {
return &EncoderManger{encoders: eosc.BuildUntyped[string, IEncoder]()}
}

func (e *EncoderManger) Set(name string, encoder IEncoder) {
e.encoders.Set(name, encoder)
}

func (e *EncoderManger) ToUTF8(name string, data []byte) ([]byte, error) {
encoder, ok := e.encoders.Get(name)
if !ok {
return nil, fmt.Errorf("encoder %s not found", name)
}
return encoder.ToUTF8(data)
}

var encoderManger = NewEncoderManger()

func init() {
encoderManger.Set("br", &Br{})
}

type Br struct {
}

func (b *Br) ToUTF8(data []byte) ([]byte, error) {
reader := brotli.NewReader(bytes.NewReader(data))
return io.ReadAll(reader)
}
8 changes: 8 additions & 0 deletions drivers/ai-provider/openAI/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (c *Chat) ResponseConvert(ctx eocontext.EoContext) error {
return nil
}
body := httpContext.Response().GetBody()
encoding := httpContext.Response().Headers().Get("content-encoding")
if encoding != "utf-8" && encoding != "" {
body, err = encoderManger.ToUTF8(encoding, body)
if err != nil {
return err
}
}
data := eosc.NewBase[Response]()
err = json.Unmarshal(body, data)
if err != nil {
Expand All @@ -102,6 +109,7 @@ func (c *Chat) ResponseConvert(ctx eocontext.EoContext) error {
if err != nil {
return err
}
httpContext.Response().SetHeader("content-encoding", "utf-8")
httpContext.Response().SetBody(body)
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ require (

require (
dubbo.apache.org/dubbo-go/v3 v3.0.2-0.20220519062747-f6405fa79d5c
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/brotli v1.0.5
github.com/apache/dubbo-go-hessian2 v1.11.6
github.com/armon/go-metrics v0.3.9 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down

0 comments on commit 9d9de6f

Please sign in to comment.