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

chore: use jsonpatch to inherit parameters from user side #11

Merged
merged 1 commit into from
Dec 30, 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
32 changes: 16 additions & 16 deletions pkg/backend/elevenlabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@ package backend

import (
"bytes"
"encoding/json"
"log/slog"
"net/http"
"net/url"
"strings"

"github.com/labstack/echo/v4"
"github.com/moeru-ai/unspeech/pkg/apierrors"
"github.com/moeru-ai/unspeech/pkg/utils/jsonpatch"
"github.com/samber/lo"
"github.com/samber/mo"
)

// https://elevenlabs.io/docs/api-reference/text-to-speech/convert#request
type ElevenLabsOptions struct {
Text string `json:"text"`
ModelID string `json:"model_id,omitempty"`
// TODO: support other options
}

func elevenlabs(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Result[any] {
reqURL := lo.Must(url.Parse("https://api.elevenlabs.io/v1/text-to-speech")).
JoinPath(options.MustGet().Voice).
String()

values := ElevenLabsOptions{
Text: options.MustGet().Input,
ModelID: options.MustGet().Model,
// https://elevenlabs.io/docs/api-reference/text-to-speech/convert#request
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),
)
if patchedPayload.IsError() {
return mo.Err[any](apierrors.NewErrInternal().WithDetail(patchedPayload.Error().Error()).WithCaller())
}

payload := lo.Must(json.Marshal(values))

req, err := http.NewRequestWithContext(
c.Request().Context(),
http.MethodPost,
reqURL,
bytes.NewBuffer(payload),
bytes.NewBuffer(patchedPayload.MustGet()),
)
if err != nil {
return mo.Err[any](apierrors.NewErrInternal().WithCaller())
Expand Down Expand Up @@ -71,8 +71,8 @@ func elevenlabs(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resu
default:
slog.Warn("unknown upstream error with unknown Content-Type",
slog.Int("status", res.StatusCode),
slog.String("content-type", res.Header.Get("Content-Type")),
slog.String("content-length", res.Header.Get("Content-Length")),
slog.String("content_type", res.Header.Get("Content-Type")),
slog.String("content_length", res.Header.Get("Content-Length")),
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/backend/koemotion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

func koemotion(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Result[any] {
// https://developers.rinna.co.jp/api-details#api=koemotion&operation=infer
patchedPayload := jsonpatch.ApplyPatches(
options.MustGet().body.OrElse(new(bytes.Buffer)).Bytes(),
mo.Some(jsonpatch.ApplyOptions{AllowMissingPathOnRemove: true}),
Expand Down Expand Up @@ -65,8 +66,8 @@ func koemotion(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Resul
default:
slog.Warn("unknown upstream error with unknown Content-Type",
slog.Int("status", res.StatusCode),
slog.String("content-type", res.Header.Get("Content-Type")),
slog.String("content-length", res.Header.Get("Content-Length")),
slog.String("content_type", res.Header.Get("Content-Type")),
slog.String("content_length", res.Header.Get("Content-Length")),
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/backend/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func openai(c echo.Context, options mo.Option[SpeechRequestOptions]) mo.Result[a
default:
slog.Warn("unknown upstream error with unknown Content-Type",
slog.Int("status", res.StatusCode),
slog.String("content-type", res.Header.Get("Content-Type")),
slog.String("content-length", res.Header.Get("Content-Length")),
slog.String("content_type", res.Header.Get("Content-Type")),
slog.String("content_length", res.Header.Get("Content-Length")),
)
}
}
Expand Down
Loading