|
| 1 | +"""Example: Token compression with Edgee Gateway SDK |
| 2 | +
|
| 3 | +This example demonstrates how to: |
| 4 | +1. Enable compression for a request with a large input context |
| 5 | +2. Set a custom compression rate |
| 6 | +3. Access compression metrics from the response |
| 7 | +
|
| 8 | +IMPORTANT: Only USER messages are compressed. System messages are not compressed. |
| 9 | +This example includes a large context in the user message to demonstrate meaningful |
| 10 | +compression savings. |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | + |
| 16 | +# Add parent directory to path for local testing |
| 17 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 18 | + |
| 19 | +from edgee import Edgee |
| 20 | + |
| 21 | +# Initialize the client |
| 22 | +edgee = Edgee(os.environ.get("EDGEE_API_KEY")) |
| 23 | + |
| 24 | +# Large context document to demonstrate input compression |
| 25 | +LARGE_CONTEXT = """ |
| 26 | +The History and Impact of Artificial Intelligence |
| 27 | +
|
| 28 | +Artificial intelligence (AI) has evolved from a theoretical concept to a |
| 29 | +transformative technology that influences nearly every aspect of modern life. |
| 30 | +The field began in earnest in the 1950s when pioneers like Alan Turing and |
| 31 | +John McCarthy laid the groundwork for machine intelligence. |
| 32 | +
|
| 33 | +Early developments focused on symbolic reasoning and expert systems. These |
| 34 | +rule-based approaches dominated the field through the 1970s and 1980s, with |
| 35 | +systems like MYCIN demonstrating practical applications in medical diagnosis. |
| 36 | +However, these early systems were limited by their inability to learn from data |
| 37 | +and adapt to new situations. |
| 38 | +
|
| 39 | +The resurgence of neural networks in the 1980s and 1990s, particularly with |
| 40 | +backpropagation algorithms, opened new possibilities. Yet it wasn't until the |
| 41 | +2010s, with the advent of deep learning and the availability of massive datasets |
| 42 | +and computational power, that AI truly began to revolutionize industries. |
| 43 | +
|
| 44 | +Modern AI applications span numerous domains: |
| 45 | +- Natural language processing enables machines to understand and generate human language |
| 46 | +- Computer vision allows machines to interpret visual information from the world |
| 47 | +- Robotics combines AI with mechanical systems for autonomous operation |
| 48 | +- Healthcare uses AI for diagnosis, drug discovery, and personalized treatment |
| 49 | +- Finance leverages AI for fraud detection, algorithmic trading, and risk assessment |
| 50 | +- Transportation is being transformed by autonomous vehicles and traffic optimization |
| 51 | +
|
| 52 | +The development of large language models like GPT, BERT, and others has |
| 53 | +particularly accelerated progress in natural language understanding and generation. |
| 54 | +These models, trained on vast amounts of text data, can perform a wide range of |
| 55 | +language tasks with remarkable proficiency. |
| 56 | +
|
| 57 | +Despite remarkable progress, significant challenges remain. Issues of bias, |
| 58 | +interpretability, safety, and ethical considerations continue to be areas of |
| 59 | +active research and debate. The AI community is working to ensure that these |
| 60 | +powerful technologies are developed and deployed responsibly, with consideration |
| 61 | +for their societal impact. |
| 62 | +
|
| 63 | +Looking forward, AI is expected to continue advancing rapidly, with potential |
| 64 | +breakthroughs in areas like artificial general intelligence, quantum machine |
| 65 | +learning, and brain-computer interfaces. The integration of AI into daily life |
| 66 | +will likely deepen, raising important questions about human-AI collaboration, |
| 67 | +workforce transformation, and the future of human cognition itself. |
| 68 | +""" |
| 69 | + |
| 70 | +print("=" * 70) |
| 71 | +print("Edgee Token Compression Example") |
| 72 | +print("=" * 70) |
| 73 | +print() |
| 74 | + |
| 75 | +# Example: Request with compression enabled and large input |
| 76 | +print("Example: Large user message with compression enabled") |
| 77 | +print("-" * 70) |
| 78 | +print(f"Input context length: {len(LARGE_CONTEXT)} characters") |
| 79 | +print() |
| 80 | + |
| 81 | +# NOTE: Only USER messages are compressed |
| 82 | +# Put the large context in the user message to demonstrate compression |
| 83 | +user_message = f"""Here is some context about AI: |
| 84 | +
|
| 85 | +{LARGE_CONTEXT} |
| 86 | +
|
| 87 | +Based on this context, summarize the key milestones in AI development in 3 bullet points.""" |
| 88 | + |
| 89 | +response = edgee.send( |
| 90 | + model="gpt-4o", |
| 91 | + input={ |
| 92 | + "messages": [ |
| 93 | + {"role": "user", "content": user_message}, |
| 94 | + ], |
| 95 | + "enable_compression": True, |
| 96 | + "compression_rate": 0.5, |
| 97 | + }, |
| 98 | +) |
| 99 | + |
| 100 | +print(f"Response: {response.text}") |
| 101 | +print() |
| 102 | + |
| 103 | +# Display usage information |
| 104 | +if response.usage: |
| 105 | + print("Token Usage:") |
| 106 | + print(f" Prompt tokens: {response.usage.prompt_tokens}") |
| 107 | + print(f" Completion tokens: {response.usage.completion_tokens}") |
| 108 | + print(f" Total tokens: {response.usage.total_tokens}") |
| 109 | + print() |
| 110 | + |
| 111 | +# Display compression information |
| 112 | +if response.compression: |
| 113 | + print("Compression Metrics:") |
| 114 | + print(f" Input tokens: {response.compression.input_tokens}") |
| 115 | + print(f" Saved tokens: {response.compression.saved_tokens}") |
| 116 | + print(f" Compression rate: {response.compression.rate:.2%}") |
| 117 | + savings_pct = ( |
| 118 | + (response.compression.saved_tokens / response.compression.input_tokens * 100) |
| 119 | + if response.compression.input_tokens > 0 |
| 120 | + else 0 |
| 121 | + ) |
| 122 | + print(f" Savings: {savings_pct:.1f}% of input tokens saved!") |
| 123 | + print() |
| 124 | + print(" 💡 Without compression, this request would have used") |
| 125 | + print(f" {response.compression.input_tokens} input tokens.") |
| 126 | + print( |
| 127 | + f" With compression, only {response.compression.input_tokens - response.compression.saved_tokens} tokens were processed!" |
| 128 | + ) |
| 129 | +else: |
| 130 | + print("No compression data available in response.") |
| 131 | + print("Note: Compression data is only returned when compression is enabled") |
| 132 | + print(" and supported by your API key configuration.") |
| 133 | + |
| 134 | +print() |
| 135 | +print("=" * 70) |
0 commit comments