llama_index [问题]:如何在Anthropic模型中计算token?

juud5qan  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(34)

问题验证

  • 我已在文档和Discord中寻找答案。

问题

你好,我想知道如何计算Anthropic模型的tokens(嵌入令牌、LLM提示令牌、LLM完成令牌、总LLM令牌计数)?

vfwfrxfs

vfwfrxfs1#

要使用 TokenCountingHandler 类为 Anthropic models 计算 tokens(Embedding Tokens,LLM Prompt Tokens,LLM Completion Tokens,Total LLM Token Count),请按照以下步骤操作:

  1. 安装 LlamaIndex:
!pip install llama-index
  1. 初始化 TokenCountingHandler:
import tiktoken
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings

token_counter = TokenCountingHandler(
    tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode
)

Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.2)
Settings.callback_manager = CallbackManager([token_counter])
  • 处理事件:
  • 对于 LLM 事件,调用 on_event_end 并传入适当的有效载荷。
  • 对于 Embedding 事件,调用 on_event_end 并传入适当的有效载荷。
  • 访问令牌计数:
  • total_llm_token_count : Total LLM token count。
  • prompt_llm_token_count : Total LLM prompt token count。
  • completion_llm_token_count : Total LLM completion token count。
  • total_embedding_token_count : Total Embedding token count。

以下是一个示例代码片段:

from llama_index.core.callbacks import TokenCountingHandler, CBEventType

# Initialize the handler
token_counting_handler = TokenCountingHandler(verbose=True)

# Simulate an LLM event
llm_payload = {
    "prompt": "What is the capital of France?",
    "completion": "The capital of France is Paris."
}
token_counting_handler.on_event_end(CBEventType.LLM, payload=llm_payload)

# Simulate an Embedding event
embedding_payload = {
    "chunks": ["This is a test sentence for embedding."]
}
token_counting_handler.on_event_end(CBEventType.EMBEDDING, payload=embedding_payload)

# Access the token counts
print("Total LLM Token Count:", token_counting_handler.total_llm_token_count)
print("LLM Prompt Token Count:", token_counting_handler.prompt_llm_token_count)
print("LLM Completion Token Count:", token_counting_handler.completion_llm_token_count)
print("Total Embedding Token Count:", token_counting_handler.total_embedding_token_count)

此代码将计算给定的 LLM 和 Embedding 事件的 tokens,并打印相应的 token 计数。对于更详细的用法,包括处理流式传输和高级 token 计数,您可以参考 TokenCountingHandler documentation1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/callbacks/token_counting.py)2(https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/callbacks/TokenCountingHandler.ipynb)。

  • 要继续与 Dosu 对话,请提及 @dosu。*
fxnxkyjh

fxnxkyjh2#

@logan-markewich TokenCountingHandler 不支持 Anthropic models。您能推荐其他方式来计算令牌吗?

rslzwgfq

rslzwgfq3#

@Ninlawat-Puhu,这与上面的内容相同,只是在TokenCountingHandler中需要更改分词器参数。你需要执行以下操作:

from llama_index.llms.anthropic import Anthropic #Assuming you have installed Anthropic plugin

token_counter = TokenCountingHandler(
   tokenizer = Anthropic().tokenizer.encode
)

请参考这里:https://docs.llamaindex.ai/en/stable/examples/llm/anthropic/#set-tokenizer

相关问题