Skip to content

Commit

Permalink
Merge pull request #600 from bmgalego/add-knowledge
Browse files Browse the repository at this point in the history
feat: add knowledge to state
  • Loading branch information
shakkernerd authored Nov 26, 2024
2 parents db9a857 + baaccb6 commit 6d49f57
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
7 changes: 3 additions & 4 deletions packages/core/src/knowledge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { embeddingZeroVector } from "./memory.ts";
import { splitChunks } from "./generation.ts";
import elizaLogger from "./logger.ts";

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

const knowledge = knowledgeDocuments
return knowledgeDocuments
.filter((memory) => memory !== null)
.map((memory) => memory.content.text);
return knowledge;
.map((memory) => ({ id: memory.id, content: memory.content }));
}

async function set(
Expand Down
14 changes: 10 additions & 4 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
ICacheManager,
IDatabaseAdapter,
IMemoryManager,
KnowledgeItem,
ModelClass,
ModelProviderName,
Plugin,
Expand Down Expand Up @@ -952,10 +953,14 @@ Text: ${attachment.text}
.join(" ");
}


const knowledegeData = await knowledge.get(this, message);

const formattedKnowledge = formatKnowledge(
await knowledge.get(this, message)
knowledegeData
);


const initialState = {
agentId: this.agentId,
agentName,
Expand All @@ -971,6 +976,7 @@ Text: ${attachment.text}
]
: "",
knowledge: formattedKnowledge,
knowledgeData: knowledegeData,
// Recent interactions between the sender and receiver, formatted as messages
recentMessageInteractions: formattedMessageInteractions,
// Recent interactions between the sender and receiver, formatted as posts
Expand Down Expand Up @@ -1097,7 +1103,7 @@ Text: ${attachment.text}
? addHeader("# Attachments", formattedAttachments)
: "",
...additionalKeys,
};
} as State;

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

const formatKnowledge = (knowledge: string[]) => {
return knowledge.map((knowledge) => `- ${knowledge}`).join("\n");
const formatKnowledge = (knowledge: KnowledgeItem[]) => {
return knowledge.map((knowledge) => `- ${knowledge.content.text}`).join("\n");
};
5 changes: 5 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ export interface State {
/** Optional formatted conversation */
formattedConversation?: string;

/** Optional formatted knowledge */
knowledge?: string,
/** Optional knowledge data */
knowledgeData?: KnowledgeItem[],

/** Additional dynamic properties */
[key: string]: unknown;
}
Expand Down

0 comments on commit 6d49f57

Please sign in to comment.