Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): extra_body option #12

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]any `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, any], 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, any], 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
Loading