Quickstart
Create and Export API Key
First step is to create an API Key from GoLean Dashboard for your account. Once you create the API Key, you can later use it to compress prompts for authentication. Export your API Key as an environment variable in your terminal:
export GOLEAN_API_KEY="your_api_key_here"
Make your first GoLean API Request
Install library from PyPI:
pip install golean
Here's an example of compressing your prompt.
We break down a prompt into 2 parts:
prompt template: static string template containing placeholders of data
data: dynamic variables passed to the template during runtime
Currently, we keep the prompt template and compress the data. The compressed data values are then populated back to the prompt template to form the compressed prompt.
from golean import GoLean
# Initialize the GoLean client
golean = GoLean(api_key=os.getenv("GOLEAN_API_KEY"))
# Compress template and data
result = golean.compress_with_template(
template="""Read the passage, then answer the question. Only output the exact answer without any extra word or punctuation.
Passage: {context}
Question: {question}""",
data={
"context": "A large language model (LLM) is a type of computational model designed for natural language processing tasks such as language generation. As language models, LLMs acquire these abilities by learning statistical relationships from vast amounts of text during a self-supervised and semi-supervised training process." ,
"question": "name an example task the LLM is designed for"
}
)
print("Compressed Result:", result)
Here's the API output:
{
"compressed_result": "Read the passage, then answer the question. Only output the exact answer without any extra word or punctuation. \nPassage: large language model (LLM) is computational model for natural language processing tasks such language generation. LLMs acquire abilities by learning statistical relationships from vast amounts text during self-supervised and semi-supervised training process.\nQuestion: name example task LLM designed for",
"original_tokens": 93,
"compressed_tokens": 77,
"actual_compression_rate": 0.8279... // ~17% token reduction and cost savings
}
This is for prompt compression in the form of prompt template and data. If you just want to compress a piece of text, you can also do it. We call any piece of text context.
Context could be any text you want to compress. Examples of this could be user and bot messages in a conversation history, a pdf article, a body of work, etc.
```python
from golean import GoLean
import os
# Initialize the GoLean client
golean = GoLean(api_key=os.getenv("GOLEAN_API_KEY"))
# Compress context
result = golean.compress_with_context(
context="A large language model (LLM) is a type of computational model designed for natural language processing tasks such as language generation. As language models, LLMs acquire these abilities by learning statistical relationships from vast amounts of text during a self-supervised and semi-supervised training process."
)
print("Compressed Result:", result)
```
Here's the API output:
{
"compressed_result": "large language model (LLM) is computational model for natural language processing tasks such language generation. LLMs acquire abilities by learning statistical relationships from vast amounts text during self-supervised and semi-supervised training process",
"original_tokens": 56,
"compressed_tokens": 43,
"actual_compression_rate": 0.7678... // ~23% token reduction and cost savings
}
Last updated