Skip to content

Commit 58f9a51

Browse files
committed
tiktoken to a weak dependency
1 parent dabf29c commit 58f9a51

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

CHANGLOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.1.26] - 2026-03-18
2+
### Fixed
3+
- update tiktoken to a weak dependency
4+
15
## [0.1.25] - 2026-01-22
26
### Added
37
- add to_runnable decorator

cozeloop/integration/langchain/util.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
22
# SPDX-License-Identifier: MIT
33

4-
import tiktoken
54
from typing import List, Dict, Union, Any, Optional
65
from langchain_core.outputs import LLMResult, Generation, ChatGeneration
76

7+
try:
8+
import tiktoken
9+
_cl100k_base_encoding = tiktoken.get_encoding('cl100k_base')
10+
except Exception:
11+
tiktoken = None # type: ignore[assignment]
12+
_cl100k_base_encoding = None
13+
814

915
def calc_token_usage(inputs: Union[List[Dict], LLMResult], model: str = 'gpt-3.5-turbo-0613'):
1016
"""Return the number of tokens used by a list of messages."""
17+
if tiktoken is None:
18+
return 0
1119
try:
1220
encoding = tiktoken.encoding_for_model(model)
1321
except KeyError:
1422
print('Warning: model not found. Using cl100k_base encoding.')
15-
encoding = tiktoken.get_encoding('cl100k_base')
23+
encoding = _cl100k_base_encoding
1624
if model == 'gpt-3.5-turbo-0301':
1725
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
1826
tokens_per_name = -1 # if there's a name, the role is omitted

cozeloop/internal/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
22
# SPDX-License-Identifier: MIT
33

4-
VERSION = 'v0.1.23'
4+
VERSION = 'v0.1.26'

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cozeloop"
3-
version = "0.1.25"
3+
version = "0.1.26"
44
description = "coze loop sdk"
55
authors = ["JiangQi715 <jiangqi.rrt@bytedance.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)