|
| 1 | +"""Example: Token compression with Edgee Gateway SDK |
| 2 | +
|
| 3 | +This example demonstrates how to: |
| 4 | +1. Enable compression for a request |
| 5 | +2. Set a custom compression rate |
| 6 | +3. Access compression metrics from the response |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | +# Add parent directory to path for local testing |
| 13 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 14 | + |
| 15 | +from edgee import Edgee |
| 16 | + |
| 17 | +# Initialize the client |
| 18 | +edgee = Edgee(os.environ.get("EDGEE_API_KEY")) |
| 19 | + |
| 20 | +print("=" * 70) |
| 21 | +print("Edgee Token Compression Example") |
| 22 | +print("=" * 70) |
| 23 | +print() |
| 24 | + |
| 25 | +# Example 1: Request with compression enabled |
| 26 | +print("Example 1: Request with compression enabled") |
| 27 | +print("-" * 70) |
| 28 | +response = edgee.send( |
| 29 | + model="gpt-4o", |
| 30 | + input={ |
| 31 | + "messages": [{"role": "user", "content": "Explain quantum computing in simple terms."}], |
| 32 | + "enable_compression": True, |
| 33 | + "compression_rate": 0.5, |
| 34 | + }, |
| 35 | +) |
| 36 | + |
| 37 | +print(f"Response: {response.text}") |
| 38 | +print() |
| 39 | + |
| 40 | +# Display usage information |
| 41 | +if response.usage: |
| 42 | + print("Token Usage:") |
| 43 | + print(f" Prompt tokens: {response.usage.prompt_tokens}") |
| 44 | + print(f" Completion tokens: {response.usage.completion_tokens}") |
| 45 | + print(f" Total tokens: {response.usage.total_tokens}") |
| 46 | + print() |
| 47 | + |
| 48 | +# Display compression information |
| 49 | +if response.compression: |
| 50 | + print("Compression Metrics:") |
| 51 | + print(f" Input tokens: {response.compression.input_tokens}") |
| 52 | + print(f" Saved tokens: {response.compression.saved_tokens}") |
| 53 | + print(f" Compression rate: {response.compression.rate:.2%}") |
| 54 | + print(f" Token savings: {response.compression.saved_tokens} tokens saved!") |
| 55 | +else: |
| 56 | + print("No compression data available in response.") |
| 57 | + print("Note: Compression data is only returned when compression is enabled") |
| 58 | + print(" and supported by your API key configuration.") |
| 59 | + |
| 60 | +print() |
| 61 | +print("=" * 70) |
0 commit comments