pytilpack.tiktoken¶
必要なextra
pip install pytilpack[tiktoken]
pytilpack.tiktoken
¶
tiktoken関連のユーティリティ集。
get_encoding_for_model(model_name)
¶
モデル名からtiktokenのEncodingを取得する。
ソースコード位置: pytilpack/tiktoken.py
num_tokens_from_messages(model, messages, tools=None, tool_choice=None)
¶
メッセージからトークン数を算出。
https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
引数:
| 名前 | タイプ | デスクリプション | デフォルト |
|---|---|---|---|
model
|
str
|
モデル名。 |
必須 |
messages
|
list[ChatCompletionMessageParam]
|
メッセージのリスト。 |
必須 |
tools
|
list[ChatCompletionToolParam] | None
|
function callingの情報。 |
None
|
tool_choice
|
ChatCompletionNamedToolChoiceParam | None
|
ツール選択の情報。 |
None
|
戻り値:
| タイプ | デスクリプション |
|---|---|
int
|
トークン数。 |
ソースコード位置: pytilpack/tiktoken.py
num_tokens_from_texts(model, texts)
¶
num_tokens_from_tools(encoding, tools)
¶
Function calling部分のトークン数算出。(非推奨)
ソースコード位置: pytilpack/tiktoken.py
num_tokens_for_tools(model, tools, tool_choice=None, encoding=None)
¶
Function calling部分のトークン数算出。
https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/10
ソースコード位置: pytilpack/tiktoken.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |