Skip to content

Commit 9af6602

Browse files
committed
Merge changes for embedding caching, change dim size to 1536
1 parent 3e1b6ea commit 9af6602

File tree

14 files changed

+145
-33
lines changed

14 files changed

+145
-33
lines changed

docs/docs/classes/BgentRuntime.md

+16
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,19 @@ Register an evaluator to assess and guide the agent's responses.
314314
#### Returns
315315

316316
`void`
317+
318+
___
319+
320+
### retriveCachedEmbedding
321+
322+
**retriveCachedEmbedding**(`input`): `Promise`\<``null`` \| `number`[]\>
323+
324+
#### Parameters
325+
326+
| Name | Type |
327+
| :------ | :------ |
328+
| `input` | `string` |
329+
330+
#### Returns
331+
332+
`Promise`\<``null`` \| `number`[]\>

docs/docs/classes/DatabaseAdapter.md

+22
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,28 @@ ___
183183

184184
___
185185

186+
### getMemoryByContent
187+
188+
**getMemoryByContent**(`«destructured»`): `Promise`\<`SimilaritySearch`[]\>
189+
190+
#### Parameters
191+
192+
| Name | Type |
193+
| :------ | :------ |
194+
| `«destructured»` | `Object` |
195+
| › `query_field_name` | `string` |
196+
| › `query_field_sub_name` | `string` |
197+
| › `query_input` | `string` |
198+
| › `query_match_count` | `number` |
199+
| › `query_table_name` | `string` |
200+
| › `query_threshold` | `number` |
201+
202+
#### Returns
203+
204+
`Promise`\<`SimilaritySearch`[]\>
205+
206+
___
207+
186208
### getRelationship
187209

188210
**getRelationship**(`params`): `Promise`\<``null`` \| [`Relationship`](../interfaces/Relationship.md)\>

docs/docs/classes/MemoryManager.md

+16
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ A Promise resolving to an array of Memory objects.
131131

132132
___
133133

134+
### getMemoryByContent
135+
136+
**getMemoryByContent**(`content`): `Promise`\<`SimilaritySearch`[]\>
137+
138+
#### Parameters
139+
140+
| Name | Type |
141+
| :------ | :------ |
142+
| `content` | `string` |
143+
144+
#### Returns
145+
146+
`Promise`\<`SimilaritySearch`[]\>
147+
148+
___
149+
134150
### removeAllMemoriesByUserIds
135151

136152
**removeAllMemoriesByUserIds**(`userIds`): `Promise`\<`void`\>

docs/docs/classes/SupabaseDatabaseAdapter.md

+26
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,32 @@ ___
230230

231231
___
232232

233+
### getMemoryByContent
234+
235+
**getMemoryByContent**(`opts`): `Promise`\<`SimilaritySearch`[]\>
236+
237+
#### Parameters
238+
239+
| Name | Type |
240+
| :------ | :------ |
241+
| `opts` | `Object` |
242+
| `opts.query_field_name` | `string` |
243+
| `opts.query_field_sub_name` | `string` |
244+
| `opts.query_input` | `string` |
245+
| `opts.query_match_count` | `number` |
246+
| `opts.query_table_name` | `string` |
247+
| `opts.query_threshold` | `number` |
248+
249+
#### Returns
250+
251+
`Promise`\<`SimilaritySearch`[]\>
252+
253+
#### Overrides
254+
255+
[DatabaseAdapter](DatabaseAdapter.md).[getMemoryByContent](DatabaseAdapter.md#getmemorybycontent)
256+
257+
___
258+
233259
### getRelationship
234260

235261
**getRelationship**(`params`): `Promise`\<``null`` \| [`Relationship`](../interfaces/Relationship.md)\>

docs/docs/variables/embeddingDimension.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sidebar_position: 0
66
custom_edit_url: null
77
---
88

9-
`Const` **embeddingDimension**: ``3072``
9+
`Const` **embeddingDimension**: ``1536``

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bgent",
3-
"version": "0.0.45",
3+
"version": "0.0.46",
44
"private": false,
55
"description": "bgent. because agent was taken.",
66
"type": "module",

scripts/shell.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async function startApplication() {
240240
Authorization: 'Bearer ' + session.access_token
241241
},
242242
body: JSON.stringify({
243-
content,
243+
content: { content, action: "WAIT"},
244244
agentId: agentUUID,
245245
room_id
246246
})

src/agents/simple/index.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const onMessage = async (
141141
logger.warn("Sender content null, skipping");
142142
return;
143143
}
144-
144+
145145
const data = (await handleMessage(runtime, message, state)) as Content;
146146
return data;
147147
};
@@ -183,14 +183,7 @@ const routes: Route[] = [
183183
}
184184

185185
// parse the body from the request
186-
const message = await req.json() as {content: any} & Message
187-
// Validate the message content
188-
if (message.hasOwnProperty("content") === false) {
189-
return new Response("content is required", { status: 400 });
190-
} else {
191-
const content = message.content
192-
message.content = {content: content, action: "null"} as Content
193-
}
186+
const message = (await req.json()) as Message;
194187

