|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from llama_index.core.callbacks.schema import EventPayload |
| 4 | +from llama_index.core.utilities.token_counting import TokenCounter |
| 5 | + |
| 6 | + |
| 7 | +class Constants: |
| 8 | + KEY_USAGE = "usage" |
| 9 | + KEY_USAGE_METADATA = "usage_metadata" |
| 10 | + KEY_EVAL_COUNT = "eval_count" |
| 11 | + KEY_PROMPT_EVAL_COUNT = "prompt_eval_count" |
| 12 | + KEY_RAW_RESPONSE = "_raw_response" |
| 13 | + INPUT_TOKENS = "input_tokens" |
| 14 | + OUTPUT_TOKENS = "output_tokens" |
| 15 | + PROMPT_TOKENS = "prompt_tokens" |
| 16 | + COMPLETION_TOKENS = "completion_tokens" |
| 17 | + DEFAULT_TOKEN_COUNT = 0 |
| 18 | + |
| 19 | + |
| 20 | +class TokenCounter: |
| 21 | + prompt_llm_token_count: int |
| 22 | + completion_llm_token_count: int |
| 23 | + total_llm_token_count: int = 0 |
| 24 | + total_embedding_token_count: int = 0 |
| 25 | + |
| 26 | + def __init__(self, input_tokens, output_tokens): |
| 27 | + self.prompt_llm_token_count = input_tokens |
| 28 | + self.completion_llm_token_count = output_tokens |
| 29 | + self.total_llm_token_count = ( |
| 30 | + self.prompt_llm_token_count + self.completion_llm_token_count |
| 31 | + ) |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def get_llm_token_counts(payload: dict[str, Any]) -> TokenCounter: |
| 35 | + token_counter = TokenCounter( |
| 36 | + input_tokens=Constants.DEFAULT_TOKEN_COUNT, |
| 37 | + output_tokens=Constants.DEFAULT_TOKEN_COUNT, |
| 38 | + ) |
| 39 | + if EventPayload.PROMPT in payload: |
| 40 | + completion_raw = payload.get(EventPayload.COMPLETION).raw |
| 41 | + if completion_raw: |
| 42 | + if completion_raw.get(Constants.KEY_USAGE): |
| 43 | + token_counts: dict[ |
| 44 | + str, int |
| 45 | + ] = TokenCounter._get_prompt_completion_tokens(completion_raw) |
| 46 | + token_counter = TokenCounter( |
| 47 | + input_tokens=token_counts[Constants.PROMPT_TOKENS], |
| 48 | + output_tokens=token_counts[Constants.COMPLETION_TOKENS], |
| 49 | + ) |
| 50 | + elif completion_raw.get(Constants.KEY_RAW_RESPONSE): |
| 51 | + if hasattr( |
| 52 | + completion_raw.get(Constants.KEY_RAW_RESPONSE), |
| 53 | + Constants.KEY_USAGE_METADATA, |
| 54 | + ): |
| 55 | + usage = completion_raw.get( |
| 56 | + Constants.KEY_RAW_RESPONSE |
| 57 | + ).usage_metadata |
| 58 | + token_counter = TokenCounter( |
| 59 | + input_tokens=usage.prompt_token_count, |
| 60 | + output_tokens=usage.candidates_token_count, |
| 61 | + ) |
| 62 | + else: |
| 63 | + prompt_tokens = Constants.DEFAULT_TOKEN_COUNT |
| 64 | + completion_tokens = Constants.DEFAULT_TOKEN_COUNT |
| 65 | + if completion_raw.get(Constants.KEY_PROMPT_EVAL_COUNT): |
| 66 | + prompt_tokens = completion_raw.get( |
| 67 | + Constants.KEY_PROMPT_EVAL_COUNT |
| 68 | + ) |
| 69 | + if completion_raw.get(Constants.KEY_EVAL_COUNT): |
| 70 | + completion_tokens = completion_raw.get(Constants.KEY_EVAL_COUNT) |
| 71 | + token_counter = TokenCounter( |
| 72 | + input_tokens=prompt_tokens, |
| 73 | + output_tokens=completion_tokens, |
| 74 | + ) |
| 75 | + elif EventPayload.MESSAGES in payload: |
| 76 | + response_raw = payload.get(EventPayload.RESPONSE).raw |
| 77 | + if response_raw: |
| 78 | + token_counts: dict[ |
| 79 | + str, int |
| 80 | + ] = TokenCounter._get_prompt_completion_tokens(response_raw) |
| 81 | + token_counter = TokenCounter( |
| 82 | + input_tokens=token_counts[Constants.PROMPT_TOKENS], |
| 83 | + output_tokens=token_counts[Constants.COMPLETION_TOKENS], |
| 84 | + ) |
| 85 | + |
| 86 | + return token_counter |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _get_prompt_completion_tokens(response) -> dict[str, int]: |
| 90 | + prompt_tokens = Constants.DEFAULT_TOKEN_COUNT |
| 91 | + completion_tokens = Constants.DEFAULT_TOKEN_COUNT |
| 92 | + |
| 93 | + usage = response.get(Constants.KEY_USAGE) |
| 94 | + if usage: |
| 95 | + if hasattr(usage, Constants.INPUT_TOKENS): |
| 96 | + prompt_tokens = usage.input_tokens |
| 97 | + elif hasattr(usage, Constants.PROMPT_TOKENS): |
| 98 | + prompt_tokens = usage.prompt_tokens |
| 99 | + |
| 100 | + if hasattr(usage, Constants.OUTPUT_TOKENS): |
| 101 | + completion_tokens = usage.output_tokens |
| 102 | + elif hasattr(usage, Constants.COMPLETION_TOKENS): |
| 103 | + completion_tokens = usage.completion_tokens |
| 104 | + |
| 105 | + token_counts: dict[str, int] = dict() |
| 106 | + token_counts[Constants.PROMPT_TOKENS] = prompt_tokens |
| 107 | + token_counts[Constants.COMPLETION_TOKENS] = completion_tokens |
| 108 | + return token_counts |
0 commit comments