|
| 1 | +# |
| 2 | +# Copyright 2025 The InfiniFlow Authors. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | + |
| 18 | +import os |
| 19 | +import tiktoken |
| 20 | + |
| 21 | +from common.file_utils import get_project_base_directory |
| 22 | + |
| 23 | +tiktoken_cache_dir = get_project_base_directory() |
| 24 | +os.environ["TIKTOKEN_CACHE_DIR"] = tiktoken_cache_dir |
| 25 | +# encoder = tiktoken.encoding_for_model("gpt-3.5-turbo") |
| 26 | +encoder = tiktoken.get_encoding("cl100k_base") |
| 27 | + |
| 28 | + |
| 29 | +def num_tokens_from_string(string: str) -> int: |
| 30 | + """Returns the number of tokens in a text string.""" |
| 31 | + try: |
| 32 | + code_list = encoder.encode(string) |
| 33 | + return len(code_list) |
| 34 | + except Exception: |
| 35 | + return 0 |
| 36 | + |
| 37 | +def total_token_count_from_response(resp): |
| 38 | + if resp is None: |
| 39 | + return 0 |
| 40 | + |
| 41 | + if hasattr(resp, "usage") and hasattr(resp.usage, "total_tokens"): |
| 42 | + try: |
| 43 | + return resp.usage.total_tokens |
| 44 | + except Exception: |
| 45 | + pass |
| 46 | + |
| 47 | + if hasattr(resp, "usage_metadata") and hasattr(resp.usage_metadata, "total_tokens"): |
| 48 | + try: |
| 49 | + return resp.usage_metadata.total_tokens |
| 50 | + except Exception: |
| 51 | + pass |
| 52 | + |
| 53 | + if 'usage' in resp and 'total_tokens' in resp['usage']: |
| 54 | + try: |
| 55 | + return resp["usage"]["total_tokens"] |
| 56 | + except Exception: |
| 57 | + pass |
| 58 | + |
| 59 | + if 'usage' in resp and 'input_tokens' in resp['usage'] and 'output_tokens' in resp['usage']: |
| 60 | + try: |
| 61 | + return resp["usage"]["input_tokens"] + resp["usage"]["output_tokens"] |
| 62 | + except Exception: |
| 63 | + pass |
| 64 | + |
| 65 | + if 'meta' in resp and 'tokens' in resp['meta'] and 'input_tokens' in resp['meta']['tokens'] and 'output_tokens' in resp['meta']['tokens']: |
| 66 | + try: |
| 67 | + return resp["meta"]["tokens"]["input_tokens"] + resp["meta"]["tokens"]["output_tokens"] |
| 68 | + except Exception: |
| 69 | + pass |
| 70 | + return 0 |
| 71 | + |
| 72 | + |
| 73 | +def truncate(string: str, max_len: int) -> str: |
| 74 | + """Returns truncated text if the length of text exceed max_len.""" |
| 75 | + return encoder.decode(encoder.encode(string)[:max_len]) |
| 76 | + |
0 commit comments