Skip to content

Commit

Permalink
Allow assistant to be found by id, name, then recreated.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Apr 30, 2024
1 parent c876da3 commit 18eb176
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
* Create a beforeInit event/hook vs. an override.
* Can citations be removed from vector store QnA?
* Make sure the assistant find by id works using something like a `process.env.ID`
* Remove the Assistant#messages. Is even `Message` needed?
* TODO: Revisit this and assistantsToolsOutputs.
* Is `addAssistantTool` the right name now?
10 changes: 7 additions & 3 deletions src/experts/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Assistant {

async init() {
if (!this.llm) return;
this.assistant = await this.reCreate();
this.assistant =
(await this.findByID()) ||
(await this.findByName()) ||
(await this.reCreate());
for (const [_name, tool] of Object.entries(this.assistantsTools)) {
await tool.init();
}
Expand Down Expand Up @@ -134,14 +137,15 @@ class Assistant {
async findByID() {
if (!this.id) return;
const assistant = await openai.beta.assistants.retrieve(this.id);
debug(`💁‍♂️ Found by id ${this.agentName} assistant ${this.id}`);
return assistant;
}

async findByName() {
const assistant = (
await openai.beta.assistants.list({ limit: "100" })
).data.find((a) => a.name === this.agentName);
debug(`💁‍♂️ Found assistant: ${this.agentName}`);
debug(`💁‍♂️ Found by name ${this.agentName} assistant`);
return assistant;
}

Expand All @@ -158,7 +162,7 @@ class Assistant {
metadata: this._metadata,
response_format: this.response_format,
});
debug(`💁‍♂️ Created ${this.agentName} assistant ${assistant.id}...`);
debug(`💁‍♂️ Created ${this.agentName} assistant ${assistant.id}`);
return assistant;
}

Expand Down
16 changes: 16 additions & 0 deletions test/experts/assistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import {
import {
TestAssistant,
EchoAssistant,
EchoAssistantWithSetNameAndIdOption,
OddFactsAssistant,
RouterAssistant,
} from "../fixtures.js";

beforeEach(() => {
delete process.env.TEST_ECHO_TOOL_ID;
});

test("assistant as tool", async () => {
const assistant = await RouterAssistant.create();
const threadID = await helperThreadID();
Expand Down Expand Up @@ -47,6 +52,17 @@ test("can ask the assistant a question using a thread id", async () => {
expect(output).toBe("hello 123");
});

test("can use environment variables to find an assistant by id", async () => {
const assistant = await EchoAssistantWithSetNameAndIdOption.create();
// Will find the same assistant by name.
const assistant2 = await EchoAssistantWithSetNameAndIdOption.create();
expect(assistant.id).toBe(assistant2.id);
// Will find by id.
process.env.TEST_ECHO_TOOL_ID = assistant2.id;
const assistant3 = await EchoAssistantWithSetNameAndIdOption.create();
expect(assistant2.id).toBe(assistant3.id);
});

test("can configure various options", async () => {
const path = helperPath("test/fixtures/data.csv");
const file = await openai.files.create({
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { EchoAssistant } from "./fixtures/echoAssistant.js";
export {
EchoAssistant,
EchoAssistantWithSetNameAndIdOption,
} from "./fixtures/echoAssistant.js";
export { OddFactsAssistant } from "./fixtures/oddFactsAssistant.js";
export { TestAssistant } from "./fixtures/testAssistant.js";
export { DataTool } from "./fixtures/dataTool.js";
Expand Down
13 changes: 12 additions & 1 deletion test/fixtures/echoAssistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ class EchoAssistant extends Assistant {
}
}

export { EchoAssistant };
class EchoAssistantWithSetNameAndIdOption extends Assistant {
constructor() {
const name = helperName("Echo", { rand: false });
const description = "Echo";
const instructions = "Echo the same text back to the user";
super(name, description, instructions, {
id: process.env.TEST_ECHO_TOOL_ID,
});
}
}

export { EchoAssistant, EchoAssistantWithSetNameAndIdOption };
4 changes: 3 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { openai } from "../src/openai.js";

const helperName = (use) => {
const helperName = (use, options = {}) => {
const rand = options.rand !== undefined ? options.rand : true;
const name = `Experts.js (${use})`;
if (!rand) return name;
return `${name} ${Math.random().toString(36).substring(4)}`;
};

Expand Down

0 comments on commit 18eb176

Please sign in to comment.