Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add knowledge to state #600

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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