-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from askorama/docs/adds-comments
general refactor
- Loading branch information
Showing
6 changed files
with
62 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,6 @@ jobs: | |
|
||
- name: Run tests | ||
run: deno test -A | ||
|
||
- name: Build Node.js package | ||
run: deno run -A dnt.ts |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,27 +1,23 @@ | ||
import { AutoTokenizer, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]' | ||
|
||
env.useBrowserCache = false | ||
env.allowLocalModels = false | ||
import type { Tiktoken } from 'npm:js-tiktoken' | ||
import { getEncoding } from 'npm:js-tiktoken' | ||
|
||
/** | ||
* Represents a Chunker object that can be used to tokenize input strings and count the number of tokens. | ||
*/ | ||
export class Chunker { | ||
protected verbose = false | ||
protected ready: Promise<boolean> | ||
// deno-lint-ignore no-explicit-any | ||
private tokenizer: any | ||
private tokenizer: Tiktoken | ||
|
||
constructor() { | ||
this.ready = this.init() | ||
.then(() => true) | ||
.catch(() => false) | ||
} | ||
|
||
private async init() { | ||
this.tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased') | ||
this.tokenizer = getEncoding('gpt2') | ||
} | ||
|
||
public async getNumberOfTokens(input: string): Promise<number> { | ||
await this.ready | ||
const result = await this.tokenizer(input) | ||
return result.input_ids.size | ||
/** | ||
* Gets the number of tokens in the input string. | ||
* @param input - The input string to tokenize. | ||
* @returns A promise that resolves with the number of tokens in the input string. | ||
*/ | ||
public getNumberOfTokens(input: string): number { | ||
return this.tokenizer.encode(input).length | ||
} | ||
} |
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,20 +1,28 @@ | ||
import nlp from 'https://esm.sh/[email protected]/one' | ||
/** | ||
* Represents a chunker that uses natural language processing (NLP) to split text into chunks. | ||
* This chunker extends the base `Chunker` class. | ||
*/ | ||
import nlp from 'npm:[email protected]/one' | ||
import { Chunker } from './common.ts' | ||
|
||
export class NLPChunker extends Chunker { | ||
public async chunk(input: string, maxTokensPerChunk: number): Promise<string[]> { | ||
/** | ||
* Splits the input text into chunks based on the maximum number of tokens per chunk. | ||
* @param {String} input - The input text to be chunked. | ||
* @param {Number} maxTokensPerChunk - The maximum number of tokens allowed per chunk. | ||
* @returns A promise that resolves to an array of chunks. | ||
*/ | ||
public chunk(input: string, maxTokensPerChunk: number): string[] { | ||
const sentences = nlp.tokenize(input).fullSentences().out('array') | ||
const chunks: string[] = [] | ||
|
||
let currentChunk = '' | ||
for (const sentence of sentences) { | ||
const [sentenceTokenCount, currentChunkTokenCount] = await Promise.all([ | ||
this.getNumberOfTokens(sentence), | ||
this.getNumberOfTokens(currentChunk), | ||
]) | ||
const sentenceTokenCount = this.getNumberOfTokens(sentence) | ||
const currentChunkTokenCount = this.getNumberOfTokens(currentChunk) | ||
|
||
if (sentenceTokenCount + currentChunkTokenCount <= maxTokensPerChunk) { | ||
currentChunk += (currentChunk ? ' ' : '') + sentence // Ensure space between sentences | ||
currentChunk += (currentChunk ? ' ' : '') + sentence | ||
} else { | ||
if (currentChunk) { | ||
chunks.push(currentChunk) | ||
|