Tracking Token Counter in RefineDocumentChain and MapReduceDocument Chain #26575
-
I want to calculate the total token usage including intermidiate steps and the final output. with Input and output tokens. Is there a way to calculate tokens? I currently for testing am using Groq API with llama3.1-8B model. The code snippet is down below.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
To calculate the total token usage, including intermediate steps and the final output, in the from langchain_groq import ChatGroq
from dotenv import load_dotenv
from langchain.chains import RefineDocumentsChain
from langchain.chains.llm import LLMChain
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.document_loaders import TextLoader
load_dotenv()
llm = ChatGroq(
model="llama-3.1-8b-instant",
max_retries=2,
)
document_prompt = PromptTemplate(
input_variables=["page_content"], template="{page_content}"
)
document_variable_name = "context"
# The prompt here should take as an input variable the
# `document_variable_name`
summarize_prompt = ChatPromptTemplate(
[
("human", "Write a concise summary of the following: {context}"),
]
)
initial_llm_chain = LLMChain(llm=llm, prompt=summarize_prompt)
initial_response_name = "existing_answer"
refine_template = """
Produce a final summary.
Existing summary up to this point:
{existing_answer}
New context:
------------
{context}
------------
Given the new context, refine the original summary.
"""
refine_prompt = ChatPromptTemplate([("human", refine_template)])
refine_llm_chain = LLMChain(llm=llm, prompt=refine_prompt)
chain = RefineDocumentsChain(
initial_llm_chain=initial_llm_chain,
refine_llm_chain=refine_llm_chain,
document_prompt=document_prompt,
document_variable_name=document_variable_name,
initial_response_name=initial_response_name,
return_intermediate_steps=True
)
documents = TextLoader('./documents/text2.txt', encoding='utf8')
docs = documents.load()
print(len(docs))
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
chunk_size=1000, chunk_overlap=0,
)
split_docs = text_splitter.split_documents(docs)
print(f"Generated {len(split_docs)} documents.")
result = chain.invoke(split_docs)
# Calculate total token usage
total_tokens = sum(step['token_usage'] for step in result[1]['intermediate_steps'])
total_tokens += result[1]['final_output']['token_usage']
print(f"Total token usage: {total_tokens}") This code sets up the |
Beta Was this translation helpful? Give feedback.
-
I guess you might have to manually find the tokens using the llm.get_num_tokens() method. You need a walkthrough to calculate the tokens for your prompt templates, input and output tokens. |
Beta Was this translation helpful? Give feedback.
Yes, there are alternative solutions to calculate the token counter without writing a custom script. You can use a language model's built-in token counting method or other available methods in LangChain. Here are some options:
Using a Language Model's Built-in Token Counting Method
You can use a language model's built-in token counting method, such as
ChatOpenAI(model="gpt-4o")
.Example:
Using
tiktoken
for Custom Token CountingIf y…