28 个版本

0.5.9 2024 年 5 月 16 日
0.5.8 2023 年 12 月 22 日
0.5.7 2023 年 11 月 14 日
0.5.0 2023 年 6 月 27 日
0.3.3 2023 年 3 月 23 日

#4机器学习 中排名

Download history • Rust 包仓库 11393/week @ 2024-05-03 • Rust 包仓库 11844/week @ 2024-05-10 • Rust 包仓库 11717/week @ 2024-05-17 • Rust 包仓库 11551/week @ 2024-05-24 • Rust 包仓库 13090/week @ 2024-05-31 • Rust 包仓库 11212/week @ 2024-06-07 • Rust 包仓库 13252/week @ 2024-06-14 • Rust 包仓库 14260/week @ 2024-06-21 • Rust 包仓库 11915/week @ 2024-06-28 • Rust 包仓库 13818/week @ 2024-07-05 • Rust 包仓库 12721/week @ 2024-07-12 • Rust 包仓库 13966/week @ 2024-07-19 • Rust 包仓库 13417/week @ 2024-07-26 • Rust 包仓库 12898/week @ 2024-08-02 • Rust 包仓库 14398/week @ 2024-08-09 • Rust 包仓库 14042/week @ 2024-08-16 • Rust 包仓库

57,548 每月下载量
55crate中使用(直接使用49个)

MIT 许可证

8.5MB
1K SLoC

tiktoken-rs

Github Contributors Github Stars CI

crates.io status crates.io downloads Rust dependency status

使用 tiktoken 进行文本分词的 Rust 库

该库提供了一套现成的分词库,用于与 GPT、tiktoken 和相关 OpenAI 模型一起工作。用例包括对文本输入进行分词和计数。

此库基于 tiktoken 库,并包含一些额外的功能和增强,以便与 Rust 代码更方便地使用。

示例

有关所有支持特性的完整工作示例,请参阅仓库中的 examples 目录。

使用方法

  1. 使用 cargo 在本地安装此工具
cargo add tiktoken-rs

然后在您的 Rust 代码中调用 API

计数 token 长度

use tiktoken_rs::p50k_base;

let bpe = p50k_base().unwrap();
let tokens = bpe.encode_with_special_tokens(
  "This is a sentence   with spaces"
);
println!("Token count: {}", tokens.len());

计数聊天完成请求的 max_tokens 参数

use tiktoken_rs::{get_chat_completion_max_tokens, ChatCompletionRequestMessage};

let messages = vec![
    ChatCompletionRequestMessage {
        content: Some("You are a helpful assistant that only speaks French.".to_string()),
        role: "system".to_string(),
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Hello, how are you?".to_string()),
        role: "user".to_string(),
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Parlez-vous francais?".to_string()),
        role: "system".to_string(),
        name: None,
        function_call: None,
    },
];
let max_tokens = get_chat_completion_max_tokens("gpt-4", &messages).unwrap();
println!("max_tokens: {}", max_tokens);

计数聊天完成请求的 max_tokens 参数(使用 async-openai

需要在您的 Cargo.toml 文件中启用 async-openai 功能。

use tiktoken_rs::async_openai::get_chat_completion_max_tokens;
use async_openai::types::{ChatCompletionRequestMessage, Role};

let messages = vec![
    ChatCompletionRequestMessage {
        content: Some("You are a helpful assistant that only speaks French.".to_string()),
        role: Role::System,
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Hello, how are you?".to_string()),
        role: Role::User,
        name: None,
        function_call: None,
    },
    ChatCompletionRequestMessage {
        content: Some("Parlez-vous francais?".to_string()),
        role: Role::System,
        name: None,
        function_call: None,
    },
];
let max_tokens = get_chat_completion_max_tokens("gpt-4", &messages).unwrap();
println!("max_tokens: {}", max_tokens);

tiktoken 支持这些 OpenAI 模型使用的编码

编码名称 OpenAI 模型
o200k_base GPT-4o 模型。
cl100k_base ChatGPT 模型,text-embedding-ada-002
p50k_base 代码模型,text-davinci-002text-davinci-003
p50k_edit 用于编辑模型,如 text-davinci-edit-001code-davinci-edit-001
r50k_base(或 gpt2 GPT-3模型,如davinci

请在仓库中的示例中查看使用案例。有关不同分词器的更多背景信息,请参阅OpenAI菜谱

遇到任何错误吗?

如果您遇到任何错误或对改进有建议,请在该仓库中提交一个问题。

致谢

感谢@spolu提供的原始代码和.tiktoken文件。

许可证

本项目遵循MIT许可证

依赖项

~4–17MB
~234K SLoC