195188
const databaseAdapter = new SupabaseDatabaseAdapter(
196189
env.SUPABASE_URL,

src/lib/__tests__/memory.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe("Memory", () => {
8989
content: { content: dissimilarMemoryContent },
9090
user_ids: [user?.id as UUID, zeroUuid],
9191
room_id: room_id as UUID,
92-
embedding: getCachedEmbedding(dissimilarMemoryContent),
92+
embedding,
9393
});
9494
if (!embedding) {
9595
writeCachedEmbedding(

src/lib/adapters/supabase.ts

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Actor,
99
GoalStatus,
1010
Account,
11+
SimilaritySearch,
1112
} from "../types";
1213
import { DatabaseAdapter } from "../database";
1314

@@ -62,6 +63,11 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {
6263
match_count: number;
6364
unique: boolean;
6465
}): Promise<Memory[]> {
66+
console.log(
67+
"searching memories",
68+
params.tableName,
69+
params.embedding.length,
70+
);
6571
const result = await this.supabase.rpc("search_memories", {
6672
query_table_name: params.tableName,
6773
query_user_ids: params.userIds,
@@ -76,6 +82,21 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {
7682
return result.data;
7783
}
7884

85+
async getMemoryByContent(opts: {
86+
query_table_name: string;
87+
query_threshold: number;
88+
query_input: string;
89+
query_field_name: string;
90+
query_field_sub_name: string;
91+
query_match_count: number;
92+
}): Promise<SimilaritySearch[]> {
93+
const result = await this.supabase.rpc("get_embedding_list", opts);
94+
if (result.error) {
95+
throw new Error(JSON.stringify(result.error));
96+
}
97+
return result.data;
98+
}
99+
79100
async updateGoalStatus(params: {
80101
goalId: UUID;
81102
status: GoalStatus;

src/lib/database.ts

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Actor,
77
GoalStatus,
88
Account,
9+
SimilaritySearch,
910
} from "./types";
1011

1112
export abstract class DatabaseAdapter {
@@ -19,6 +20,22 @@ export abstract class DatabaseAdapter {
1920
tableName: string;
2021
}): Promise<Memory[]>;
2122

23+
abstract getMemoryByContent({
24+
query_table_name,
25+
query_threshold,
26+
query_input,
27+
query_field_name,
28+
query_field_sub_name,
29+
query_match_count,
30+
}: {
31+
query_table_name: string;
32+
query_threshold: number;
33+
query_input: string;
34+
query_field_name: string;
35+
query_field_sub_name: string;
36+
query_match_count: number;
37+
}): Promise<SimilaritySearch[]>;
38+
2239
abstract log(params: {
2340
body: { [key: string]: unknown };
2441
user_id: UUID;

src/lib/memory.ts

+16-17
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,15 @@ export class MemoryManager {
8484
}
8585

8686
async getMemoryByContent(content: string): Promise<SimilaritySearch[]> {
87-
const opts = {
88-
query_table_name: this.tableName,
89-
query_threshold: 2,
90-
query_input: content,
91-
query_field_name: 'content',
92-
query_field_sub_name: 'content',
93-
query_match_count: 10,
94-
};
95-
96-
if (!this.runtime || 'undefined' === typeof this.runtime.supabase) {
97-
return [];
98-
}
99-
const result = await this.runtime.supabase.rpc("get_embedding_list", opts);
100-
if (result.error) {
101-
throw new Error(JSON.stringify(result.error));
102-
}
103-
return result.data;
87+
const result = await this.runtime.databaseAdapter.getMemoryByContent({
88+
query_table_name: this.tableName,
89+
query_threshold: 2,
90+
query_input: content,
91+
query_field_name: "content",
92+
query_field_sub_name: "content",
93+
query_match_count: 10,
94+
});
95+
return result;
10496
}
10597

10698
/**
@@ -129,6 +121,11 @@ export class MemoryManager {
129121
unique,
130122
} = opts;
131123

124+
console.log("embedding length to search is", embedding.length);
125+
126+
console.log("opts are", opts);
127+
console.log(opts);
128+
132129
const result = await this.runtime.databaseAdapter.searchMemories({
133130
tableName: this.tableName,
134131
userIds: userIds,
@@ -138,6 +135,8 @@ export class MemoryManager {
138135
unique: !!unique,
139136
});
140137

138+
console.log("result.embedding.length", result[0]?.embedding?.length);
139+
141140
return result;
142141
}
143142

src/lib/runtime.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ export class BgentRuntime {
299299
body: JSON.stringify({
300300
input,
301301
model: embeddingModel,
302+
length: 1536,
302303
}),
303304
};
304305
try {
@@ -319,6 +320,8 @@ export class BgentRuntime {
319320

320321
const data: OpenAIEmbeddingResponse = await response.json();
321322

323+
console.log("*** EMBEDDING LENGTH IS", data?.data?.[0].embedding.length);
324+
322325
return data?.data?.[0].embedding;
323326
} catch (e) {
324327
console.error(e);
@@ -327,7 +330,8 @@ export class BgentRuntime {
327330
}
328331

329332
async retriveCachedEmbedding(input: string) {
330-
const similaritySearchResult = await this.messageManager.getMemoryByContent(input);
333+
const similaritySearchResult =
334+
await this.messageManager.getMemoryByContent(input);
331335
if (similaritySearchResult.length > 0) {
332336
return similaritySearchResult[0].embedding;
333337
}

src/lib/types.ts

-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export interface Memory {
4949
room_id: UUID; // The room or conversation ID associated with the memory.
5050
}
5151

52-
5352
/**
5453
* Represents a similarity search result, including the embedding vector and the Levenshtein score for a given search query.
5554
*/
@@ -58,7 +57,6 @@ export interface SimilaritySearch {
5857
levenshtein_score: number;
5958
}
6059

61-
6260
/**
6361
* Represents an objective within a goal, detailing what needs to be achieved and whether it has been completed.
6462
*/

0 commit comments

Comments
 (0)