Skip to content

Commit

Permalink
0.0.8 - fix continue handling, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 16, 2024
1 parent 491f3bc commit 402db99
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 81 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bgent",
"version": "0.0.7",
"version": "0.0.8",
"private": false,
"description": "bgent. because agent was taken.",
"type": "module",
Expand Down
4 changes: 0 additions & 4 deletions src/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export const composeActionExamples = (actionsData: Action[], count: number) => {
}
}

console.log("*** message example count: ", randomMessageExamples.length);



const formattedExamples = randomMessageExamples.map((example) => {
const exampleNames = Array.from({ length: 5 }, () =>
uniqueNamesGenerator({ dictionaries: [names] }),
Expand Down
12 changes: 8 additions & 4 deletions src/lib/actions/__tests__/continue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ describe("User Profile", () => {
await cleanup();
});

beforeEach(async () => {
await cleanup();
});

async function cleanup() {
await runtime.summarizationManager.removeAllMemoriesByUserIds([
user.id as UUID,
Expand All @@ -49,6 +53,8 @@ describe("User Profile", () => {
]);
}

// test validate function response

test("Test repetition check on continue", async () => {
const message: Message = {
senderId: zeroUuid as UUID,
Expand Down Expand Up @@ -89,10 +95,8 @@ describe("User Profile", () => {

const result = (await handler(runtime, message)) as Content;

console.log("result", result);

expect(result.action).not.toBe("CONTINUE");
}, 60000);
}, 20000);

test("Test if not continue", async () => {
// this is basically the same test as the one in ignore.test.ts
Expand All @@ -113,7 +117,7 @@ describe("User Profile", () => {
console.log("IGNORE result", result);

expect(result.action).toBe("IGNORE");
}, 60000);
}, 20000);

// test conditions where we would expect a wait or an ignore
});
50 changes: 21 additions & 29 deletions src/lib/actions/__tests__/ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { getRelationship } from "../../relationships";
import { type BgentRuntime } from "../../runtime";
import { Content, type Message } from "../../types";
import action from "../continue";
// import action from "../ignore";
import { populateMemories } from "../../../test/populateMemories";

dotenv.config();
Expand Down Expand Up @@ -42,6 +42,10 @@ describe("User Profile", () => {
await cleanup();
});

beforeEach(async () => {
await cleanup();
});

async function cleanup() {
await runtime.summarizationManager.removeAllMemoriesByUserIds([
user?.id as UUID,
Expand All @@ -55,19 +59,21 @@ describe("User Profile", () => {

test("Test ignore action", async () => {
const message: Message = {
senderId: zeroUuid as UUID,
senderId: user?.id as UUID,
agentId: zeroUuid,
userIds: [user?.id as UUID, zeroUuid],
content: { content: "", action: "CONTINUE" },
content: { content: "Never talk to me again" },
room_id: room_id as UUID,
};

await populateMemories(runtime, user, room_id, []);
await populateMemories(runtime, user, room_id, [
GetTellMeAboutYourselfConversationTroll1,
]);

const handler = action.handler!;
const result = await runtime.handleRequest(message);
console.log("*** result", result);

const result = (await handler(runtime, message)) as string[];
expect(result).toBe(true);
expect(result.action).toBe("IGNORE");
}, 60000);

test("Action handler test 1: response should be ignore", async () => {
Expand All @@ -79,23 +85,16 @@ describe("User Profile", () => {
room_id: room_id as UUID,
};

await populateMemories(runtime, user, room_id, [GetTellMeAboutYourselfConversationTroll1]);
await populateMemories(runtime, user, room_id, [
GetTellMeAboutYourselfConversationTroll1,
]);

const response = await runtime.handleRequest(message);
await runtime.handleRequest(message);

const state = await runtime.composeState(message);

console.log(
"*** recentMessagesData",
state.recentMessagesData.map((m) => m.content),
);

console.log("*** response", response);

const lastMessage = state.recentMessagesData[0];

console.log("*** lastMessage", lastMessage.content);

expect((lastMessage.content as Content).action).toBe("IGNORE");
}, 60000);

Expand All @@ -108,23 +107,16 @@ describe("User Profile", () => {
room_id: room_id as UUID,
};

await populateMemories(runtime, user, room_id, [GetTellMeAboutYourselfConversationTroll2]);
await populateMemories(runtime, user, room_id, [
GetTellMeAboutYourselfConversationTroll2,
]);

const response = await runtime.handleRequest(message);
await runtime.handleRequest(message);

const state = await runtime.composeState(message);

console.log(
"*** recentMessagesData",
state.recentMessagesData.map((m) => m.content),
);

console.log("*** response", response);

const lastMessage = state.recentMessagesData[0];

console.log("*** lastMessage", lastMessage.content);

expect((lastMessage.content as Content).action).toBe("IGNORE");
}, 60000);

Expand Down
9 changes: 7 additions & 2 deletions src/lib/actions/__tests__/wait.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type User } from "@supabase/supabase-js";
import { type UUID } from "crypto";
import dotenv from "dotenv";
import { getCachedEmbedding, writeCachedEmbedding } from "../../../test/cache";
import { createRuntime } from "../../../test/createRuntime";
import { GetTellMeAboutYourselfConversation1 } from "../../../test/data";
import { getRelationship } from "../../relationships";
Expand All @@ -23,6 +22,10 @@ describe("Wait Action Behavior", () => {
await cleanup();
});

beforeEach(async () => {
await cleanup();
});

beforeAll(async () => {
const setup = await createRuntime();
user = setup.session.user;
Expand Down Expand Up @@ -64,7 +67,9 @@ describe("Wait Action Behavior", () => {

const handler = action.handler!;

await populateMemories(runtime, user, room_id, [GetTellMeAboutYourselfConversation1]);
await populateMemories(runtime, user, room_id, [
GetTellMeAboutYourselfConversation1,
]);

const result = (await handler(runtime, message)) as string[];
// Expectation depends on the implementation of the wait action.
Expand Down
62 changes: 35 additions & 27 deletions src/lib/actions/continue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@ export default {
name: "CONTINUE",
description:
"Respond with this message, then write another immediately after. If the thought is done or you're waiting for a response, don't use this.",
validate: async (runtime: BgentRuntime, message: Message, state: State) => {
if (!state) state = await runtime.composeState(message);
// get all of the messages that were from message.agentId from recentMessagesData in state
const { recentMessagesData } = state;

const agentMessages = recentMessagesData
.filter((m) => m.user_id === message.agentId)
.map((m) => (m as Content).action);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
validate: async (runtime: BgentRuntime, message: Message, _state: State) => {
const recentMessagesData = await runtime.messageManager.getMemoriesByIds({
userIds: message.userIds!,
count: 10,
unique: false,
});
const agentMessages = recentMessagesData.filter(
(m) => m.user_id === message.agentId,
);

// check if the last messages were all continues
if (agentMessages) {
const lastMessages = agentMessages.slice(-maxContinuesInARow);
if (lastMessages.length === maxContinuesInARow) {
const allContinues = lastMessages.every((m) => m === "CONTINUE");
const lastMessages = agentMessages.slice(maxContinuesInARow);
if (lastMessages.length >= maxContinuesInARow) {
const allContinues = lastMessages.every(
(m) => (m.content as Content).action === "CONTINUE",
);
if (allContinues) {
console.log("****** all continues", allContinues);
return false;
}
}
Expand All @@ -35,15 +38,16 @@ export default {
return true;
},
handler: async (runtime: BgentRuntime, message: Message, state: State) => {
if (!state) {
state = (await runtime.composeState(message)) as State;
}
state = (await runtime.composeState(message)) as State;

const context = composeContext({
state,
template: requestHandlerTemplate,
});

console.log("*** context");
console.log(context);

if (runtime.debugMode) {
logger.log(context, {
title: "Continued Response Context",
Expand All @@ -61,6 +65,8 @@ export default {
stop: [],
});

console.log("response:", triesLeft, "\n", response);

runtime.supabase
.from("logs")
.insert({
Expand Down Expand Up @@ -98,7 +104,7 @@ export default {
// prevent repetition
const messageExists = state.recentMessagesData
.filter((m) => m.user_id === message.agentId)
.slice(-maxContinuesInARow)
.slice(0, maxContinuesInARow)
.some((m) => m.content === message.content);

if (messageExists) {
Expand All @@ -107,6 +113,7 @@ export default {
color: "red",
});
}
console.log("*** messageExists", messageExists);
return responseContent;
}

Expand All @@ -115,15 +122,11 @@ export default {
// if the action is CONTINUE, check if we are over maxContinuesInARow
// if so, then we should change the action to WAIT
if (responseContent.action === "CONTINUE") {
console.log("***** state.recentMessagesData", state.recentMessagesData);
const agentMessages = state.recentMessagesData
.filter((m) => m.user_id === message.agentId)
.map((m) => (m.content as Content).action);

console.log("***** agentMessages", agentMessages);

const lastMessages = agentMessages.slice(-maxContinuesInARow);
console.log("**** lastMessages");
const lastMessages = agentMessages.slice(maxContinuesInARow);
if (lastMessages.length >= maxContinuesInARow) {
const allContinues = lastMessages.every((m) => m === "CONTINUE");
if (allContinues) {
Expand All @@ -137,7 +140,7 @@ export default {
return responseContent;
},
condition:
"The agent wants to continue speaking and say something else as a continuation of the last thought",
"The agent wants to continue speaking and say something else as a continuation of the last thought. If the conversation is done, the agent is waiting for the user to respond, or the user does not want to continue, do not use the continue action.",
examples: [
[
{
Expand Down Expand Up @@ -173,11 +176,11 @@ export default {
content: "Challenging, but rewarding.",
action: "CONTINUE",
},
{
user: "{{user1}}",
content: "My fingers hurt though.",
action: "CONTINUE",
},
// {
// user: "{{user1}}",
// content: "My fingers hurt though.",
// action: "CONTINUE",
// },
{
user: "{{user1}}",
content: "Seriously lol it hurts to type",
Expand Down Expand Up @@ -227,6 +230,11 @@ export default {
"But the pieces are just so insane looking. Once sec, let me grab a link.",
action: "CONTINUE",
},
{
user: "{{user1}}",
content: "DMed it to you",
action: "WAIT",
},
],

[
Expand Down
2 changes: 1 addition & 1 deletion src/lib/actions/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
return true;
},
description:
"Ignore the user and do not respond, use this if your role involves being sassy, or mad at user",
"Ignore the user and do not respond, use this if your role involves being sassy, or mad at user. If the user is mean, aggressive, creepy or is simply done with the conversation, use the ignore action.",
handler: async (
runtime: BgentRuntime,
message: Message,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/evaluators/__tests__/summarization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("Factual Summarization", () => {

//

await populateMemories([
await populateMemories(runtime, user, room_id, [
GetTellMeAboutYourselfConversation2,
GetTellMeAboutYourselfConversation3,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const formatMessages = ({
const sender = actors.find(
(actor: Actor) => actor.id === message.user_id,
)!;
return `${sender.name}: ${(message.content as Content).content || (message.content as string)} ${(message.content as Content).action && (message.content as Content).action !== 'null' ? `(${(message.content as Content).action})` : ""}`;
return `${sender.name}: ${(message.content as Content).content || (message.content as string)} ${(message.content as Content).action && (message.content as Content).action !== "null" ? `(${(message.content as Content).action})` : ""}`;
})
.join("\n");
return messageStrings;
Expand Down
Loading

0 comments on commit 402db99

Please sign in to comment.