mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-19 22:53:41 +08:00
Compare commits
1 Commits
008ffe4662
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
929c2438b6 |
13
Dockerfile
13
Dockerfile
@@ -4,10 +4,9 @@ WORKDIR /web
|
||||
COPY ./VERSION .
|
||||
COPY ./web .
|
||||
|
||||
RUN npm config set registry https://registry.npmmirror.com && \
|
||||
npm install --prefix /web/default --legacy-peer-deps --retry 5 & \
|
||||
npm install --prefix /web/berry --legacy-peer-deps --retry 5 & \
|
||||
npm install --prefix /web/air --legacy-peer-deps --retry 5 & \
|
||||
RUN npm install --prefix /web/default & \
|
||||
npm install --prefix /web/berry & \
|
||||
npm install --prefix /web/air & \
|
||||
wait
|
||||
|
||||
RUN DISABLE_ESLINT_PLUGIN='true' REACT_APP_VERSION=$(cat ./VERSION) npm run build --prefix /web/default & \
|
||||
@@ -25,14 +24,12 @@ RUN apk add --no-cache \
|
||||
|
||||
ENV GO111MODULE=on \
|
||||
CGO_ENABLED=1 \
|
||||
GOOS=linux \
|
||||
GOPROXY=https://goproxy.cn,direct \
|
||||
GOSUMDB=off
|
||||
GOOS=linux
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
ADD go.mod go.sum ./
|
||||
RUN go mod download -x
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
COPY --from=builder /web/build ./web/build
|
||||
|
||||
@@ -36,8 +36,6 @@ func relayHelper(c *gin.Context, relayMode int) *model.ErrorWithStatusCode {
|
||||
err = controller.RelayAudioHelper(c, relayMode)
|
||||
case relaymode.Proxy:
|
||||
err = controller.RelayProxyHelper(c, relayMode)
|
||||
case relaymode.AnthropicMessages:
|
||||
err = controller.RelayAnthropicHelper(c)
|
||||
default:
|
||||
err = controller.RelayTextHelper(c)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
version: '3.4'
|
||||
|
||||
services:
|
||||
one-api:
|
||||
# image: "${REGISTRY:-docker.io}/justsong/one-api:latest"
|
||||
build: .
|
||||
image: "${REGISTRY:-docker.io}/justsong/one-api:latest"
|
||||
container_name: one-api
|
||||
platform: linux/amd64
|
||||
restart: always
|
||||
command: --log-dir /app/logs
|
||||
ports:
|
||||
|
||||
4
go.mod
4
go.mod
@@ -1,6 +1,6 @@
|
||||
module github.com/songquanpeng/one-api
|
||||
|
||||
go 1.20
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
cloud.google.com/go/iam v1.1.10
|
||||
@@ -98,7 +98,7 @@ require (
|
||||
go.opentelemetry.io/otel/trace v1.24.0 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/net v0.26.0 // indirect
|
||||
golang.org/x/oauth2 v0.21.0 // indirect
|
||||
golang.org/x/oauth2 v0.27.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
|
||||
15
go.sum
15
go.sum
@@ -73,6 +73,7 @@ github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
@@ -111,8 +112,10 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o=
|
||||
github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw=
|
||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
@@ -157,6 +160,7 @@ github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuV
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
@@ -171,8 +175,11 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
|
||||
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
|
||||
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
|
||||
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
|
||||
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
@@ -185,6 +192,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
|
||||
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
|
||||
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
|
||||
@@ -239,8 +247,8 @@ golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwY
|
||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
|
||||
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
|
||||
golang.org/x/oauth2 v0.27.0 h1:da9Vo7/tDv5RH/7nZDz1eMGS/q1Vv1N/7FCrBhI9I3M=
|
||||
golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -297,8 +305,11 @@ google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6h
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/songquanpeng/one-api/relay/adaptor"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
)
|
||||
|
||||
type Adaptor struct {
|
||||
@@ -22,15 +21,7 @@ func (a *Adaptor) Init(meta *meta.Meta) {
|
||||
}
|
||||
|
||||
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
||||
// For native Anthropic API
|
||||
if strings.Contains(meta.BaseURL, "api.anthropic.com") {
|
||||
return fmt.Sprintf("%s/v1/messages", meta.BaseURL), nil
|
||||
}
|
||||
|
||||
// For third-party providers supporting Anthropic protocol (like DeepSeek)
|
||||
// They typically expose the endpoint at /anthropic/v1/messages
|
||||
baseURL := strings.TrimSuffix(meta.BaseURL, "/")
|
||||
return fmt.Sprintf("%s/anthropic/v1/messages", baseURL), nil
|
||||
return fmt.Sprintf("%s/v1/messages", meta.BaseURL), nil
|
||||
}
|
||||
|
||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
||||
@@ -56,15 +47,6 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
|
||||
if request == nil {
|
||||
return nil, errors.New("request is nil")
|
||||
}
|
||||
|
||||
// For native Anthropic protocol requests, return the request as-is (no conversion needed)
|
||||
if relayMode == relaymode.AnthropicMessages {
|
||||
// The request should already be in Anthropic format, so we pass it through
|
||||
// This will be handled by the caller which already has the anthropic request
|
||||
return request, nil
|
||||
}
|
||||
|
||||
// For OpenAI to Anthropic conversion (existing functionality)
|
||||
return ConvertRequest(*request), nil
|
||||
}
|
||||
|
||||
@@ -80,17 +62,6 @@ func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Read
|
||||
}
|
||||
|
||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
||||
// For native Anthropic protocol requests, handle response directly without conversion
|
||||
if meta.Mode == relaymode.AnthropicMessages {
|
||||
if meta.IsStream {
|
||||
err, usage = DirectStreamHandler(c, resp)
|
||||
} else {
|
||||
err, usage = DirectHandler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// For OpenAI to Anthropic conversion (existing functionality)
|
||||
if meta.IsStream {
|
||||
err, usage = StreamHandler(c, resp)
|
||||
} else {
|
||||
|
||||
@@ -4,12 +4,11 @@ import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/songquanpeng/one-api/common/render"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/songquanpeng/one-api/common/render"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/helper"
|
||||
@@ -90,12 +89,8 @@ func ConvertRequest(textRequest model.GeneralOpenAIRequest) *Request {
|
||||
claudeRequest.Model = "claude-2.1"
|
||||
}
|
||||
for _, message := range textRequest.Messages {
|
||||
if message.Role == "system" && claudeRequest.System.IsEmpty() {
|
||||
// Create a SystemPrompt from the string content
|
||||
systemPrompt := SystemPrompt{}
|
||||
systemData := []byte(`"` + message.StringContent() + `"`) // Wrap in JSON string quotes
|
||||
_ = systemPrompt.UnmarshalJSON(systemData)
|
||||
claudeRequest.System = systemPrompt
|
||||
if message.Role == "system" && claudeRequest.System == "" {
|
||||
claudeRequest.System = message.StringContent()
|
||||
continue
|
||||
}
|
||||
claudeMessage := Message{
|
||||
@@ -382,131 +377,3 @@ func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName st
|
||||
_, err = c.Writer.Write(jsonResponse)
|
||||
return nil, &usage
|
||||
}
|
||||
|
||||
// DirectHandler handles native Anthropic API responses without conversion to OpenAI format
|
||||
func DirectHandler(c *gin.Context, resp *http.Response, promptTokens int, modelName string) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Infof(ctx, "=== DirectHandler Start ===")
|
||||
logger.Infof(ctx, "Response status: %d", resp.StatusCode)
|
||||
logger.Infof(ctx, "Response headers: %+v", resp.Header)
|
||||
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to read response body: %s", err.Error())
|
||||
return openai.ErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
err = resp.Body.Close()
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to close response body: %s", err.Error())
|
||||
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Raw response body: %s", string(responseBody))
|
||||
|
||||
var claudeResponse Response
|
||||
err = json.Unmarshal(responseBody, &claudeResponse)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to unmarshal response: %s", err.Error())
|
||||
// If we can't parse as Anthropic response, maybe it's an error response
|
||||
// Let's try to write it directly and see what happens
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
c.Writer.WriteHeader(resp.StatusCode)
|
||||
_, writeErr := c.Writer.Write(responseBody)
|
||||
if writeErr != nil {
|
||||
logger.Errorf(ctx, "Failed to write raw response: %s", writeErr.Error())
|
||||
return openai.ErrorWrapper(writeErr, "write_response_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
// Return a minimal usage for tracking
|
||||
usage := &model.Usage{PromptTokens: promptTokens, CompletionTokens: 0, TotalTokens: promptTokens}
|
||||
return nil, usage
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Parsed response - ID: %s, Model: %s, Usage: %+v",
|
||||
claudeResponse.Id, claudeResponse.Model, claudeResponse.Usage)
|
||||
|
||||
if claudeResponse.Error.Type != "" {
|
||||
logger.Errorf(ctx, "Anthropic API error: %s - %s", claudeResponse.Error.Type, claudeResponse.Error.Message)
|
||||
return &model.ErrorWithStatusCode{
|
||||
Error: model.Error{
|
||||
Message: claudeResponse.Error.Message,
|
||||
Type: claudeResponse.Error.Type,
|
||||
Param: "",
|
||||
Code: claudeResponse.Error.Type,
|
||||
},
|
||||
StatusCode: resp.StatusCode,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// For direct mode, return the response as-is without conversion
|
||||
usage := model.Usage{
|
||||
PromptTokens: claudeResponse.Usage.InputTokens,
|
||||
CompletionTokens: claudeResponse.Usage.OutputTokens,
|
||||
TotalTokens: claudeResponse.Usage.InputTokens + claudeResponse.Usage.OutputTokens,
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Usage calculated: %+v", usage)
|
||||
|
||||
// Write the original Anthropic response directly
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
c.Writer.WriteHeader(resp.StatusCode)
|
||||
_, err = c.Writer.Write(responseBody)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to write response: %s", err.Error())
|
||||
return openai.ErrorWrapper(err, "write_response_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Response written successfully")
|
||||
logger.Infof(ctx, "=== DirectHandler End ===")
|
||||
return nil, &usage
|
||||
}
|
||||
|
||||
// DirectStreamHandler handles native Anthropic API streaming responses without conversion
|
||||
func DirectStreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusCode, *model.Usage) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Set headers for streaming
|
||||
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||
c.Writer.Header().Set("Cache-Control", "no-cache")
|
||||
c.Writer.Header().Set("Connection", "keep-alive")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
c.Writer.WriteHeader(resp.StatusCode)
|
||||
|
||||
// Stream the response directly without conversion
|
||||
var usage model.Usage
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
for scanner.Scan() {
|
||||
data := scanner.Text()
|
||||
if len(data) < 6 || !strings.HasPrefix(data, "data:") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Parse usage information if available
|
||||
if strings.Contains(data, "\"usage\":") {
|
||||
var eventData map[string]interface{}
|
||||
jsonData := strings.TrimPrefix(data, "data:")
|
||||
jsonData = strings.TrimSpace(jsonData)
|
||||
if err := json.Unmarshal([]byte(jsonData), &eventData); err == nil {
|
||||
if usageData, ok := eventData["usage"].(map[string]interface{}); ok {
|
||||
if inputTokens, ok := usageData["input_tokens"].(float64); ok {
|
||||
usage.PromptTokens = int(inputTokens)
|
||||
}
|
||||
if outputTokens, ok := usageData["output_tokens"].(float64); ok {
|
||||
usage.CompletionTokens = int(outputTokens)
|
||||
usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write data directly to the response
|
||||
c.Writer.WriteString(data + "\n")
|
||||
c.Writer.Flush()
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return openai.ErrorWrapper(err, "stream_read_failed", http.StatusInternalServerError), nil
|
||||
}
|
||||
|
||||
return nil, &usage
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
package anthropic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// https://docs.anthropic.com/claude/reference/messages_post
|
||||
|
||||
type Metadata struct {
|
||||
@@ -46,92 +41,18 @@ type InputSchema struct {
|
||||
Required any `json:"required,omitempty"`
|
||||
}
|
||||
|
||||
// SystemPrompt can handle both string and array formats for the system field
|
||||
type SystemPrompt struct {
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler to handle both string and array formats
|
||||
func (s *SystemPrompt) UnmarshalJSON(data []byte) error {
|
||||
// Try to unmarshal as string first
|
||||
var str string
|
||||
if err := json.Unmarshal(data, &str); err == nil {
|
||||
s.value = str
|
||||
return nil
|
||||
}
|
||||
|
||||
// If that fails, try to unmarshal as array
|
||||
var arr []interface{}
|
||||
if err := json.Unmarshal(data, &arr); err == nil {
|
||||
s.value = arr
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("system field must be either a string or an array")
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler
|
||||
func (s SystemPrompt) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(s.value)
|
||||
}
|
||||
|
||||
// String returns the system prompt as a string
|
||||
func (s SystemPrompt) String() string {
|
||||
if s.value == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
switch v := s.value.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []interface{}:
|
||||
// Convert array to string by concatenating text content
|
||||
var result string
|
||||
for _, item := range v {
|
||||
if itemMap, ok := item.(map[string]interface{}); ok {
|
||||
if text, exists := itemMap["text"]; exists {
|
||||
if textStr, ok := text.(string); ok {
|
||||
result += textStr + " "
|
||||
}
|
||||
}
|
||||
} else if str, ok := item.(string); ok {
|
||||
result += str + " "
|
||||
}
|
||||
}
|
||||
return result
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// IsEmpty returns true if the system prompt is empty
|
||||
func (s SystemPrompt) IsEmpty() bool {
|
||||
if s.value == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
switch v := s.value.(type) {
|
||||
case string:
|
||||
return v == ""
|
||||
case []interface{}:
|
||||
return len(v) == 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
System SystemPrompt `json:"system,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
StopSequences []string `json:"stop_sequences,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
TopP *float64 `json:"top_p,omitempty"`
|
||||
TopK int `json:"top_k,omitempty"`
|
||||
Tools []Tool `json:"tools,omitempty"`
|
||||
ToolChoice any `json:"tool_choice,omitempty"`
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
System string `json:"system,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
StopSequences []string `json:"stop_sequences,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
TopP *float64 `json:"top_p,omitempty"`
|
||||
TopK int `json:"top_k,omitempty"`
|
||||
Tools []Tool `json:"tools,omitempty"`
|
||||
ToolChoice any `json:"tool_choice,omitempty"`
|
||||
//Metadata `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.G
|
||||
AnthropicVersion: anthropicVersion,
|
||||
// Model: claudeReq.Model,
|
||||
Messages: claudeReq.Messages,
|
||||
System: claudeReq.System.String(), // Convert SystemPrompt to string
|
||||
System: claudeReq.System,
|
||||
MaxTokens: claudeReq.MaxTokens,
|
||||
Temperature: claudeReq.Temperature,
|
||||
TopP: claudeReq.TopP,
|
||||
|
||||
@@ -1,224 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
dbmodel "github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/relay"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/anthropic"
|
||||
"github.com/songquanpeng/one-api/relay/adaptor/openai"
|
||||
"github.com/songquanpeng/one-api/relay/billing"
|
||||
billingratio "github.com/songquanpeng/one-api/relay/billing/ratio"
|
||||
"github.com/songquanpeng/one-api/relay/meta"
|
||||
"github.com/songquanpeng/one-api/relay/model"
|
||||
)
|
||||
|
||||
// RelayAnthropicHelper handles native Anthropic API requests (anthropic -> anthropic passthrough)
|
||||
func RelayAnthropicHelper(c *gin.Context) *model.ErrorWithStatusCode {
|
||||
ctx := c.Request.Context()
|
||||
meta := meta.GetByContext(c)
|
||||
|
||||
logger.Infof(ctx, "=== Anthropic Request Start ===")
|
||||
logger.Infof(ctx, "Request URL: %s", c.Request.URL.String())
|
||||
logger.Infof(ctx, "Request Method: %s", c.Request.Method)
|
||||
logger.Infof(ctx, "Request Headers: %+v", c.Request.Header)
|
||||
|
||||
// get & validate anthropic request
|
||||
anthropicRequest, err := getAndValidateAnthropicRequest(c)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "getAndValidateAnthropicRequest failed: %s", err.Error())
|
||||
return openai.ErrorWrapper(err, "invalid_anthropic_request", http.StatusBadRequest)
|
||||
}
|
||||
logger.Infof(ctx, "Parsed anthropic request - Model: %s, Stream: %v, Messages: %d",
|
||||
anthropicRequest.Model, anthropicRequest.Stream, len(anthropicRequest.Messages))
|
||||
meta.IsStream = anthropicRequest.Stream
|
||||
|
||||
// map model name
|
||||
meta.OriginModelName = anthropicRequest.Model
|
||||
mappedModel, _ := getMappedModelName(anthropicRequest.Model, meta.ModelMapping)
|
||||
anthropicRequest.Model = mappedModel
|
||||
meta.ActualModelName = anthropicRequest.Model
|
||||
|
||||
// estimate token usage for anthropic request
|
||||
promptTokens := estimateAnthropicTokens(anthropicRequest)
|
||||
meta.PromptTokens = promptTokens
|
||||
|
||||
// get model ratio & group ratio
|
||||
modelRatio := billingratio.GetModelRatio(anthropicRequest.Model, meta.ChannelType)
|
||||
groupRatio := billingratio.GetGroupRatio(meta.Group)
|
||||
ratio := modelRatio * groupRatio
|
||||
|
||||
// pre-consume quota
|
||||
preConsumedQuota, bizErr := preConsumeQuotaForAnthropic(ctx, anthropicRequest, promptTokens, ratio, meta)
|
||||
if bizErr != nil {
|
||||
logger.Warnf(ctx, "preConsumeQuota failed: %+v", *bizErr)
|
||||
return bizErr
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Meta info - APIType: %d, ChannelType: %d, BaseURL: %s", meta.APIType, meta.ChannelType, meta.BaseURL)
|
||||
|
||||
adaptor := relay.GetAdaptor(meta.APIType)
|
||||
if adaptor == nil {
|
||||
logger.Errorf(ctx, "Failed to get adaptor for API type: %d", meta.APIType)
|
||||
return openai.ErrorWrapper(fmt.Errorf("invalid api type: %d", meta.APIType), "invalid_api_type", http.StatusBadRequest)
|
||||
}
|
||||
logger.Infof(ctx, "Using adaptor: %s", adaptor.GetChannelName())
|
||||
adaptor.Init(meta)
|
||||
|
||||
// get request body - for anthropic passthrough, we directly use the request body
|
||||
requestBody, err := getAnthropicRequestBody(c, anthropicRequest)
|
||||
if err != nil {
|
||||
return openai.ErrorWrapper(err, "convert_anthropic_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// do request
|
||||
logger.Infof(ctx, "Sending request to upstream...")
|
||||
resp, err := adaptor.DoRequest(c, meta, requestBody)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "DoRequest failed: %s", err.Error())
|
||||
return openai.ErrorWrapper(err, "do_request_failed", http.StatusInternalServerError)
|
||||
}
|
||||
logger.Infof(ctx, "Received response - Status: %d, Headers: %+v", resp.StatusCode, resp.Header)
|
||||
|
||||
if isErrorHappened(meta, resp) {
|
||||
logger.Errorf(ctx, "Error detected in response")
|
||||
billing.ReturnPreConsumedQuota(ctx, preConsumedQuota, meta.TokenId)
|
||||
return RelayErrorHandler(resp)
|
||||
}
|
||||
|
||||
// do response - for anthropic native requests, we need to handle the response directly
|
||||
logger.Infof(ctx, "Processing anthropic response...")
|
||||
usage, respErr := handleAnthropicResponse(c, resp, meta)
|
||||
if respErr != nil {
|
||||
logger.Errorf(ctx, "respErr is not nil: %+v", respErr)
|
||||
billing.ReturnPreConsumedQuota(ctx, preConsumedQuota, meta.TokenId)
|
||||
return respErr
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Response processed successfully - Usage: %+v", usage)
|
||||
logger.Infof(ctx, "=== Anthropic Request End ===")
|
||||
|
||||
// post-consume quota - for anthropic, we create a placeholder GeneralOpenAIRequest
|
||||
placeholderRequest := &model.GeneralOpenAIRequest{
|
||||
Model: anthropicRequest.Model,
|
||||
}
|
||||
go postConsumeQuota(ctx, usage, meta, placeholderRequest, ratio, preConsumedQuota, modelRatio, groupRatio, false)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAndValidateAnthropicRequest(c *gin.Context) (*anthropic.Request, error) {
|
||||
anthropicRequest := &anthropic.Request{}
|
||||
err := common.UnmarshalBodyReusable(c, anthropicRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Basic validation
|
||||
if anthropicRequest.Model == "" {
|
||||
return nil, fmt.Errorf("model is required")
|
||||
}
|
||||
if len(anthropicRequest.Messages) == 0 {
|
||||
return nil, fmt.Errorf("messages are required")
|
||||
}
|
||||
if anthropicRequest.MaxTokens == 0 {
|
||||
anthropicRequest.MaxTokens = 4096 // default max tokens
|
||||
}
|
||||
|
||||
return anthropicRequest, nil
|
||||
}
|
||||
|
||||
func getAnthropicRequestBody(c *gin.Context, anthropicRequest *anthropic.Request) (io.Reader, error) {
|
||||
// For anthropic native requests, we marshal the request back to JSON
|
||||
jsonData, err := json.Marshal(anthropicRequest)
|
||||
if err != nil {
|
||||
logger.Debugf(c.Request.Context(), "anthropic request json_marshal_failed: %s\n", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
logger.Debugf(c.Request.Context(), "anthropic request: \n%s", string(jsonData))
|
||||
return bytes.NewBuffer(jsonData), nil
|
||||
}
|
||||
|
||||
func estimateAnthropicTokens(request *anthropic.Request) int {
|
||||
// Simple token estimation for Anthropic requests
|
||||
// This is a rough estimation, actual implementation might need more sophisticated logic
|
||||
totalTokens := 0
|
||||
|
||||
// Count tokens in system prompt
|
||||
if !request.System.IsEmpty() {
|
||||
systemText := request.System.String()
|
||||
totalTokens += len(systemText) / 4 // rough estimate: 1 token per 4 characters
|
||||
}
|
||||
|
||||
// Count tokens in messages
|
||||
for _, message := range request.Messages {
|
||||
for _, content := range message.Content {
|
||||
if content.Type == "text" {
|
||||
totalTokens += len(content.Text) / 4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return totalTokens
|
||||
}
|
||||
|
||||
func handleAnthropicResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (*model.Usage, *model.ErrorWithStatusCode) {
|
||||
// For anthropic native requests, use direct handlers to maintain Anthropic format
|
||||
if meta.IsStream {
|
||||
// Handle streaming response - note: DirectStreamHandler returns (error, usage)
|
||||
err, usage := anthropic.DirectStreamHandler(c, resp)
|
||||
return usage, err
|
||||
} else {
|
||||
// Handle non-streaming response - note: DirectHandler returns (error, usage)
|
||||
err, usage := anthropic.DirectHandler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
||||
return usage, err
|
||||
}
|
||||
}
|
||||
|
||||
func preConsumeQuotaForAnthropic(ctx context.Context, request *anthropic.Request, promptTokens int, ratio float64, meta *meta.Meta) (int64, *model.ErrorWithStatusCode) {
|
||||
// Use the same quota logic as text requests but adapted for Anthropic
|
||||
preConsumedTokens := config.PreConsumedQuota + int64(promptTokens)
|
||||
if request.MaxTokens != 0 {
|
||||
preConsumedTokens += int64(request.MaxTokens)
|
||||
}
|
||||
preConsumedQuota := int64(float64(preConsumedTokens) * ratio)
|
||||
|
||||
userQuota, err := dbmodel.CacheGetUserQuota(ctx, meta.UserId)
|
||||
if err != nil {
|
||||
return preConsumedQuota, openai.ErrorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if userQuota-preConsumedQuota < 0 {
|
||||
return preConsumedQuota, openai.ErrorWrapper(fmt.Errorf("user quota is not enough"), "insufficient_user_quota", http.StatusForbidden)
|
||||
}
|
||||
|
||||
err = dbmodel.CacheDecreaseUserQuota(meta.UserId, preConsumedQuota)
|
||||
if err != nil {
|
||||
return preConsumedQuota, openai.ErrorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if userQuota > 100*preConsumedQuota {
|
||||
// in this case, we do not pre-consume quota
|
||||
// because the user has enough quota
|
||||
preConsumedQuota = 0
|
||||
logger.Info(ctx, fmt.Sprintf("user %d has enough quota %d, trusted and no need to pre-consume", meta.UserId, userQuota))
|
||||
}
|
||||
|
||||
if preConsumedQuota > 0 {
|
||||
err := dbmodel.PreConsumeTokenQuota(meta.TokenId, preConsumedQuota)
|
||||
if err != nil {
|
||||
return preConsumedQuota, openai.ErrorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
return preConsumedQuota, nil
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/songquanpeng/one-api/common/ctxkey"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
"github.com/songquanpeng/one-api/relay/apitype"
|
||||
"github.com/songquanpeng/one-api/relay/channeltype"
|
||||
"github.com/songquanpeng/one-api/relay/relaymode"
|
||||
)
|
||||
@@ -63,11 +62,5 @@ func GetByContext(c *gin.Context) *Meta {
|
||||
meta.BaseURL = channeltype.ChannelBaseURLs[meta.ChannelType]
|
||||
}
|
||||
meta.APIType = channeltype.ToAPIType(meta.ChannelType)
|
||||
|
||||
// Force Anthropic API type for native Anthropic protocol requests
|
||||
if meta.Mode == relaymode.AnthropicMessages {
|
||||
meta.APIType = apitype.Anthropic
|
||||
}
|
||||
|
||||
return &meta
|
||||
}
|
||||
|
||||
@@ -13,6 +13,4 @@ const (
|
||||
AudioTranslation
|
||||
// Proxy is a special relay mode for proxying requests to custom upstream
|
||||
Proxy
|
||||
// AnthropicMessages is for native Anthropic API messages endpoint
|
||||
AnthropicMessages
|
||||
)
|
||||
|
||||
@@ -26,8 +26,6 @@ func GetByPath(path string) int {
|
||||
relayMode = AudioTranslation
|
||||
} else if strings.HasPrefix(path, "/v1/oneapi/proxy") {
|
||||
relayMode = Proxy
|
||||
} else if strings.HasPrefix(path, "/anthropic/v1/messages") {
|
||||
relayMode = AnthropicMessages
|
||||
}
|
||||
return relayMode
|
||||
}
|
||||
|
||||
@@ -71,16 +71,4 @@ func SetRelayRouter(router *gin.Engine) {
|
||||
relayV1Router.GET("/threads/:id/runs/:runsId/steps/:stepId", controller.RelayNotImplemented)
|
||||
relayV1Router.GET("/threads/:id/runs/:runsId/steps", controller.RelayNotImplemented)
|
||||
}
|
||||
|
||||
// Anthropic API compatibility - https://docs.anthropic.com/claude/reference/
|
||||
anthropicRouter := router.Group("/anthropic")
|
||||
anthropicRouter.Use(middleware.RelayPanicRecover(), middleware.TokenAuth(), middleware.Distribute())
|
||||
{
|
||||
// Models API
|
||||
anthropicRouter.GET("/v1/models", controller.ListModels)
|
||||
anthropicRouter.GET("/v1/models/:model", controller.RetrieveModel)
|
||||
|
||||
// Messages API - main endpoint for chat completions
|
||||
anthropicRouter.POST("/v1/messages", controller.Relay)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user