Skip to content

Commit

Permalink
Switch to Streaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed May 1, 2024
1 parent 18eb176 commit 793f2c0
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 219 deletions.
9 changes: 9 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
* Remove the Assistant#messages. Is even `Message` needed?
* TODO: Revisit this and assistantsToolsOutputs.
* Is `addAssistantTool` the right name now?


```javascript
stream.on("event", (e) => {
if (e.event.startsWith("thread.run")) {
aRun = e.data;
}
});
```
63 changes: 22 additions & 41 deletions src/experts/assistant.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { openai } from "../openai.js";
import { debug, formatToolOutputs } from "../helpers.js";
import { Thread } from "./thread.js";
import { Message } from "./messages.js";
import { Run } from "./run.js";

class Assistant {
Expand All @@ -11,11 +10,6 @@ class Assistant {
return asst;
}

static async delete() {
const asst = new this();
await asst.deleteByName();
}

constructor(agentName, description, instructions, options = {}) {
this.agentName = agentName;
this.description = description;
Expand Down Expand Up @@ -62,15 +56,19 @@ class Assistant {
return this.assistant.metadata;
}

// Messages: Asking and access.
// Interface

async ask(message, threadID) {
return await this.askAssistant(message, threadID);
}

get lastMessageContent() {
return this.messages[0].content;
}
// Run Event Overrides

onEvent(event) {}

onTextDelta(delta, snapshot) {}

onToolCallDelta(delta, snapshot) {}

// Tool Assistant

Expand All @@ -84,56 +82,34 @@ class Assistant {
}
}

// Tool Outputs: Used for response pass-thru.

get isAssistantsToolsOutputs() {
return (
this.assistantsToolsOutputs && this.assistantsToolsOutputs.length > 0
);
}

addAssistantsToolsOutputs(output) {
this.assistantsToolsOutputs.push(output);
}

clearAssistantsToolsOutputs() {
if (this.isAssistantsToolsOutputs) {
this.assistantsToolsOutputs.length = 0;
}
}

// Private

async askAssistant(message, threadID) {
if (!this.llm) return;
this.clearAssistantsToolsOutputs();
let thread = await Thread.find(threadID);
if (this.isTool && this.hasToolThread) {
thread = await thread.toolThread(this);
}
const _msg = await Message.createForAssistant(this, message, thread);
const run = await Run.createForAssistant(this, thread);
let output = await run.actions();
const messageContent =
typeof message === "string" ? message : JSON.stringify(message);
debug("💌 " + JSON.stringify(messageContent));
await openai.beta.threads.messages.create(thread.id, {
role: "user",
content: messageContent,
});
const run = await Run.streamForAssistant(this, thread);
let output = await run.wait();
if (this.isTool) {
if (this.llm && this.ignoreLLMToolOutput) {
output = "";
}
} else {
if (this.isAssistantsToolsOutputs) {
output = formatToolOutputs(this.assistantsToolsOutputs);
}
}
debug(`🤖 ${output}`);
return output;
}

// Private (Lifecycle)

async reCreate() {
const assistant = (await this.deleteByName()) || (await this.create());
return assistant;
}

async findByID() {
if (!this.id) return;
const assistant = await openai.beta.assistants.retrieve(this.id);
Expand All @@ -149,6 +125,11 @@ class Assistant {
return assistant;
}

async reCreate() {
const assistant = (await this.deleteByName()) || (await this.create());
return assistant;
}

async create() {
const assistant = await openai.beta.assistants.create({
model: this.model,
Expand Down
68 changes: 0 additions & 68 deletions src/experts/messages.js

This file was deleted.

Loading

0 comments on commit 793f2c0

Please sign in to comment.