Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the utility to count the token saved using Semantic Cache #182

Open
pierrelambert opened this issue Jul 10, 2024 · 2 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@pierrelambert
Copy link

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.

@tylerhutcherson
Copy link
Collaborator

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)

@justin-cechmanek
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants