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 other BPE models #5

Open
wants to merge 1 commit into
base: master
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
23 changes: 21 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,33 @@ import { TextArea } from "~/components/Input";
import {
encoding_for_model,
get_encoding,
Tiktoken,
type TiktokenModel,
type TiktokenEncoding,
} from "@dqbd/tiktoken";
import { getSegments } from "~/utils/segments";

import gptj from "~/ranks/gptj.json" assert { type: "json" };
import claude from "~/ranks/claude.json" assert { type: "json" };

function getUserSelectedEncoder(
params: { model: TiktokenModel } | { encoder: TiktokenEncoding }
params:
| { model: TiktokenModel | "gpt-j" | "claude" }
| { encoder: TiktokenEncoding }
) {
if ("model" in params) {
if (params.model === "gpt-j") {
return new Tiktoken(gptj.bpe_ranks, gptj.special_tokens, gptj.pat_str);
}

if (params.model === "claude") {
return new Tiktoken(
claude.bpe_ranks,
claude.special_tokens,
claude.pat_str
);
}

if (
params.model === "gpt-4" ||
params.model === "gpt-4-32k" ||
Expand All @@ -44,7 +62,8 @@ function getUserSelectedEncoder(
const Home: NextPage = () => {
const [inputText, setInputText] = useState<string>("");
const [params, setParams] = useState<
{ model: TiktokenModel } | { encoder: TiktokenEncoding }
| { model: TiktokenModel | "gpt-j" | "claude" }
| { encoder: TiktokenEncoding }
>({ model: "gpt-3.5-turbo" });

const [encoder, setEncoder] = useState(() => getUserSelectedEncoder(params));
Expand Down
12 changes: 12 additions & 0 deletions src/ranks/claude.json

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions src/ranks/gptj.json

Large diffs are not rendered by default.

38 changes: 34 additions & 4 deletions src/sections/EncoderSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const MODELS = [
"gpt-4",
"gpt-4-32k",
"gpt-3.5-turbo",
"gpt-j",
"claude",
];

const POPULAR = [
Expand All @@ -56,6 +58,8 @@ const POPULAR = [
"text-embedding-ada-002",
];

const CUSTOM = ["gpt-j", "claude"];

const CHAT_GPT_MODELS = ["gpt-3.5-turbo"];

const ENCODERS = ["gpt2", "cl100k_base", "p50k_base", "p50k_edit", "r50k_base"];
Expand All @@ -64,11 +68,15 @@ function isEncoder(encoder: string | undefined): encoder is TiktokenEncoding {
return !!encoder?.includes(encoder as TiktokenEncoding);
}

function isModel(model: string | undefined): model is TiktokenModel {
function isModel(
model: string | undefined
): model is TiktokenModel | "gpt-j" | "claude" {
return !!model?.includes(model as TiktokenModel);
}

type ModelOnly = { model: TiktokenModel } | { encoder: TiktokenEncoding };
type ModelOnly =
| { model: TiktokenModel | "gpt-j" | "claude" }
| { encoder: TiktokenEncoding };

export function EncoderSelect(props: {
value: ModelOnly;
Expand Down Expand Up @@ -132,8 +140,28 @@ export function EncoderSelect(props: {

<CommandSeparator />

<CommandGroup heading="Custom">
{CUSTOM.map((value) => (
<CommandItem
key={value}
value={`${
ENCODERS.includes(value) ? "encoder" : "model"
}:${value}`}
onSelect={onSelect}
>
{CHAT_GPT_MODELS.includes(value)
? `${value} (ChatGPT)`
: value}
</CommandItem>
))}
</CommandGroup>

<CommandSeparator />

<CommandGroup heading="Encoders">
{ENCODERS.filter((x) => !POPULAR.includes(x)).map((value) => (
{ENCODERS.filter(
(x) => !POPULAR.includes(x) && !CUSTOM.includes(x)
).map((value) => (
<CommandItem
key={value}
value={`encoder:${value}`}
Expand All @@ -147,7 +175,9 @@ export function EncoderSelect(props: {
<CommandSeparator />

<CommandGroup heading="Models">
{MODELS.filter((x) => !POPULAR.includes(x)).map((value) => (
{MODELS.filter(
(x) => !POPULAR.includes(x) && !CUSTOM.includes(x)
).map((value) => (
<CommandItem
key={value}
value={`model:${value}`}
Expand Down