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

feature: 支持 replicate 的绘图 #1954

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: CI

# This setup assumes that you run the unit tests with code coverage in the same
# workflow that will also print the coverage report as comment to the pull request.
# workflow that will also print the coverage report as comment to the pull request.
# Therefore, you need to trigger this workflow when a pull request is (re)opened or
# when new code is pushed to the branch of the pull request. In addition, you also
# need to trigger this workflow when new code is pushed to the main branch because
# need to trigger this workflow when new code is pushed to the main branch because
# we need to upload the code coverage results as artifact for the main branch as
# well since it will be the baseline code coverage.
#
#
# We do not want to trigger the workflow for pushes to *any* branch because this
# would trigger our jobs twice on pull requests (once from "push" event and once
# from "pull_request->synchronize")
Expand All @@ -31,7 +31,7 @@ jobs:
with:
go-version: ^1.22

# When you execute your unit tests, make sure to use the "-coverprofile" flag to write a
# When you execute your unit tests, make sure to use the "-coverprofile" flag to write a
# coverage profile to a file. You will need the name of the file (e.g. "coverage.txt")
# in the next step as well as the next job.
- name: Test
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ logs
data
/web/node_modules
cmd.md
.env
.env
/one-api
Binary file removed one-api
Binary file not shown.
3 changes: 3 additions & 0 deletions relay/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/songquanpeng/one-api/relay/adaptor/palm"
"github.com/songquanpeng/one-api/relay/adaptor/proxy"
"github.com/songquanpeng/one-api/relay/adaptor/replicate"
"github.com/songquanpeng/one-api/relay/adaptor/tencent"
"github.com/songquanpeng/one-api/relay/adaptor/vertexai"
"github.com/songquanpeng/one-api/relay/adaptor/xunfei"
Expand Down Expand Up @@ -61,6 +62,8 @@ func GetAdaptor(apiType int) adaptor.Adaptor {
return &vertexai.Adaptor{}
case apitype.Proxy:
return &proxy.Adaptor{}
case apitype.Replicate:
return &replicate.Adaptor{}
}
return nil
}
10 changes: 9 additions & 1 deletion relay/adaptor/openai/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package openai

import "github.com/songquanpeng/one-api/relay/model"
import (
"context"
"fmt"

"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/relay/model"
)

func ErrorWrapper(err error, code string, statusCode int) *model.ErrorWithStatusCode {
logger.Error(context.TODO(), fmt.Sprintf("[%s]%+v", code, err))

Check warning on line 13 in relay/adaptor/openai/util.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/openai/util.go#L12-L13

Added lines #L12 - L13 were not covered by tests
Error := model.Error{
Message: err.Error(),
Type: "one_api_error",
Expand Down
85 changes: 85 additions & 0 deletions relay/adaptor/replicate/adaptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package replicate

import (
"fmt"
"io"
"net/http"
"slices"
"time"

"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/songquanpeng/one-api/common/logger"
"github.com/songquanpeng/one-api/relay/adaptor"
"github.com/songquanpeng/one-api/relay/adaptor/openai"
"github.com/songquanpeng/one-api/relay/meta"
"github.com/songquanpeng/one-api/relay/model"
"github.com/songquanpeng/one-api/relay/relaymode"
)

type Adaptor struct {
meta *meta.Meta
}

// ConvertImageRequest implements adaptor.Adaptor.
func (*Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
return DrawImageRequest{
Input: ImageInput{
Steps: 25,
Prompt: request.Prompt,
Guidance: 3,
Seed: int(time.Now().UnixNano()),
SafetyTolerance: 5,
NImages: 1, // replicate will always return 1 image
Width: 1440,
Height: 1440,
AspectRatio: "1:1",
},
}, nil

Check warning on line 38 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L25-L38

Added lines #L25 - L38 were not covered by tests
}

func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
return nil, errors.New("not implemented")

Check warning on line 42 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

func (a *Adaptor) Init(meta *meta.Meta) {
a.meta = meta

Check warning on line 46 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}

func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
if !slices.Contains(ModelList, meta.OriginModelName) {
return "", errors.Errorf("model %s not supported", meta.OriginModelName)
}

Check warning on line 52 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L49-L52

Added lines #L49 - L52 were not covered by tests

return fmt.Sprintf("https://api.replicate.com/v1/models/%s/predictions", meta.OriginModelName), nil

Check warning on line 54 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L54

Added line #L54 was not covered by tests
}

func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
adaptor.SetupCommonRequestHeader(c, req, meta)
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
return nil

Check warning on line 60 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L57-L60

Added lines #L57 - L60 were not covered by tests
}

