Skip to content

Commit

Permalink
Merge pull request #23 from metaskills/BufferedOutput.v2
Browse files Browse the repository at this point in the history
Streaming Buffered Text Output Support
  • Loading branch information
metaskills authored Aug 5, 2024
2 parents a53e2fe + 627ae20 commit 5eabffb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## v1.4.2

### Added

Streaming support for buffered output added in v1.4.1.

## v1.4.1

### Added
Expand Down
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.1",
"version": "1.4.2",
"description": "An opinionated panel of experts implementation using OpenAI's Assistants API",
"type": "module",
"main": "./src/index.js",
Expand Down
20 changes: 14 additions & 6 deletions src/experts/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { EventEmitter2 } = EventEmitter2Pkg;

const ASYNC_EVENTS = [
"textDoneAsync",
"bufferedTextDoneAsync",
"imageFileDoneAsync",
"runStepDoneAsync",
"toolCallDoneAsync",
Expand All @@ -17,7 +18,7 @@ class Assistant {
#stream;
#streamEmitter;
#expertsOutputs = [];
#bufferedOutputs = [];
#bufferedTextOutputs = [];
#asyncListeners = {};

static async create(options = {}) {
Expand Down Expand Up @@ -105,7 +106,7 @@ class Assistant {
}

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

addAssistantTool(toolClass) {
Expand Down Expand Up @@ -152,8 +153,8 @@ class Assistant {
break;
}
}
if (this.#bufferedOutputs.length > 0) {
newOutput = newOutput + "\n\n" + this.#bufferedOutputs.join("\n\n");
if (this.#bufferedTextOutputs.length > 0) {
newOutput = newOutput + "\n\n" + this.#bufferedTextOutputs.join("\n\n");
}
return newOutput;
}
Expand Down Expand Up @@ -195,8 +196,8 @@ class Assistant {
if (this.#expertsOutputs.length > 0) {
this.#expertsOutputs.length = 0;
}
if (this.#bufferedOutputs.length > 0) {
this.#bufferedOutputs.length = 0;
if (this.#bufferedTextOutputs.length > 0) {
this.#bufferedTextOutputs.length = 0;
}
}

Expand Down Expand Up @@ -250,6 +251,13 @@ class Assistant {
const args = [...arguments, this.#onMetaData];
this.emitter.emit("textDone", ...args);
this.#forwardAsyncEvent("textDoneAsync", ...args);
this.#bufferedTextOutputs.forEach((output) => {
this.emitter.emit(
"bufferedTextDoneAsync",
{ value: output },
this.#onMetaData
);
});
}

#onImageFileDone() {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/accountsAssistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class AccountsTool extends Tool {
class AccountsAssistant extends Assistant {
constructor() {
super({
name: helperName("AccountsAssistant"),
instructions: "Routes messages to the right tool.",
});
this.addAssistantTool(AccountsTool);
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/htmlAssistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class HtmlAssistant extends Assistant {
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'.
1. For each user message, use the 'data_tool' first to find items.
2. For each item from the 'data_tool', use the 'html_tool' to HTML format them.
3. HTML output is hidden to you. Assume the user can see it appended to your message.
`,
temperature: 0.1,
Expand Down
17 changes: 16 additions & 1 deletion test/uat/bespokeUIAssistant.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { helperThreadID } from "../helpers.js";
import { HtmlAssistant } from "../fixtures.js";

test("can find an expert that has many tools", async () => {
test("can attached buffered output to non-streaming output", async () => {
const assistant = await HtmlAssistant.create();
const dataTool = assistant.experts[0];
const htmlTool = assistant.experts[1];
Expand All @@ -16,3 +16,18 @@ test("can find an expert that has many tools", async () => {
// Ensure the output has HTML.
expect(output).toMatch(/class="my-Component"/);
}, 20000);

test("can use streaming events to capture buffered output", async () => {
const dones = [];
const assistant = await HtmlAssistant.create();
assistant.on("bufferedTextDoneAsync", (content) => {
dones.push(content.value);
});
const threadID = await helperThreadID();
await assistant.ask("Show me cow data.", threadID);
expect(dones.length).toBe(2);
expect(dones[0]).toMatch(/class="my-Component"/);
expect(dones[0]).toMatch(/Bessie/i);
expect(dones[1]).toMatch(/class="my-Component"/);
expect(dones[1]).toMatch(/Gertie/i);
}, 20000);

0 comments on commit 5eabffb

Please sign in to comment.