Skip to content

Commit

Permalink
helpers for orchestration variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Thyrst committed Nov 21, 2024
1 parent 69da387 commit f6a5309
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.13.8

- New Handlebars helpers: last, toJSON, formatMessage (more variants)

## 0.13.7

- Fix react peerDependency version syntax #73
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langtail",
"version": "0.13.7",
"version": "0.13.8",
"description": "",
"main": "./Langtail.js",
"packageManager": "[email protected]",
Expand Down
41 changes: 41 additions & 0 deletions src/handlebars-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type handlebars from "@langtail/handlebars-evalless"
import { format, parseISO } from "date-fns"
import { Message } from "../schemas"

Check failure on line 3 in src/handlebars-helpers.ts

View workflow job for this annotation

GitHub Actions / runs typescript and tests

Cannot find module '../schemas' or its corresponding type declarations.

const isObject = function (val: any) {
return typeof val === "object"
Expand Down Expand Up @@ -68,3 +69,43 @@ export const operatorHelpers: handlebars.HelperDeclareSpec = {
return Array.prototype.slice.call(arguments, 0, -1).some(Boolean)
},
}

const formatMessage = (message: Message, includeToolCalls: boolean): string => {
if (!message) return '';

let result = `[${message.role}] `;

if (message.content) {
result += message.content;
}

if (includeToolCalls && message.tool_calls?.length) {
result += '\nTool Calls:';
for (const tool of message.tool_calls) {
result += `\n - ${tool.function.name}(${tool.function.arguments})`;
}
}

return result;
};

const formatMessages = (messages: Message[], includeToolCalls: boolean): string => {
if (!Array.isArray(messages)) return '';
return messages.map((m) => formatMessage(m, includeToolCalls)).join('\n\n');
};

export const variableHelpers: handlebars.HelperDeclareSpec = {
toJSON: function (context) {
return JSON.stringify(context, null, 2);
},
last: function (array) {
if (Array.isArray(array) && array.length > 0) {
return array[array.length - 1];
}
return null;
},
formatMessage: (message: Message) => formatMessage(message, false),
formatMessages: (messages: Message[]) => formatMessages(messages, false),
formatMessageWithToolCalls: (message: Message) => formatMessage(message, true),
formatMessagesWithToolCalls: (messages: Message[]) => formatMessages(messages, true),
}
10 changes: 9 additions & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import handlebarsEvalless from "@langtail/handlebars-evalless"

import { handlebarsDateHelper, operatorHelpers } from "./handlebars-helpers"
import { handlebarsDateHelper, operatorHelpers, variableHelpers } from "./handlebars-helpers"
import { JSONValue } from "./jsonType"
import { ContentArray } from "./schemas"

const handlebars = handlebarsEvalless.default ?? handlebarsEvalless

handlebars.registerHelper("$date", handlebarsDateHelper)
handlebars.registerHelper(operatorHelpers)
handlebars.registerHelper(variableHelpers)

export const LANGTAIL_HELPERS = [
"$date",
...Object.keys(operatorHelpers),
...Object.keys(variableHelpers),
]

const Visitor = handlebars.Visitor

/*
Expand Down

0 comments on commit f6a5309

Please sign in to comment.