Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/a8m/envsubst v1.4.3
github.com/alecthomas/kong v1.15.0
github.com/andybalholm/brotli v1.2.1
github.com/anthropics/anthropic-sdk-go v1.45.0
github.com/anthropics/anthropic-sdk-go v1.46.0
github.com/aws/aws-sdk-go-v2 v1.41.7
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.10
github.com/aws/aws-sdk-go-v2/config v1.32.18
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs
github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro=
github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/anthropics/anthropic-sdk-go v1.45.0 h1:rWnpyBpm9OAm97jyH5bi6W4SRCwJeNY/RyhaJ7CHSUI=
github.com/anthropics/anthropic-sdk-go v1.45.0/go.mod h1:bx5vWuHFuGPkELH8Z4KUiNSohFnUwScdpTyr+50myPo=
github.com/anthropics/anthropic-sdk-go v1.46.0 h1:yl3n+el5ZfNgiCtQ7zQ7s/NXxB11YbrKXdc3uLPNWlU=
github.com/anthropics/anthropic-sdk-go v1.46.0/go.mod h1:bx5vWuHFuGPkELH8Z4KUiNSohFnUwScdpTyr+50myPo=
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
Expand Down
10 changes: 10 additions & 0 deletions internal/translator/anthropic_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ func (p *anthropicStreamParser) Process(body io.Reader, endOfStream bool, span t
totalTokens, _ := p.tokenUsage.TotalTokens()
cachedTokens, _ := p.tokenUsage.CachedInputTokens()
cacheCreationTokens, _ := p.tokenUsage.CacheCreationInputTokens()
reasoningTokens, _ := p.tokenUsage.ReasoningTokens()
finalChunk := openai.ChatCompletionResponseChunk{
ID: p.activeMessageID,
Created: p.created,
Expand All @@ -877,6 +878,9 @@ func (p *anthropicStreamParser) Process(body io.Reader, endOfStream bool, span t
CachedTokens: int(cachedTokens),
CacheCreationTokens: int(cacheCreationTokens),
},
CompletionTokensDetails: &openai.CompletionTokensDetails{
ReasoningTokens: int(reasoningTokens),
},
},
Model: p.requestModel,
}
Expand Down Expand Up @@ -1064,6 +1068,7 @@ func (p *anthropicStreamParser) handleAnthropicStreamEvent(eventType []byte, dat
// Accumulate cache creation tokens
p.tokenUsage.AddCacheCreationInputTokens(cacheCreation)
}
p.tokenUsage.SetReasoningTokens(uint32(u.OutputTokensDetails.ThinkingTokens)) //nolint:gosec
if event.Delta.StopReason != "" {
p.stopReason = event.Delta.StopReason
}
Expand Down Expand Up @@ -1178,11 +1183,13 @@ func messageToChatCompletion(anthropicResp *anthropic.Message, responseModel int
&usage.CacheReadInputTokens,
&usage.CacheCreationInputTokens,
)
tokenUsage.SetReasoningTokens(uint32(usage.OutputTokensDetails.ThinkingTokens)) //nolint:gosec
inputTokens, _ := tokenUsage.InputTokens()
outputTokens, _ := tokenUsage.OutputTokens()
totalTokens, _ := tokenUsage.TotalTokens()
cachedTokens, _ := tokenUsage.CachedInputTokens()
cacheCreationTokens, _ := tokenUsage.CacheCreationInputTokens()
reasoningTokens, _ := tokenUsage.ReasoningTokens()
openAIResp.Usage = openai.Usage{
CompletionTokens: int(outputTokens),
PromptTokens: int(inputTokens),
Expand All @@ -1191,6 +1198,9 @@ func messageToChatCompletion(anthropicResp *anthropic.Message, responseModel int
CachedTokens: int(cachedTokens),
CacheCreationTokens: int(cacheCreationTokens),
},
CompletionTokensDetails: &openai.CompletionTokensDetails{
ReasoningTokens: int(reasoningTokens),
},
}

