Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gpt4o with o200k_base #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/Tiktoken/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum Model {
private extension Model {
static let MODEL_PREFIX_TO_ENCODING: [String: String] = [
// chat
"gpt-4o-": "o200k_base",
"gpt-4-": "cl100k_base", // e.g., gpt-4-0314, etc., plus gpt-4-32k
"gpt-3.5-turbo-": "cl100k_base", // e.g, gpt-3.5-turbo-0301, -0401, etc.
]
Expand All @@ -28,6 +29,7 @@ private extension Model {
// chat
"gpt-4": "cl100k_base",
"gpt-3.5-turbo": "cl100k_base",
"gpt-4o": "o200k_base",
// text
"text-davinci-003": "p50k_base",
"text-davinci-002": "p50k_base",
Expand Down
23 changes: 21 additions & 2 deletions Sources/Tiktoken/Vocab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ public extension Vocab {
"<|endofprompt|>": 100276
])
}

static var all: [Vocab] = [.gpt2, .r50kBase, .p50kBase, .p50kEdit, .cl100kBase]

static var o200kBase: Vocab {
.init(name: "o200k_base",
url: "https://openaipublic.blob.core.windows.net/encodings/o200k_base.tiktoken",
pattern:
String([
#"[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?"#,
#"[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?"#,
#"\p{N}{1,3}"#,
#" ?[^\s\p{L}\p{N}]+[\r\n/]*"#,
#"\s*[\r\n]+"#,
#"\s+(?!\S)"#,
#"\s+"#,
].joined(separator: "|")),
specialTokens: [
"<|endoftext|>": 199999,
"<|endofprompt|>": 200018
])
}

static var all: [Vocab] = [.gpt2, .r50kBase, .p50kBase, .p50kEdit, .cl100kBase, .o200kBase]
}
7 changes: 7 additions & 0 deletions Tests/TiktokenTests/TiktokenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ final class TiktokenTests: XCTestCase {
let output = try XCTUnwrap(encoder?.encode(value: input))
XCTAssertEqual(output, expected)
}

func testGivenGPT4oEncodeAndDecode() async throws {
let input = "hello world"
let encoder = try await sut.getEncoding("gpt-4o")
let output = try XCTUnwrap(encoder?.encode(value: input))
XCTAssertEqual(encoder?.decode(value: output), input)
}
}