harbor run -t openthoughts/tasktrove-mix-h2-language-balanced-v2-mix-h2-lang-balanced-3175harbor download openthoughts/tasktrove-mix-h2-language-balanced-v2-mix-h2-lang-balanced-3175solution file
import { Message, PromptFunctions, PromptMemory, RenderedPromptSection, Tokenizer } from "./types";
import { PromptSectionBase } from "./PromptSectionBase";
import { Utilities } from "./Utilities";
/**
* A template section that will be rendered as a message.
* @remarks
* This section type is used to render a template as a message. The template can contain
* parameters that will be replaced with values from memory or call functions to generate
* dynamic content.
*
* Template syntax:
* - `{{$memoryKey}}` - Renders the value of the specified memory key.
* - `{{functionName}}` - Calls the specified function and renders the result.
* - `{{functionName arg1 arg2 ...}}` - Calls the specified function with the provided list of arguments.
*
* Function arguments are optional and separated by spaces. They can be quoted using `'`, `"`, or `\`` delimiters.
*/
export class TemplateSection extends PromptSectionBase {
private _parts: PartRenderer[] = [];
public readonly template: string;
public readonly role: string;
/**
* Creates a new 'TemplateSection' instance.
* @param template Template to use for this section.
* @param role Message role to use for this section.
* @param tokens Optional. Sizing strategy for this section. Defaults to `auto`.
* @param required Optional. Indicates if this section is required. Defaults to `true`.
* @param separator Optional. Separator to use between sections when rendering as text. Defaults to `\n`.
* @param textPrefix Optional. Prefix to use for text output. Defaults to `undefined`.
*/
public constructor(template: string, role: string, tokens: number = -1, required: boolean = true, separator: string = '\n', textPrefix?: string) {
super(tokens, required, separator, textPrefix);
this.template = template;
this.role = role;
this.parseTemplate();
}
public async renderAsMessages(memory: PromptMemory, functions: PromptFunctions, tokenizer: Tokenizer, maxTokens: number): Promise<RenderedPromptSection<Message[]>> {
// Render parts in parallel
const renderedParts = await Promise.all(this._parts.map((part) => part(memory, functions, tokenizer, maxTokens)));
// Join all parts
const text = renderedParts.join('');
const length = tokenizer.encode(text).length;
// Return output
return this
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py