finishReason, err := anthropicToOpenAIFinishReason(anthropicResp.StopReason)
Expand Down
4 changes: 3 additions & 1 deletion internal/translator/openai_awsanthropic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func TestOpenAIToAWSAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
PromptTokensDetails: &openai.PromptTokensDetails{
CachedTokens: 5,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -367,6 +368,7 @@ func TestOpenAIToAWSAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
PromptTokensDetails: &openai.PromptTokensDetails{
CachedTokens: 10,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -420,7 +422,7 @@ func TestOpenAIToAWSAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
int32(tt.expectedOpenAIResponse.Usage.PromptTokensDetails.CacheCreationTokens), // nolint:gosec
int32(tt.expectedOpenAIResponse.Usage.CompletionTokens), // nolint:gosec
int32(tt.expectedOpenAIResponse.Usage.TotalTokens), // nolint:gosec
-1,
int32(tt.expectedOpenAIResponse.Usage.CompletionTokensDetails.ReasoningTokens), // nolint:gosec
Comment thread
hustxiayang marked this conversation as resolved.
)
require.Equal(t, expectedTokenUsage, usedToken)

Expand Down
7 changes: 6 additions & 1 deletion internal/translator/openai_gcpanthropic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ func TestOpenAIToGCPAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
CachedTokens: 5,
CacheCreationTokens: 3,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -478,6 +479,7 @@ func TestOpenAIToGCPAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
CachedTokens: 10,
CacheCreationTokens: 7,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -524,6 +526,7 @@ func TestOpenAIToGCPAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
PromptTokensDetails: &openai.PromptTokensDetails{
CachedTokens: 2,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -557,6 +560,7 @@ func TestOpenAIToGCPAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
PromptTokensDetails: &openai.PromptTokensDetails{
CachedTokens: 3,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -602,6 +606,7 @@ func TestOpenAIToGCPAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
PromptTokensDetails: &openai.PromptTokensDetails{
CachedTokens: 1,
},
CompletionTokensDetails: &openai.CompletionTokensDetails{},
},
Choices: []openai.ChatCompletionResponseChoice{
{
Expand Down Expand Up @@ -651,7 +656,7 @@ func TestOpenAIToGCPAnthropicTranslatorV1ChatCompletion_ResponseBody(t *testing.
int32(tt.expectedOpenAIResponse.Usage.PromptTokensDetails.CacheCreationTokens), // nolint:gosec
int32(tt.expectedOpenAIResponse.Usage.CompletionTokens), // nolint:gosec
int32(tt.expectedOpenAIResponse.Usage.TotalTokens), // nolint:gosec
-1,
int32(tt.expectedOpenAIResponse.Usage.CompletionTokensDetails.ReasoningTokens), // nolint:gosec
Comment thread
hustxiayang marked this conversation as resolved.
)
require.Equal(t, expectedTokenUsage, usedToken)

Expand Down
10 changes: 5 additions & 5 deletions tests/data-plane/testupstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func TestWithTestUpstream(t *testing.T) {
requestBody: toolCallResultsRequestBody,
expRequestBody: `{"max_tokens":1024,"messages":[{"content":[{"text":"List the files in the /tmp directory","type":"text"}],"role":"user"},{"content":[{"id":"call_abc123","input":{"path":"/tmp"},"name":"list_files","type":"tool_use"}],"role":"assistant"},{"content":[{"tool_use_id":"call_abc123","is_error":false,"content":[{"text":"[\"foo.txt\", \"bar.log\", \"data.csv\"]","type":"text"}],"type":"tool_result"}],"role":"user"}],"anthropic_version":"vertex-2023-10-16"}`,
responseBody: `{"id":"msg_123","type":"message","role":"assistant","stop_reason": "end_turn", "content":[{"type":"text","text":"Hello from Anthropic!"}],"usage":{"input_tokens":10,"output_tokens":25,"cache_read_input_tokens":10}}`,
expResponseBody: `{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hello from Anthropic!","role":"assistant"}}],"created":123, "id":"msg_123","model":"gpt-4-0613","object":"chat.completion","usage":{"completion_tokens":25,"prompt_tokens":20,"total_tokens":45,"prompt_tokens_details":{"cached_tokens":10}}}`,
expResponseBody: `{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hello from Anthropic!","role":"assistant"}}],"created":123, "id":"msg_123","model":"gpt-4-0613","object":"chat.completion","usage":{"completion_tokens":25,"completion_tokens_details":{},"prompt_tokens":20,"total_tokens":45,"prompt_tokens_details":{"cached_tokens":10}}}`,
expStatus: http.StatusOK,
},
{
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestWithTestUpstream(t *testing.T) {
responseStatus: strconv.Itoa(http.StatusOK),
responseBody: `{"id":"msg_123","type":"message","role":"assistant","stop_reason": "end_turn", "content":[{"type":"text","text":"Hello from Anthropic!"}],"usage":{"input_tokens":10,"output_tokens":25}}`,
expStatus: http.StatusOK,
expResponseBody: `{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hello from Anthropic!","role":"assistant"}}],"created":123, "id":"msg_123","model":"claude-3-sonnet","object":"chat.completion","usage":{"completion_tokens":25,"prompt_tokens":10,"total_tokens":35,"prompt_tokens_details":{}}}`,
expResponseBody: `{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hello from Anthropic!","role":"assistant"}}],"created":123, "id":"msg_123","model":"claude-3-sonnet","object":"chat.completion","usage":{"completion_tokens":25,"completion_tokens_details":{},"prompt_tokens":10,"total_tokens":35,"prompt_tokens_details":{}}}`,
},
{
name: "gcp-anthropicai - /v1/chat/completions - with cache",
Expand All @@ -372,7 +372,7 @@ func TestWithTestUpstream(t *testing.T) {
responseStatus: strconv.Itoa(http.StatusOK),
responseBody: `{"id":"msg_123","type":"message","role":"assistant","stop_reason": "end_turn", "content":[{"type":"text","text":"Hello from cached Anthropic!"}],"usage":{"input_tokens":10,"output_tokens":25, "cache_read_input_tokens": 8}}`,
expStatus: http.StatusOK,
expResponseBody: `{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hello from cached Anthropic!","role":"assistant"}}], "created":123, "id":"msg_123", "model":"claude-3-sonnet","object":"chat.completion","usage":{"completion_tokens":25,"prompt_tokens":18,"total_tokens":43,"prompt_tokens_details":{"cached_tokens":8}}}`,
expResponseBody: `{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"Hello from cached Anthropic!","role":"assistant"}}], "created":123, "id":"msg_123", "model":"claude-3-sonnet","object":"chat.completion","usage":{"completion_tokens":25,"completion_tokens_details":{},"prompt_tokens":18,"total_tokens":43,"prompt_tokens_details":{"cached_tokens":8}}}`,
},
{
name: "modelname-override - /v1/chat/completions",
Expand Down Expand Up @@ -578,7 +578,7 @@ data: {"id":"msg_123","choices":[{"index":0,"delta":{"content":" due to Rayleigh

data: {"id":"msg_123","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"created":123,"model":"claude-3-sonnet","object":"chat.completion.chunk"}

data: {"id":"msg_123","choices":[],"created":123,"model":"claude-3-sonnet","object":"chat.completion.chunk","usage":{"prompt_tokens":25,"completion_tokens":12,"total_tokens":37,"prompt_tokens_details":{"cached_tokens":10}}}
data: {"id":"msg_123","choices":[],"created":123,"model":"claude-3-sonnet","object":"chat.completion.chunk","usage":{"prompt_tokens":25,"completion_tokens":12,"total_tokens":37,"completion_tokens_details":{},"prompt_tokens_details":{"cached_tokens":10}}}

data: [DONE]

Expand Down Expand Up @@ -642,7 +642,7 @@ data: {"id":"msg_123","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"i

data: {"id":"msg_123","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}],"created":123,"model":"claude-3-sonnet","object":"chat.completion.chunk"}

data: {"id":"msg_123","choices":[],"created":123,"model":"claude-3-sonnet","object":"chat.completion.chunk","usage":{"prompt_tokens":50,"completion_tokens":20,"total_tokens":70,"prompt_tokens_details":{}}}
data: {"id":"msg_123","choices":[],"created":123,"model":"claude-3-sonnet","object":"chat.completion.chunk","usage":{"prompt_tokens":50,"completion_tokens":20,"total_tokens":70,"completion_tokens_details":{},"prompt_tokens_details":{}}}

data: [DONE]

Expand Down