Skip to content

Commit 4d52b9f

Browse files
committedJan 3, 2025
feat: add JsDoc to trimTokens function
1 parent 5d9b7de commit 4d52b9f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 

‎packages/core/src/generation.ts

+21
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ import { tavily } from "@tavily/core";
4646
type Tool = CoreTool<any, any>;
4747
type StepResult = AIStepResult<any>;
4848

49+
/**
50+
* Trims the provided text context to a specified token limit using a tokenizer model and type.
51+
*
52+
* The function dynamically determines the truncation method based on the tokenizer settings
53+
* provided by the runtime. If no tokenizer settings are defined, it defaults to using the
54+
* TikToken truncation method with the "gpt-4o" model.
55+
*
56+
* @async
57+
* @function trimTokens
58+
* @param {string} context - The text to be tokenized and trimmed.
59+
* @param {number} maxTokens - The maximum number of tokens allowed after truncation.
60+
* @param {IAgentRuntime} runtime - The runtime interface providing tokenizer settings.
61+
*
62+
* @returns {Promise<string>} A promise that resolves to the trimmed text.
63+
*
64+
* @throws {Error} Throws an error if the runtime settings are invalid or missing required fields.
65+
*
66+
* @example
67+
* const trimmedText = await trimTokens("This is an example text", 50, runtime);
68+
* console.log(trimmedText); // Output will be a truncated version of the input text.
69+
*/
4970
export async function trimTokens(
5071
context: string,
5172
maxTokens: number,

0 commit comments

Comments
 (0)