File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,27 @@ import { tavily } from "@tavily/core";
46
46
type Tool = CoreTool < any , any > ;
47
47
type StepResult = AIStepResult < any > ;
48
48
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
+ */
49
70
export async function trimTokens (
50
71
context : string ,
51
72
maxTokens : number ,
You can’t perform that action at this time.
0 commit comments