Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffered Output for Bespoke UI. #22

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

See this http://keepachangelog.com link for information on how we want this document formatted.

## v1.4.1

### Added

New buffered output support for non-LLM tools. This allows a tool to add string data as part of the `ask()` response. However, this data is not submitted to tool outputs, hence is not seen by the parent. Useful for bespoke UI where an LLM assistant is formatting data for display.

See Bespoke UI Assistant test for a full example.

## v1.4.0

### Changed
Expand Down
1 change: 0 additions & 1 deletion TODO.md

This file was deleted.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "experts",
"version": "1.4.0",
"version": "1.4.1",
"description": "An opinionated panel of experts implementation using OpenAI's Assistants API",
"type": "module",
"main": "./src/index.js",
Expand Down
13 changes: 13 additions & 0 deletions src/experts/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Assistant {
#stream;
#streamEmitter;
#expertsOutputs = [];
#bufferedOutputs = [];
#asyncListeners = {};

static async create(options = {}) {
Expand Down Expand Up @@ -103,8 +104,13 @@ class Assistant {
this.#expertsOutputs.push(output);
}

addBufferedOutput(output) {
this.#bufferedOutputs.push(output);
}

addAssistantTool(toolClass) {
const assistantTool = new toolClass();
assistantTool.parent = this;
this.experts.push(assistantTool);
if (assistantTool.isParentsTools) {
for (const tool of assistantTool.parentsTools) {
Expand Down Expand Up @@ -141,10 +147,14 @@ class Assistant {
break;
case "tools":
newOutput = this.#expertsOutputs.join("\n\n");
break;
default:
break;
}
}
if (this.#bufferedOutputs.length > 0) {
newOutput = newOutput + "\n\n" + this.#bufferedOutputs.join("\n\n");
}
return newOutput;
}

Expand Down Expand Up @@ -185,6 +195,9 @@ class Assistant {
if (this.#expertsOutputs.length > 0) {
this.#expertsOutputs.length = 0;
}
if (this.#bufferedOutputs.length > 0) {
this.#bufferedOutputs.length = 0;
}
}

// Private (Tool Outputs)
Expand Down
1 change: 1 addition & 0 deletions src/experts/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Tool extends Assistant {
this.hasThread = options.hasThread !== undefined ? options.hasThread : true;
this.outputs = options.outputs || "default";
this.parentsTools = options.parentsTools || [];
this.parent = undefined;
}

get toolName() {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export {
AccountsAssistant,
AccountsTool,
} from "./fixtures/accountsAssistant.js";
export { HtmlAssistant } from "./fixtures/htmlAssistant.js";
1 change: 1 addition & 0 deletions test/fixtures/accountsAssistant.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { helperName } from "../helpers.js";
import { Tool } from "../../src/experts/tool.js";
import { Assistant } from "../../src/experts/assistant.js";

Expand Down
117 changes: 117 additions & 0 deletions test/fixtures/htmlAssistant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { Tool } from "../../src/experts/tool.js";
import { Assistant } from "../../src/experts/assistant.js";
import { helperName } from "../helpers.js";

class MockDataTool extends Tool {
constructor() {
super({
llm: false,
parentsTools: [
{
type: "function",
function: {
name: "data_tool",
description: "Returns data based on the message",
parameters: {
type: "object",
properties: { message: { type: "string" } },
required: ["message"],
},
},
},
],
});
this.toolCallCount = 0;
}

async ask(message) {
const data = {
cows: [
{
name: "Bessie",
age: 3,
milk_production: "27 liters/day",
},
{
name: "Gertie",
age: 5.5,
milk_production: "24 liters/day",
},
],
};
this.toolCallCount++;
return JSON.stringify(data);
}
}

class HtmlTool extends Tool {
constructor() {
super({
llm: false,
parentsTools: [
{
type: "function",
function: {
name: "html_tool",
description: "Format one or more items into HTML components.",
parameters: {
type: "object",
properties: {
items: {
type: "array",
description: "Array of items to render as HTML.",
items: {
type: "object",
properties: {
heading: {
type: "string",
description: "Data for the header tag.",
},
content: {
type: "string",
description: "Basic HTML content.",
},
},
required: ["heading", "content"],
},
},
},
required: ["items"],
},
},
},
],
});
this.toolCallCount = 0;
}

async ask(message) {
const data = JSON.parse(message);
data.items.forEach((item) => {
const renderedTemplate = `<div class="my-Component"><h2>${item.heading}</h2><p>${item.content}</p></div>`;
this.parent.addBufferedOutput(renderedTemplate);
});
this.toolCallCount++;
return "Success. Added HTML to hidden buffered output only see by the use.";
}
}

class HtmlAssistant extends Assistant {
constructor() {
super({
name: helperName("HtmlAssistant"),
instructions: `
## Rules

1. For each user message, use the 'data_tool' first.
2. The use the 'html_tool' to format the submitted outputs of the 'data_tool'.
3. HTML output is hidden to you but assume the user can see it appended to your message.
`,
temperature: 0.1,
});
this.addAssistantTool(MockDataTool);
this.addAssistantTool(HtmlTool);
}
}

export { HtmlAssistant };
18 changes: 18 additions & 0 deletions test/uat/bespokeUIAssistant.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { helperThreadID } from "../helpers.js";
import { HtmlAssistant } from "../fixtures.js";

test("can find an expert that has many tools", async () => {
const assistant = await HtmlAssistant.create();
const dataTool = assistant.experts[0];
const htmlTool = assistant.experts[1];
expect(dataTool.toolCallCount).toBe(0);
expect(htmlTool.toolCallCount).toBe(0);
// Exercise.
const threadID = await helperThreadID();
const output = await assistant.ask("Show me cow data.", threadID);
// Ensure each tool was called once.
expect(dataTool.toolCallCount).toBe(1);
expect(htmlTool.toolCallCount).toBe(1);
// Ensure the output has HTML.
expect(output).toMatch(/class="my-Component"/);
}, 20000);