Skip to content

Commit 6d49f57

Browse files
authored
Merge pull request #600 from bmgalego/add-knowledge
feat: add knowledge to state
2 parents db9a857 + baaccb6 commit 6d49f57

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

packages/core/src/knowledge.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { embeddingZeroVector } from "./memory.ts";
66
import { splitChunks } from "./generation.ts";
77
import elizaLogger from "./logger.ts";
88

9-
async function get(runtime: AgentRuntime, message: Memory): Promise<string[]> {
9+
async function get(runtime: AgentRuntime, message: Memory): Promise<KnowledgeItem[]> {
1010
const processed = preprocess(message.content.text);
1111
elizaLogger.log(`Querying knowledge for: ${processed}`);
1212
const embedding = await embed(runtime, processed);
@@ -36,10 +36,9 @@ async function get(runtime: AgentRuntime, message: Memory): Promise<string[]> {
3636
)
3737
);
3838

39-
const knowledge = knowledgeDocuments
39+
return knowledgeDocuments
4040
.filter((memory) => memory !== null)
41-
.map((memory) => memory.content.text);
42-
return knowledge;
41+
.map((memory) => ({ id: memory.id, content: memory.content }));
4342
}
4443

4544
async function set(

packages/core/src/runtime.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ICacheManager,
3030
IDatabaseAdapter,
3131
IMemoryManager,
32+
KnowledgeItem,
3233
ModelClass,
3334
ModelProviderName,
3435
Plugin,
@@ -952,10 +953,14 @@ Text: ${attachment.text}
952953
.join(" ");
953954
}
954955

956+
957+
const knowledegeData = await knowledge.get(this, message);
958+
955959
const formattedKnowledge = formatKnowledge(
956-
await knowledge.get(this, message)
960+
knowledegeData
957961
);
958962

963+
959964
const initialState = {
960965
agentId: this.agentId,
961966
agentName,
@@ -971,6 +976,7 @@ Text: ${attachment.text}
971976
]
972977
: "",
973978
knowledge: formattedKnowledge,
979+
knowledgeData: knowledegeData,
974980
// Recent interactions between the sender and receiver, formatted as messages
975981
recentMessageInteractions: formattedMessageInteractions,
976982
// Recent interactions between the sender and receiver, formatted as posts
@@ -1097,7 +1103,7 @@ Text: ${attachment.text}
10971103
? addHeader("# Attachments", formattedAttachments)
10981104
: "",
10991105
...additionalKeys,
1100-
};
1106+
} as State;
11011107

11021108
const actionPromises = this.actions.map(async (action: Action) => {
11031109
const result = await action.validate(this, message, initialState);
@@ -1235,6 +1241,6 @@ Text: ${attachment.text}
12351241
}
12361242
}
12371243

1238-
const formatKnowledge = (knowledge: string[]) => {
1239-
return knowledge.map((knowledge) => `- ${knowledge}`).join("\n");
1244+
const formatKnowledge = (knowledge: KnowledgeItem[]) => {
1245+
return knowledge.map((knowledge) => `- ${knowledge.content.text}`).join("\n");
12401246
};

packages/core/src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ export interface State {
294294
/** Optional formatted conversation */
295295
formattedConversation?: string;
296296

297+
/** Optional formatted knowledge */
298+
knowledge?: string,
299+
/** Optional knowledge data */
300+
knowledgeData?: KnowledgeItem[],
301+
297302
/** Additional dynamic properties */
298303
[key: string]: unknown;
299304
}

0 commit comments

Comments
 (0)