Skip to content

Commit dfe13de

Browse files
committed
feat(new tool): GPT Token Encoder/Decoder
Fix CorentinTh#1109, CorentinTh#1334
1 parent c031d57 commit dfe13de

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<script setup lang="ts">
2+
import type { TiktokenModel } from 'js-tiktoken';
3+
import { GPTTokens } from '../gpt-token-estimator/gpt-tokens.service';
4+
import TextareaCopyable from '@/components/TextareaCopyable.vue';
5+
import { useQueryParamOrStorage } from '@/composable/queryParams';
6+
7+
const models = GPTTokens.supportModels;
8+
const model = useQueryParamOrStorage({ name: 'model', storageName: 'gpt-token-encoder:model', defaultValue: 'gpt-3.5-turbo-1106' });
9+
10+
const decodedInput = ref('');
11+
const encodedOutput = computed(
12+
() => {
13+
try {
14+
return GPTTokens.encode(model.value as TiktokenModel, decodedInput.value).join(' ');
15+
}
16+
catch (e: any) {
17+
return e.toString();
18+
}
19+
},
20+
);
21+
22+
const encodedInput = ref('');
23+
const decodedOutput = computed(
24+
() => {
25+
try {
26+
return GPTTokens.decode(model.value as TiktokenModel, encodedInput.value.split(/\s+/).map(Number));
27+
}
28+
catch (e: any) {
29+
return e.toString();
30+
}
31+
},
32+
);
33+
</script>
34+
35+
<template>
36+
<div max-w-600>
37+
<c-select
38+
v-model:value="model"
39+
label="GPT IA Model"
40+
:options="models"
41+
mb-2
42+
/>
43+
44+
<c-card title="Encode text to GPT Tokens">
45+
<c-input-text
46+
v-model:value="decodedInput"
47+
placeholder="Put your text to encode here..."
48+
label="Text to encode"
49+
raw-text
50+
multiline
51+
rows="5"
52+
mb-5
53+
/>
54+
55+
<n-divider />
56+
57+
<TextareaCopyable
58+
label="Encoded tokens"
59+
:value="encodedOutput"
60+
multiline
61+
readonly
62+
rows="5"
63+
mb-5
64+
/>
65+
</c-card>
66+
67+
<c-card title="Decode GPT Tokens to text" mt-5>
68+
<c-input-text
69+
v-model:value="encodedInput"
70+
placeholder="Put your encoded tokens here..."
71+
label="Tokens to decode"
72+
raw-text
73+
multiline
74+
rows="5"
75+
mb-5
76+
/>
77+
78+
<n-divider />
79+
80+
<TextareaCopyable
81+
label="Decoded text"
82+
:value="decodedOutput"
83+
multiline
84+
readonly
85+
rows="5"
86+
mb-5
87+
/>
88+
</c-card>
89+
</div>
90+
</template>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { FileText } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'GPT Token Encoder/Decoder',
6+
path: '/gpt-token-encoder',
7+
description: 'Encode text to GPT tokens and decode GPT tokens back to text',
8+
keywords: ['gpt', 'llm', 'openai', 'token', 'encode', 'decode'],
9+
component: () => import('./gpt-token-encoder.vue'),
10+
icon: FileText,
11+
createdAt: new Date('2025-08-15'),
12+
category: 'Text',
13+
});

src/tools/gpt-token-estimator/gpt-token-estimator.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useI18n } from 'vue-i18n';
33
import JSON5 from 'json5';
44
import type { TiktokenModel } from 'js-tiktoken';
55
import { GPTTokens } from './gpt-tokens.service';
6-
import TextareaCopyable from '@/components/TextareaCopyable.vue';
76
import { useValidation } from '@/composable/validation';
87
import { useQueryParamOrStorage } from '@/composable/queryParams';
98
@@ -162,6 +161,12 @@ const outputTokenCosts = computed(() => {
162161
<n-form-item :label="t('tools.gpt-token-estimator.texts.label-completion-tokens')" label-placement="left">
163162
<InputCopyable :value="outputTokenCosts.completionUsedTokens" />
164163
</n-form-item>
164+
165+
<n-p mt-3>
166+
<n-a href="https://platform.openai.com/docs/pricing" target="_blank">
167+
See OpenAPI Pricings
168+
</n-a>
169+
</n-p>
165170
</div>
166171
</div>
167172
</template>

0 commit comments

Comments
 (0)