Skip to content

Commit

Permalink
feat(backend): extra_body option
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Dec 31, 2024
1 parent 174908d commit 6fc4789
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pkg/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type OpenAISpeechRequestOptions struct {
// Select a value from 0.25 to 4.0.
// 1.0 is the default.
Speed int `json:"speed,omitempty"`

// Extension: allows you to add custom content to body.
ExtraBody map[string]interface{} `json:"extra_body,omitempty"`
}

type SpeechRequestOptions struct {
Expand Down
19 changes: 14 additions & 5 deletions pkg/backend/elevenlabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ func elevenlabs(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resu
patchedPayload := jsonpatch.ApplyPatches(
options.MustGet().body.OrElse(new(bytes.Buffer)).Bytes(),
mo.Some(jsonpatch.ApplyOptions{AllowMissingPathOnRemove: true}),
jsonpatch.NewRemove("/model"),
jsonpatch.NewRemove("/voice"),
jsonpatch.NewRemove("/input"),
jsonpatch.NewAdd("/text", options.MustGet().Input),
jsonpatch.NewAdd("/model_id", options.MustGet().Model),
append(
[]mo.Option[jsonpatch.JSONPatchOperationObject]{
jsonpatch.NewRemove("/model"),
jsonpatch.NewRemove("/voice"),
jsonpatch.NewRemove("/input"),
jsonpatch.NewAdd("/text", options.MustGet().Input),
jsonpatch.NewAdd("/model_id", options.MustGet().Model),
},
lo.Map(
lo.Entries(options.MustGet().ExtraBody),
func(item lo.Entry[string, interface{}], index int) mo.Option[jsonpatch.JSONPatchOperationObject] {
return jsonpatch.NewAdd(strings.Join([]string{"/", item.Key}, ""), item.Value)
})...,
)...,
)
if patchedPayload.IsError() {
return mo.Err[any](apierrors.NewErrInternal().WithDetail(patchedPayload.Error().Error()).WithCaller())
Expand Down
18 changes: 14 additions & 4 deletions pkg/backend/koemotion.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/moeru-ai/unspeech/pkg/apierrors"
"github.com/moeru-ai/unspeech/pkg/utils"
"github.com/moeru-ai/unspeech/pkg/utils/jsonpatch"
"github.com/samber/lo"
"github.com/samber/mo"
"github.com/vincent-petithory/dataurl"
)
Expand All @@ -20,10 +21,19 @@ func koemotion(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resul
patchedPayload := jsonpatch.ApplyPatches(
options.MustGet().body.OrElse(new(bytes.Buffer)).Bytes(),
mo.Some(jsonpatch.ApplyOptions{AllowMissingPathOnRemove: true}),
jsonpatch.NewRemove("/model"),
jsonpatch.NewRemove("/voice"),
jsonpatch.NewRemove("/input"),
jsonpatch.NewAdd("/text", options.MustGet().Input),
append(
[]mo.Option[jsonpatch.JSONPatchOperationObject]{
jsonpatch.NewRemove("/model"),
jsonpatch.NewRemove("/voice"),
jsonpatch.NewRemove("/input"),
jsonpatch.NewAdd("/text", options.MustGet().Input),
},
lo.Map(
lo.Entries(options.MustGet().ExtraBody),
func(item lo.Entry[string, interface{}], index int) mo.Option[jsonpatch.JSONPatchOperationObject] {
return jsonpatch.NewAdd(strings.Join([]string{"/", item.Key}, ""), item.Value)
})...,
)...,
)
if patchedPayload.IsError() {
return mo.Err[any](apierrors.NewErrInternal().WithDetail(patchedPayload.Error().Error()).WithCaller())
Expand Down

0 comments on commit 6fc4789

Please sign in to comment.