mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	fix: implement improved headers for anthropic to support 8k outputs (#1654)
This commit is contained in:
		@@ -147,7 +147,6 @@ var InitialRootAccessToken = os.Getenv("INITIAL_ROOT_ACCESS_TOKEN")
 | 
			
		||||
 | 
			
		||||
var GeminiVersion = env.String("GEMINI_VERSION", "v1")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var OnlyOneLogFile = env.Bool("ONLY_ONE_LOG_FILE", false)
 | 
			
		||||
 | 
			
		||||
var RelayProxy = env.String("RELAY_PROXY", "")
 | 
			
		||||
 
 | 
			
		||||
@@ -3,12 +3,14 @@ package anthropic
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/adaptor"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/meta"
 | 
			
		||||
	"github.com/songquanpeng/one-api/relay/model"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Adaptor struct {
 | 
			
		||||
@@ -31,6 +33,13 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *me
 | 
			
		||||
	}
 | 
			
		||||
	req.Header.Set("anthropic-version", anthropicVersion)
 | 
			
		||||
	req.Header.Set("anthropic-beta", "messages-2023-12-15")
 | 
			
		||||
 | 
			
		||||
	// https://x.com/alexalbert__/status/1812921642143900036
 | 
			
		||||
	// claude-3-5-sonnet can support 8k context
 | 
			
		||||
	if strings.HasPrefix(meta.ActualModelName, "claude-3-5-sonnet") {
 | 
			
		||||
		req.Header.Set("anthropic-beta", "max-tokens-3-5-sonnet-2024-07-15")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,6 @@ func init() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type innerAIAdapter interface {
 | 
			
		||||
	ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error)
 | 
			
		||||
	DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode)
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,6 @@ type ApplicationDefaultCredentials struct {
 | 
			
		||||
	UniverseDomain          string `json:"universe_domain"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var Cache = cache.New(50*time.Minute, 55*time.Minute)
 | 
			
		||||
 | 
			
		||||
const defaultScope = "https://www.googleapis.com/auth/cloud-platform"
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,9 @@ type Meta struct {
 | 
			
		||||
	APIType      int
 | 
			
		||||
	Config       model.ChannelConfig
 | 
			
		||||
	IsStream     bool
 | 
			
		||||
	// OriginModelName is the model name from the raw user request
 | 
			
		||||
	OriginModelName string
 | 
			
		||||
	// ActualModelName is the model name after mapping
 | 
			
		||||
	ActualModelName string
 | 
			
		||||
	RequestURLPath  string
 | 
			
		||||
	PromptTokens    int // only for DoResponse
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user