redis-vl-python icon indicating copy to clipboard operation
redis-vl-python copied to clipboard

Provide the utility to count the token saved using Semantic Cache

Open pierrelambert opened this issue 1 year ago • 2 comments

It would be nice to have an optional mode permitting to monitor the number of token the Semantic Cache permits to save. To do so, today we can store the token as metadata item.

from langchain_community.callbacks import get_openai_callback

with get_openai_callback() as cb:
        response = agent.run(input=user_query, callbacks=[retrieval_handler])
        red.set("query_token_total",cb.total_tokens)
        red.incr("session_token",cb.total_tokens)
    return response

(...)

metadata=dict()
                if int(red.get("query_token_total").decode('utf-8'))!=0:
                    metadata["token"]= int(red.get("query_token_total").decode('utf-8'))
                    llmcache.store(user_query, response, metadata=metadata)
                else:
                    llmcache.store(user_query, response)

I am wondering if it would make sense to make such easier as it permits to highlight the Semantic Cache value.

pierrelambert avatar Jul 10 '24 08:07 pierrelambert

Interesting idea. @justin-cechmanek curious your take on this. Is there a way we could build this as a generalized extension on the cache so that we could handle this elegantly (not just OpenAI obviously)

tylerhutcherson avatar Aug 20 '24 13:08 tylerhutcherson

We could elevate the token_count to be a permanent optional field, similar to prompt and response, instead of requiring users to define it in meta data. Users will still have to pass in the token count per prompt/response because that will vary by LLM tokenizer, but we can handle the incrementing.

justin-cechmanek avatar Aug 22 '24 22:08 justin-cechmanek