func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
logger.Info(c, "send image request to replicate")
return adaptor.DoRequestHelper(a, c, meta, requestBody)

Check warning on line 65 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}

func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
switch meta.Mode {
case relaymode.ImagesGenerations:
err, usage = ImageHandler(c, resp)
default:
err = openai.ErrorWrapper(errors.New("not implemented"), "not_implemented", http.StatusInternalServerError)

Check warning on line 73 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L68-L73

Added lines #L68 - L73 were not covered by tests
}

return

Check warning on line 76 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L76

Added line #L76 was not covered by tests
}

func (a *Adaptor) GetModelList() []string {
return ModelList

Check warning on line 80 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L79-L80

Added lines #L79 - L80 were not covered by tests
}

func (a *Adaptor) GetChannelName() string {
return "replicate"

Check warning on line 84 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L83-L84

Added lines #L83 - L84 were not covered by tests
}
58 changes: 58 additions & 0 deletions relay/adaptor/replicate/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package replicate

// ModelList is a list of models that can be used with Replicate.
//
// https://replicate.com/pricing
var ModelList = []string{
// -------------------------------------
// image model
// -------------------------------------
"black-forest-labs/flux-1.1-pro",
"black-forest-labs/flux-1.1-pro-ultra",
"black-forest-labs/flux-canny-dev",
"black-forest-labs/flux-canny-pro",
"black-forest-labs/flux-depth-dev",
"black-forest-labs/flux-depth-pro",
"black-forest-labs/flux-dev",
"black-forest-labs/flux-dev-lora",
"black-forest-labs/flux-fill-dev",
"black-forest-labs/flux-fill-pro",
"black-forest-labs/flux-pro",
"black-forest-labs/flux-redux-dev",
"black-forest-labs/flux-redux-schnell",
"black-forest-labs/flux-schnell",
"black-forest-labs/flux-schnell-lora",
"ideogram-ai/ideogram-v2",
"ideogram-ai/ideogram-v2-turbo",
"recraft-ai/recraft-v3",
"recraft-ai/recraft-v3-svg",
"stability-ai/stable-diffusion-3",
"stability-ai/stable-diffusion-3.5-large",
"stability-ai/stable-diffusion-3.5-large-turbo",
"stability-ai/stable-diffusion-3.5-medium",
// -------------------------------------
// language model
// -------------------------------------
// "ibm-granite/granite-20b-code-instruct-8k", // TODO: implement the adaptor
// "ibm-granite/granite-3.0-2b-instruct", // TODO: implement the adaptor
// "ibm-granite/granite-3.0-8b-instruct", // TODO: implement the adaptor
// "ibm-granite/granite-8b-code-instruct-128k", // TODO: implement the adaptor
// "meta/llama-2-13b", // TODO: implement the adaptor
// "meta/llama-2-13b-chat", // TODO: implement the adaptor
// "meta/llama-2-70b", // TODO: implement the adaptor
// "meta/llama-2-70b-chat", // TODO: implement the adaptor
// "meta/llama-2-7b", // TODO: implement the adaptor
// "meta/llama-2-7b-chat", // TODO: implement the adaptor
// "meta/meta-llama-3.1-405b-instruct", // TODO: implement the adaptor
// "meta/meta-llama-3-70b", // TODO: implement the adaptor
// "meta/meta-llama-3-70b-instruct", // TODO: implement the adaptor
// "meta/meta-llama-3-8b", // TODO: implement the adaptor
// "meta/meta-llama-3-8b-instruct", // TODO: implement the adaptor
// "mistralai/mistral-7b-instruct-v0.2", // TODO: implement the adaptor
// "mistralai/mistral-7b-v0.1", // TODO: implement the adaptor
// "mistralai/mixtral-8x7b-instruct-v0.1", // TODO: implement the adaptor
// -------------------------------------
// video model
// -------------------------------------
// "minimax/video-01", // TODO: implement the adaptor
}
Loading