-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce better tokenizer and token counting
- Loading branch information
1 parent
004b058
commit eee61f0
Showing
4 changed files
with
131 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule Buildel.LangChain.ChatGptTokenizerTest do | ||
alias Buildel.Langchain.ChatGptTokenizer | ||
|
||
use Buildel.LangChain.BaseCase | ||
|
||
test "correctly counts tokens" do | ||
tokenizer = ChatGptTokenizer.init("gpt-3.5-turbo") | ||
|
||
messages = [ | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :incomplete, | ||
index: 0, | ||
function_name: "query", | ||
role: :assistant, | ||
arguments: "" | ||
}, | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :incomplete, | ||
index: 0, | ||
function_name: nil, | ||
role: :unknown, | ||
arguments: "{\"" | ||
}, | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :incomplete, | ||
index: 0, | ||
function_name: nil, | ||
role: :unknown, | ||
arguments: "query" | ||
}, | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :incomplete, | ||
index: 0, | ||
function_name: nil, | ||
role: :unknown, | ||
arguments: "\":\"" | ||
}, | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :incomplete, | ||
index: 0, | ||
function_name: nil, | ||
role: :unknown, | ||
arguments: "TEST" | ||
}, | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :incomplete, | ||
index: 0, | ||
function_name: nil, | ||
role: :unknown, | ||
arguments: "\"}" | ||
}, | ||
%LangChain.MessageDelta{ | ||
content: nil, | ||
status: :complete, | ||
index: 0, | ||
function_name: nil, | ||
role: :unknown, | ||
arguments: nil | ||
} | ||
] | ||
|
||
assert ChatGptTokenizer.count_chain_tokens(tokenizer, %{ | ||
functions: [], | ||
input_messages: [], | ||
messages: messages | ||
}) == %Buildel.Langchain.ChatTokenSummary{ | ||
model: "gpt-3.5-turbo", | ||
endpoint: "openai", | ||
input_tokens: 3, | ||
output_tokens: 14 | ||
} | ||
end | ||
end |