def get_token_counter(session):
token_counter = TokenCountingHandler(
tokenizer=tiktoken.encoding_for_model(session.model).encode)
# token_counter.reset_counts()
callback_manager = CallbackManager([token_counter])
# set the global service_context
embed_model = OpenAIEmbedding(embed_batch_size=100) # ada-002
llm = OpenAI(model=session.model, temperature=0)
service_context = ServiceContext.from_defaults(
embed_model=embed_model, llm=llm, callback_manager=callback_manager)
set_global_service_context(service_context)
return token_counter
token_counter = get_token_counter(session)
.......
print("TOKEN USEDDDD: ", (token_counter.total_llm_token_count +
token_counter.total_embedding_token_count))