Skip to content

Commit

Permalink
add tokenRepr in jsctrl
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmingtarja committed Mar 15, 2024
1 parent 977e50a commit 6262f05
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/jsctrl/samples/aici-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ declare module "_aici" {
*/
function detokenize(tokens: number[]): Buffer;

/**
* Return debug string representation of a given token index
*/
function tokenRepr(token: number): string;

/**
* Return identifier of the current sequence.
* Most useful with fork_group parameter in mid_process() callback.
Expand Down
6 changes: 6 additions & 0 deletions controllers/jsctrl/src/jsctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ mod aici_mod {
Buffer(bytes)
}

#[rquickjs::function]
pub fn tokenRepr(token: TokenId) -> String {
let trie = &mut GLOBAL_STATE.lock().unwrap().trie;
trie.token_dbg(token)
}

#[rquickjs::function]
pub fn getVar(name: String) -> Option<Buffer> {
let name = name.as_str();
Expand Down
2 changes: 2 additions & 0 deletions controllers/jsctrl/ts/aici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ export async function genTokens(options: GenOptions): Promise<Token[]> {
const tokens = await next_token.run();
res.push(...tokens);

console.log("GEN-STEP:", tokens.map(t => _aici.tokenRepr(t)).join(", "));

const text = detokenize(res).decode();

if (stopAt !== undefined && text.includes(stopAt)) {
Expand Down
5 changes: 5 additions & 0 deletions controllers/jsctrl/ts/native.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ declare module "_aici" {
*/
function detokenize(tokens: number[]): Buffer;

/**
* Return debug string representation of a given token index
*/
function tokenRepr(token: number): string;

/**
* Return identifier of the current sequence.
* Most useful with fork_group parameter in mid_process() callback.
Expand Down

0 comments on commit 6262f05

Please sign in to comment.