zijiren 7 месяцев назад
Родитель
Сommit
d4eba2dde3

+ 5 - 0
core/relay/adaptor/anthropic/adaptor.go

@@ -38,6 +38,8 @@ func (a *Adaptor) SetupRequestHeader(meta *meta.Meta, c *gin.Context, req *http.
 		anthropicVersion = AnthropicVersion
 	}
 	req.Header.Set("Anthropic-Version", anthropicVersion)
+
+	// https://docs.anthropic.com/en/api/beta-headers
 	req.Header.Set("Anthropic-Beta", "messages-2023-12-15")
 
 	// https://x.com/alexalbert__/status/1812921642143900036
@@ -50,6 +52,9 @@ func (a *Adaptor) SetupRequestHeader(meta *meta.Meta, c *gin.Context, req *http.
 		req.Header.Set("Anthropic-Beta", "output-128k-2025-02-19")
 	}
 
+	// https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration-beta
+	// req.Header.Set("Anthropic-Beta", "extended-cache-ttl-2025-04-11")
+
 	return nil
 }
 

+ 19 - 2
core/relay/adaptor/anthropic/model.go

@@ -74,6 +74,16 @@ type Tool struct {
 
 type CacheControl struct {
 	Type string `json:"type"`
+	// "5m" | "1h"
+	TTL string `json:"ttl,omitempty"`
+}
+
+func (cc *CacheControl) ResetTTL() *CacheControl {
+	if cc == nil {
+		return nil
+	}
+	cc.TTL = ""
+	return cc
 }
 
 type InputSchema struct {
@@ -106,8 +116,15 @@ type Usage struct {
 	InputTokens  int64 `json:"input_tokens"`
 	OutputTokens int64 `json:"output_tokens"`
 
-	CacheCreationInputTokens int64 `json:"cache_creation_input_tokens"`
-	CacheReadInputTokens     int64 `json:"cache_read_input_tokens"`
+	CacheCreationInputTokens int64          `json:"cache_creation_input_tokens"`
+	CacheReadInputTokens     int64          `json:"cache_read_input_tokens"`
+	CacheCreation            *CacheCreation `json:"cache_creation,omitempty"`
+}
+
+// https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration-beta
+type CacheCreation struct {
+	Ephemeral5mInputTokens int64 `json:"ephemeral_5m_input_tokens,omitempty"`
+	Ephemeral1hInputTokens int64 `json:"ephemeral_1h_input_tokens,omitempty"`
 }
 
 type Error struct {

+ 4 - 4
core/relay/adaptor/anthropic/openai.go

@@ -73,7 +73,7 @@ func OpenAIConvertRequest(meta *meta.Meta, req *http.Request) (*Request, error)
 				DisplayWidthPx:  tool.DisplayWidthPx,
 				DisplayHeightPx: tool.DisplayHeightPx,
 				DisplayNumber:   tool.DisplayNumber,
-				CacheControl:    tool.CacheControl,
+				CacheControl:    tool.CacheControl.ResetTTL(),
 			})
 		} else {
 			if params, ok := tool.Function.Parameters.(map[string]any); ok {
@@ -89,7 +89,7 @@ func OpenAIConvertRequest(meta *meta.Meta, req *http.Request) (*Request, error)
 						Properties: params["properties"],
 						Required:   params["required"],
 					},
-					CacheControl: tool.CacheControl,
+					CacheControl: tool.CacheControl.ResetTTL(),
 				})
 			}
 		}
@@ -154,7 +154,7 @@ func OpenAIConvertRequest(meta *meta.Meta, req *http.Request) (*Request, error)
 			claudeRequest.System = append(claudeRequest.System, Content{
 				Type:         conetentTypeText,
 				Text:         message.StringContent(),
-				CacheControl: message.CacheControl,
+				CacheControl: message.CacheControl.ResetTTL(),
 			})
 			continue
 		}
@@ -162,7 +162,7 @@ func OpenAIConvertRequest(meta *meta.Meta, req *http.Request) (*Request, error)
 			Role: message.Role,
 		}
 		var content Content
-		content.CacheControl = message.CacheControl
+		content.CacheControl = message.CacheControl.ResetTTL()
 		if message.IsStringContent() {
 			content.Type = conetentTypeText
 			content.Text = message.StringContent()

+ 2 - 1
go.work.sum

@@ -114,6 +114,7 @@ golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457 h1:zf5N6UOrA487eEFacMe
 golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
 golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
 golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
+golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
 golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
 golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
 golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
@@ -123,10 +124,10 @@ google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAs
 google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
 google.golang.org/genproto v0.0.0-20250303144028-a0af3efb3deb h1:ITgPrl429bc6+2ZraNSzMDk3I95nmQln2fuPstKwFDE=
 google.golang.org/genproto v0.0.0-20250303144028-a0af3efb3deb/go.mod h1:sAo5UzpjUwgFBCzupwhcLcxHVDK7vG5IqI30YnwX2eE=
-google.golang.org/genproto v0.0.0-20250505200425-f936aa4a68b2 h1:1tXaIXCracvtsRxSBsYDiSBN0cuJvM7QYW+MrpIRY78=
 google.golang.org/genproto/googleapis/bytestream v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:WkJpQl6Ujj3ElX4qZaNm5t6cT95ffI4K+HKQ0+1NyMw=
 google.golang.org/genproto/googleapis/bytestream v0.0.0-20250414145226-207652e42e2e h1:OK8bKvRgTGs7U871RdjtCiRcQJLice8/rZkeoaZgnlc=
 google.golang.org/genproto/googleapis/bytestream v0.0.0-20250414145226-207652e42e2e/go.mod h1:h6yxum/C2qRb4txaZRLDHK8RyS0H/o2oEDeKY4onY/Y=
+google.golang.org/genproto/googleapis/bytestream v0.0.0-20250512202823-5a2f75b736a9/go.mod h1:h6yxum/C2qRb4txaZRLDHK8RyS0H/o2oEDeKY4onY/Y=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 lukechampine.com/uint128 v1.3.0 h1:cDdUVfRwDUDovz610ABgFD17nXD4/uDgVHl2sC3+sbo=