The Token utilities help you:
- Count tokens in text strings
- Select search results that fit within token limits
- Manage context window sizes
- Optimize token usage for completions
fromunique_sdk.utils.tokenimportcount_tokenstext="Hello, how are you today?"token_count=count_tokens(text)print(f"Tokens: {token_count}")# Output: Tokens: 6
messages=[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"What is Python?"},{"role":"assistant","content":"Python is a programming language."}]total_tokens=sum(count_tokens(msg["content"])formsginmessages)print(f"Total tokens: {total_tokens}")
unique_sdk.utils.token.pick_search_results_for_token_window - Select results within token limit
Selects search results that fit within a specified token limit.
Parameters:
searchResults (required) - List of search result objects with text field
tokenLimit (required) - Maximum number of tokens to include
encoding_model (optional) - Encoding model (default: "cl100k_base")
Returns:
List of search result objects that fit within the token limit
fromunique_sdk.utils.tokenimportpick_search_results_for_token_windowimportunique_sdk# Perform searchsearch=unique_sdk.Search.create(user_id=user_id,company_id=company_id,searchString="project documentation",searchType="COMBINED",limit=50# Get more than needed)# Select results that fit in 4000 tokensselected_results=pick_search_results_for_token_window(search["data"],tokenLimit=4000)print(f"Selected {len(selected_results)} results")
fromunique_sdk.utils.tokenimportcount_tokens,pick_search_results_for_token_windowdefbuild_optimized_context(query,max_context_tokens=4000):"""Build context that fits within token limit."""# Searchsearch=unique_sdk.Search.create(user_id=user_id,company_id=company_id,searchString=query,searchType="COMBINED",limit=100# Get many results)# Select best results that fitcontext=pick_search_results_for_token_window(search["data"],tokenLimit=max_context_tokens)returncontext
fromunique_sdk.utils.tokenimportcount_tokensdefcheck_token_usage(messages,max_tokens):"""Check if messages fit within token limit."""total=sum(count_tokens(msg["content"])formsginmessages)iftotal>max_tokens:print(f"Warning: {total} tokens exceeds limit of {max_tokens}")returnFalseprint(f"Using {total}/{max_tokens} tokens ({total/max_tokens*100:.1f}%)")returnTruemessages=[{"role":"system","content":"..."},{"role":"user","content":"..."}]check_token_usage(messages,max_tokens=8000)
# Always reserve tokens for the AI responsemax_tokens=8000response_reserve=1000# Reserve for responseavailable_for_context=max_tokens-response_reservecontext=pick_search_results_for_token_window(search["data"],tokenLimit=available_for_context)
fromunique_sdk.utils.sourcesimportmerge_sources,sort_sourcesfromunique_sdk.utils.tokenimportpick_search_results_for_token_window# 1. Process sources firstmerged=merge_sources(search["data"])sorted_results=sort_sources(merged)# 2. Then select by token limitcontext=pick_search_results_for_token_window(sorted_results,tokenLimit=4000)