From 80dd404d4cf3331e7efb9f812c2d1e66ffd15eee Mon Sep 17 00:00:00 2001 From: SachaMorard <2254275+SachaMorard@users.noreply.github.com> Date: Fri, 6 Mar 2026 08:58:46 +0100 Subject: [PATCH] feat: update compression response to new API format - Replace InputTokens, Rate with SavedTokens, CostSavings, Reduction, TimeMs - Use float64 for Reduction (API returns fractional percentages) - Update example, README, and tests Made-with: Cursor --- README.md | 13 +++++++++---- edgee/edgee.go | 5 +++-- edgee/edgee_test.go | 18 +++++++++++------- example/basic/main.go | 8 ++++---- example/compression/main.go | 24 +++++++++++------------- 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index a85e50d..514fc7f 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,11 @@ func main() { log.Fatal(err) } +<<<<<<< HEAD response, err := client.Send("gpt-5.2", "What is the capital of France?") +======= + response, err := client.Send("anthropic/claude-haiku-4-5", "What is the capital of France?") +>>>>>>> d846ba2 (feat: update compression response to new API format) if err != nil { log.Fatal(err) } @@ -43,7 +47,7 @@ func main() { The `Send()` method makes non-streaming chat completion requests: ```go -response, err := client.Send("gpt-5.2", "Hello, world!") +response, err := client.Send("anthropic/claude-haiku-4-5", "Hello, world!") if err != nil { log.Fatal(err) } @@ -59,9 +63,10 @@ if response.Usage != nil { } if response.Compression != nil { - fmt.Printf("Input tokens: %d\n", response.Compression.InputTokens) fmt.Printf("Saved tokens: %d\n", response.Compression.SavedTokens) - fmt.Printf("Compression rate: %.2f\n", response.Compression.Rate) + fmt.Printf("Reduction: %.1f%%\n", response.Compression.Reduction) + fmt.Printf("Cost savings: $%.3f\n", float64(response.Compression.CostSavings)/1000000) + fmt.Printf("Time: %d ms\n", response.Compression.TimeMs) } ``` @@ -70,7 +75,7 @@ if response.Compression != nil { The `Stream()` method enables real-time streaming responses: ```go -chunkChan, errChan := client.Stream("gpt-5.2", "Tell me a story") +chunkChan, errChan := client.Stream("anthropic/claude-haiku-4-5", "Tell me a story") for { select { diff --git a/edgee/edgee.go b/edgee/edgee.go index 25e1cb7..6782171 100644 --- a/edgee/edgee.go +++ b/edgee/edgee.go @@ -106,9 +106,10 @@ type Usage struct { // Compression represents compression information type Compression struct { - InputTokens int `json:"input_tokens"` SavedTokens int `json:"saved_tokens"` - Rate float64 `json:"rate"` + CostSavings int `json:"cost_savings"` // micro-units (e.g. 27000 = $0.027) + Reduction float64 `json:"reduction"` // percentage (e.g. 48 = 48%, may be fractional) + TimeMs int `json:"time_ms"` // milliseconds } // SendResponse represents the response from a non-streaming request diff --git a/edgee/edgee_test.go b/edgee/edgee_test.go index 6729fdc..c1ca74e 100644 --- a/edgee/edgee_test.go +++ b/edgee/edgee_test.go @@ -701,9 +701,10 @@ func TestClient_SendWithCompression(t *testing.T) { TotalTokens: 150, }, Compression: &Compression{ - InputTokens: 100, SavedTokens: 42, - Rate: 0.6102003642987249, + CostSavings: 27000, + Reduction: 48.0, + TimeMs: 150, }, } @@ -726,14 +727,17 @@ func TestClient_SendWithCompression(t *testing.T) { if response.Compression == nil { t.Fatal("Expected compression data, got nil") } - if response.Compression.InputTokens != 100 { - t.Errorf("Expected input_tokens 100, got %d", response.Compression.InputTokens) - } if response.Compression.SavedTokens != 42 { t.Errorf("Expected saved_tokens 42, got %d", response.Compression.SavedTokens) } - if response.Compression.Rate != 0.6102003642987249 { - t.Errorf("Expected rate 0.6102003642987249, got %f", response.Compression.Rate) + if response.Compression.CostSavings != 27000 { + t.Errorf("Expected cost_savings 27000, got %d", response.Compression.CostSavings) + } + if response.Compression.Reduction != 48.0 { + t.Errorf("Expected reduction 48, got %f", response.Compression.Reduction) + } + if response.Compression.TimeMs != 150 { + t.Errorf("Expected time_ms 150, got %d", response.Compression.TimeMs) } }) diff --git a/example/basic/main.go b/example/basic/main.go index 069109c..32de1e4 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -16,7 +16,7 @@ func main() { // Test 1: Simple string input fmt.Println("Test 1: Simple string input") - response1, err := client.ChatCompletion("mistral/mistral-small-latest", "What is the capital of France?") + response1, err := client.ChatCompletion("anthropic/claude-haiku-4-5", "What is the capital of France?") if err != nil { log.Printf("Error: %v\n", err) } else { @@ -29,7 +29,7 @@ func main() { // Test 2: Full input object with messages fmt.Println("Test 2: Full input object with messages") - response2, err := client.ChatCompletion("mistral/mistral-small-latest", map[string]interface{}{ + response2, err := client.ChatCompletion("anthropic/claude-haiku-4-5", map[string]interface{}{ "messages": []map[string]string{ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Say hello!"}, @@ -44,7 +44,7 @@ func main() { // Test 3: With tools fmt.Println("Test 3: With tools") - response3, err := client.ChatCompletion("gpt-5.2", map[string]interface{}{ + response3, err := client.ChatCompletion("anthropic/claude-haiku-4-5", map[string]interface{}{ "messages": []map[string]string{ {"role": "user", "content": "What is the weather in Paris?"}, }, @@ -81,7 +81,7 @@ func main() { // Test 4: Streaming fmt.Println("Test 4: Streaming") - chunkChan, errChan := client.Stream("mistral/mistral-small-latest", "What is Go?") + chunkChan, errChan := client.Stream("anthropic/claude-haiku-4-5", "What is Go?") for { select { case chunk, ok := <-chunkChan: diff --git a/example/compression/main.go b/example/compression/main.go index 11220d2..1825071 100644 --- a/example/compression/main.go +++ b/example/compression/main.go @@ -95,7 +95,7 @@ Based on this context, summarize the key milestones in AI development in 3 bulle input.EnableCompression = &enableCompression input.CompressionRate = &compressionRate - response, err := client.Send("gpt-5.2", input) + response, err := client.Send("anthropic/claude-haiku-4-5", input) if err != nil { log.Fatalf("Error: %v", err) } @@ -115,20 +115,18 @@ Based on this context, summarize the key milestones in AI development in 3 bulle // Display compression information if response.Compression != nil { fmt.Println("Compression Metrics:") - fmt.Printf(" Input tokens: %d\n", response.Compression.InputTokens) fmt.Printf(" Saved tokens: %d\n", response.Compression.SavedTokens) - fmt.Printf(" Compression rate: %.2f%%\n", response.Compression.Rate*100) - - var savingsPct float64 - if response.Compression.InputTokens > 0 { - savingsPct = (float64(response.Compression.SavedTokens) / float64(response.Compression.InputTokens)) * 100 + fmt.Printf(" Reduction: %.1f%%\n", response.Compression.Reduction) + fmt.Printf(" Cost savings: $%.3f\n", float64(response.Compression.CostSavings)/1000000) + fmt.Printf(" Time: %d ms\n", response.Compression.TimeMs) + if response.Compression.Reduction > 0 { + originalTokens := int(float64(response.Compression.SavedTokens) * 100 / response.Compression.Reduction) + tokensAfter := originalTokens - response.Compression.SavedTokens + fmt.Println() + fmt.Println(" 💡 Without compression, this request would have used") + fmt.Printf(" %d input tokens.\n", originalTokens) + fmt.Printf(" With compression, only %d tokens were processed!\n", tokensAfter) } - fmt.Printf(" Savings: %.1f%% of input tokens saved!\n", savingsPct) - fmt.Println() - fmt.Println(" 💡 Without compression, this request would have used") - fmt.Printf(" %d input tokens.\n", response.Compression.InputTokens) - fmt.Printf(" With compression, only %d tokens were processed!\n", - response.Compression.InputTokens-response.Compression.SavedTokens) } else { fmt.Println("No compression data available in response.") fmt.Println("Note: Compression data is only returned when compression is enabled")