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: support replicate chat models #1989

Merged
merged 2 commits into from
Dec 21, 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
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
3 changes: 2 additions & 1 deletion common/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package render
import (
"encoding/json"
"fmt"
"strings"

"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"strings"
)

func StringData(c *gin.Context, str string) {
Expand Down
2 changes: 1 addition & 1 deletion monitor/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
strings.Contains(lowerMessage, "credit") ||
strings.Contains(lowerMessage, "balance") ||
strings.Contains(lowerMessage, "permission denied") ||
strings.Contains(lowerMessage, "organization has been restricted") || // groq
strings.Contains(lowerMessage, "organization has been restricted") || // groq

Check warning on line 37 in monitor/manage.go

View check run for this annotation

Codecov / codecov/patch

monitor/manage.go#L37

Added line #L37 was not covered by tests
strings.Contains(lowerMessage, "已欠费") {
return true
}
Expand Down
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
}
6 changes: 3 additions & 3 deletions relay/adaptor/ollama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
TopP: request.TopP,
FrequencyPenalty: request.FrequencyPenalty,
PresencePenalty: request.PresencePenalty,
NumPredict: request.MaxTokens,
NumCtx: request.NumCtx,
NumPredict: request.MaxTokens,
NumCtx: request.NumCtx,

Check warning on line 35 in relay/adaptor/ollama/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/ollama/main.go#L34-L35

Added lines #L34 - L35 were not covered by tests
},
Stream: request.Stream,
}
Expand Down Expand Up @@ -122,7 +122,7 @@
for scanner.Scan() {
data := scanner.Text()
if strings.HasPrefix(data, "}") {
data = strings.TrimPrefix(data, "}") + "}"
data = strings.TrimPrefix(data, "}") + "}"

Check warning on line 125 in relay/adaptor/ollama/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/ollama/main.go#L125

Added line #L125 was not covered by tests
}

var ollamaResponse ChatResponse
Expand Down
7 changes: 4 additions & 3 deletions relay/adaptor/openai/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import (
"fmt"
"strings"

"github.com/songquanpeng/one-api/relay/channeltype"
"github.com/songquanpeng/one-api/relay/model"
"strings"
)

func ResponseText2Usage(responseText string, modeName string, promptTokens int) *model.Usage {
func ResponseText2Usage(responseText string, modelName string, promptTokens int) *model.Usage {

Check warning on line 11 in relay/adaptor/openai/helper.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/openai/helper.go#L11

Added line #L11 was not covered by tests
usage := &model.Usage{}
usage.PromptTokens = promptTokens
usage.CompletionTokens = CountTokenText(responseText, modeName)
usage.CompletionTokens = CountTokenText(responseText, modelName)

Check warning on line 14 in relay/adaptor/openai/helper.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/openai/helper.go#L14

Added line #L14 was not covered by tests
usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
return usage
}
Expand Down
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
136 changes: 136 additions & 0 deletions relay/adaptor/replicate/adaptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package replicate

import (
"fmt"
"io"
"net/http"
"slices"
"strings"
"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 39 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L26-L39

Added lines #L26 - L39 were not covered by tests
}

func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
if !request.Stream {
// TODO: support non-stream mode
return nil, errors.Errorf("replicate models only support stream mode now, please set stream=true")
}

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#L42-L46

Added lines #L42 - L46 were not covered by tests

// Build the prompt from OpenAI messages
var promptBuilder strings.Builder
for _, message := range request.Messages {
switch msgCnt := message.Content.(type) {
case string:
promptBuilder.WriteString(message.Role)
promptBuilder.WriteString(": ")
promptBuilder.WriteString(msgCnt)
promptBuilder.WriteString("\n")
default:

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

View check run for this annotation

Codecov / codecov/patch

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

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

replicateRequest := ReplicateChatRequest{
Input: ChatInput{
Prompt: promptBuilder.String(),
MaxTokens: request.MaxTokens,
Temperature: 1.0,
TopP: 1.0,
PresencePenalty: 0.0,
FrequencyPenalty: 0.0,
},
}

// Map optional fields
if request.Temperature != nil {
replicateRequest.Input.Temperature = *request.Temperature
}
if request.TopP != nil {
replicateRequest.Input.TopP = *request.TopP
}
if request.PresencePenalty != nil {
replicateRequest.Input.PresencePenalty = *request.PresencePenalty
}
if request.FrequencyPenalty != nil {
replicateRequest.Input.FrequencyPenalty = *request.FrequencyPenalty
}
if request.MaxTokens > 0 {
replicateRequest.Input.MaxTokens = request.MaxTokens
} else if request.MaxTokens == 0 {
replicateRequest.Input.MaxTokens = 500
}

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L61-L89

Added lines #L61 - L89 were not covered by tests

return replicateRequest, nil

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L91

Added line #L91 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L94-L95

Added lines #L94 - L95 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 101 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L98-L101

Added lines #L98 - L101 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L103

Added line #L103 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 109 in relay/adaptor/replicate/adaptor.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L106-L109

Added lines #L106 - L109 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 request to replicate")
return adaptor.DoRequestHelper(a, c, meta, requestBody)

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L112-L114

Added lines #L112 - L114 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)
case relaymode.ChatCompletions:
err, usage = ChatHandler(c, resp)
default:
err = openai.ErrorWrapper(errors.New("not implemented"), "not_implemented", http.StatusInternalServerError)

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L117-L124

Added lines #L117 - L124 were not covered by tests
}

return

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L127

Added line #L127 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/replicate/adaptor.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}
Loading
Loading