Skip to content

Commit

Permalink
Sync callback object with llama.rn
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Apr 24, 2024
1 parent a32bf54 commit 23d9d61
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ export type LlamaCompletionResult = {
truncated: boolean
}

export type LlamaCompletionToken = {
token: string
}

export interface LlamaContext {
new (options: LlamaModelOptions): LlamaContext
getSystemInfo(): string
completion(options: LlamaCompletionOptions, onToken?: (token: string) => void): Promise<LlamaCompletionResult>
completion(options: LlamaCompletionOptions, callback?: (token: LlamaCompletionToken) => void): Promise<LlamaCompletionResult>
stopCompletion(): void
saveSession(path: string): Promise<void>
loadSession(path: string): Promise<void>
Expand Down
4 changes: 3 additions & 1 deletion src/addons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ class LlamaCompletionWorker : public Napi::AsyncWorker,
const char *c_token = strdup(token.c_str());
_tsfn.BlockingCall(c_token, [](Napi::Env env, Napi::Function jsCallback,
const char *value) {
jsCallback.Call({Napi::String::New(env, value)});
auto obj = Napi::Object::New(env);
obj.Set("token", Napi::String::New(env, value));
jsCallback.Call({obj});
});
}
// is it an end of generation?
Expand Down
60 changes: 60 additions & 0 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,63 @@ exports[`work fine 1`] = `
"truncated": false,
}
`;

exports[`work fine: partial 1`] = `
{
"token": " swo",
}
`;

exports[`work fine: partial 2`] = `
{
"token": "cha",
}
`;

exports[`work fine: partial 3`] = `
{
"token": "door",
}
`;

exports[`work fine: partial 4`] = `
{
"token": "ter",
}
`;

exports[`work fine: partial 5`] = `
{
"token": " scientific",
}
`;

exports[`work fine: partial 6`] = `
{
"token": " Windows",
}
`;

exports[`work fine: partial 7`] = `
{
"token": "Ca",
}
`;

exports[`work fine: partial 8`] = `
{
"token": " occupied",
}
`;

exports[`work fine: partial 9`] = `
{
"token": "",
}
`;

exports[`work fine: partial 10`] = `
{
"token": " alta",
}
`;
4 changes: 3 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ it('work fine', async () => {
max_length: 100,
n_predict: 10,
seed: 0,
}, () => {})
}, (data) => {
expect(data).toMatchSnapshot('partial')
})
expect(result).toMatchSnapshot()
})

0 comments on commit 23d9d61

Please sign in to comment.