-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core[patch]: Reducing heap area consumption regardless of the number …
…of prompts (#3519) * Remove unused option * Cache the Tiktoken object * Fix format * Bump core version * Upgrade to [email protected] --------- Co-authored-by: jacoblee93 <[email protected]> Co-authored-by: Tat Dat Duong <[email protected]>
- Loading branch information
1 parent
e0c23e3
commit 72aa018
Showing
3 changed files
with
19 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,30 @@ | ||
import { | ||
Tiktoken, | ||
TiktokenBPE, | ||
TiktokenEncoding, | ||
TiktokenModel, | ||
getEncodingNameForModel, | ||
} from "js-tiktoken/lite"; | ||
import { AsyncCaller } from "./async_caller.js"; | ||
|
||
const cache: Record<string, Promise<TiktokenBPE>> = {}; | ||
const cache: Record<string, Promise<Tiktoken>> = {}; | ||
|
||
const caller = /* #__PURE__ */ new AsyncCaller({}); | ||
|
||
export async function getEncoding( | ||
encoding: TiktokenEncoding, | ||
options?: { | ||
signal?: AbortSignal; | ||
extendedSpecialTokens?: Record<string, number>; | ||
} | ||
) { | ||
export async function getEncoding(encoding: TiktokenEncoding) { | ||
if (!(encoding in cache)) { | ||
cache[encoding] = caller | ||
.fetch(`https://tiktoken.pages.dev/js/${encoding}.json`, { | ||
signal: options?.signal, | ||
}) | ||
.fetch(`https://tiktoken.pages.dev/js/${encoding}.json`) | ||
.then((res) => res.json()) | ||
.then((data) => new Tiktoken(data)) | ||
.catch((e) => { | ||
delete cache[encoding]; | ||
throw e; | ||
}); | ||
} | ||
|
||
return new Tiktoken(await cache[encoding], options?.extendedSpecialTokens); | ||
return await cache[encoding]; | ||
} | ||
|
||
export async function encodingForModel( | ||
model: TiktokenModel, | ||
options?: { | ||
signal?: AbortSignal; | ||
extendedSpecialTokens?: Record<string, number>; | ||
} | ||
) { | ||
return getEncoding(getEncodingNameForModel(model), options); | ||
export async function encodingForModel(model: TiktokenModel) { | ||
return getEncoding(getEncodingNameForModel(model)); | ||
} |
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
72aa018
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-api-refs – ./docs/api_refs
langchainjs-api-refs-git-main-langchain.vercel.app
langchainjs-api-docs.vercel.app
langchainjs-api-refs-langchain.vercel.app
api.js.langchain.com
72aa018
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-docs – ./docs/core_docs/
langchainjs-docs-ruddy.vercel.app
js.langchain.com
langchainjs-docs-git-main-langchain.vercel.app
langchainjs-docs-langchain.vercel.app