@@ -14,8 +14,8 @@ import {
14
14
} from "./evaluators.ts" ;
15
15
import { generateText } from "./generation.ts" ;
16
16
import { formatGoalsAsString , getGoals } from "./goals.ts" ;
17
- import { elizaLogger , embed , splitChunks } from "./index.ts" ;
18
- import { embeddingZeroVector , MemoryManager } from "./memory.ts" ;
17
+ import { elizaLogger } from "./index.ts" ;
18
+ import { MemoryManager } from "./memory.ts" ;
19
19
import { formatActors , formatMessages , getActorDetails } from "./messages.ts" ;
20
20
import { parseJsonArrayFromText } from "./parsing.ts" ;
21
21
import { formatPosts } from "./posts.ts" ;
@@ -44,6 +44,7 @@ import {
44
44
} from "./types.ts" ;
45
45
import { stringToUuid } from "./uuid.ts" ;
46
46
import { v4 as uuidv4 } from "uuid" ;
47
+ import knowledge from "./knowledge.ts" ;
47
48
48
49
/**
49
50
* Represents the runtime environment for an agent, handling message processing,
@@ -222,11 +223,21 @@ export class AgentRuntime implements IAgentRuntime {
222
223
opts . character ?. id ??
223
224
opts ?. agentId ??
224
225
stringToUuid ( opts . character ?. name ?? uuidv4 ( ) ) ;
226
+ this . character = opts . character || defaultCharacter ;
227
+
228
+ // By convention, we create a user and room using the agent id.
229
+ // Memories related to it are considered global context for the agent.
230
+ this . ensureRoomExists ( this . agentId ) ;
231
+ this . ensureUserExists (
232
+ this . agentId ,
233
+ this . character . name ,
234
+ this . character . name
235
+ ) ;
236
+ this . ensureParticipantExists ( this . agentId , this . agentId ) ;
225
237
226
238
elizaLogger . success ( "Agent ID" , this . agentId ) ;
227
239
228
240
this . fetch = ( opts . fetch as typeof fetch ) ?? this . fetch ;
229
- this . character = opts . character || defaultCharacter ;
230
241
if ( ! opts . databaseAdapter ) {
231
242
throw new Error ( "No database adapter provided" ) ;
232
243
}
@@ -348,60 +359,28 @@ export class AgentRuntime implements IAgentRuntime {
348
359
* then chunks the content into fragments, embeds each fragment, and creates fragment memories.
349
360
* @param knowledge An array of knowledge items containing id, path, and content.
350
361
*/
351
- private async processCharacterKnowledge ( knowledge : string [ ] ) {
352
- // ensure the room exists and the agent exists in the room
353
- await this . ensureRoomExists ( this . agentId ) ;
354
-
355
- await this . ensureUserExists (
356
- this . agentId ,
357
- this . character . name ,
358
- this . character . name
359
- ) ;
360
-
361
- await this . ensureParticipantExists ( this . agentId , this . agentId ) ;
362
-
363
- for ( const knowledgeItem of knowledge ) {
364
- const knowledgeId = stringToUuid ( knowledgeItem ) ;
362
+ private async processCharacterKnowledge ( items : string [ ] ) {
363
+ for ( const item of items ) {
364
+ const knowledgeId = stringToUuid ( item ) ;
365
365
const existingDocument =
366
366
await this . documentsManager . getMemoryById ( knowledgeId ) ;
367
- if ( ! existingDocument ) {
368
- elizaLogger . success (
369
- "Processing knowledge for " ,
370
- this . character . name ,
371
- " - " ,
372
- knowledgeItem . slice ( 0 , 100 )
373
- ) ;
374
- await this . documentsManager . createMemory ( {
375
- embedding : embeddingZeroVector ,
376
- id : knowledgeId ,
377
- agentId : this . agentId ,
378
- roomId : this . agentId ,
379
- userId : this . agentId ,
380
- createdAt : Date . now ( ) ,
381
- content : {
382
- text : knowledgeItem ,
383
- } ,
384
- } ) ;
385
-
386
- const fragments = await splitChunks ( knowledgeItem , 1200 , 200 ) ;
387
- for ( const fragment of fragments ) {
388
- const embedding = await embed ( this , fragment ) ;
389
- await this . knowledgeManager . createMemory ( {
390
- // We namespace the knowledge base uuid to avoid id
391
- // collision with the document above.
392
- id : stringToUuid ( knowledgeId + fragment ) ,
393
- roomId : this . agentId ,
394
- agentId : this . agentId ,
395
- userId : this . agentId ,
396
- createdAt : Date . now ( ) ,
397
- content : {
398
- source : knowledgeId ,
399
- text : fragment ,
400
- } ,
401
- embedding,
402
- } ) ;
403
- }
367
+ if ( existingDocument ) {
368
+ return ;
404
369
}
370
+
371
+ console . log (
372
+ "Processing knowledge for " ,
373
+ this . character . name ,
374
+ " - " ,
375
+ item . slice ( 0 , 100 )
376
+ ) ;
377
+
378
+ await knowledge . set ( this , {
379
+ id : knowledgeId ,
380
+ content : {
381
+ text : item ,
382
+ } ,
383
+ } ) ;
405
384
}
406
385
}
407
386
@@ -935,33 +914,8 @@ Text: ${attachment.text}
935
914
. join ( " " ) ;
936
915
}
937
916
938
- async function getKnowledge (
939
- runtime : AgentRuntime ,
940
- message : Memory
941
- ) : Promise < string [ ] > {
942
- const embedding = await embed ( runtime , message . content . text ) ;
943
-
944
- const memories =
945
- await runtime . knowledgeManager . searchMemoriesByEmbedding (
946
- embedding ,
947
- {
948
- roomId : message . agentId ,
949
- agentId : message . agentId ,
950
- count : 3 ,
951
- }
952
- ) ;
953
-
954
- const knowledge = memories . map ( ( memory ) => memory . content . text ) ;
955
-
956
- return knowledge ;
957
- }
958
-
959
- const formatKnowledge = ( knowledge : string [ ] ) => {
960
- return knowledge . map ( ( knowledge ) => `- ${ knowledge } ` ) . join ( "\n" ) ;
961
- } ;
962
-
963
917
const formattedKnowledge = formatKnowledge (
964
- await getKnowledge ( this , message )
918
+ await knowledge . get ( this , message )
965
919
) ;
966
920
967
921
const initialState = {
@@ -1243,3 +1197,7 @@ Text: ${attachment.text}
1243
1197
} as State ;
1244
1198
}
1245
1199
}
1200
+
1201
+ const formatKnowledge = ( knowledge : string [ ] ) => {
1202
+ return knowledge . map ( ( knowledge ) => `- ${ knowledge } ` ) . join ( "\n" ) ;
1203
+ } ;
0 commit comments