Replies: 1 comment
-
|
Knowledge support in the NodeJS/TypeScript library is valuable. Here is how to implement it: Knowledge Layer Architectureimport { ChromaClient, Collection } from "chromadb";
interface KnowledgeConfig {
collectionName: string;
embeddingModel?: string;
chunkSize?: number;
chunkOverlap?: number;
}
interface KnowledgeDocument {
id: string;
content: string;
metadata?: Record<string, any>;
}
class Knowledge {
private client: ChromaClient;
private collection: Collection | null = null;
private config: KnowledgeConfig;
constructor(config: KnowledgeConfig) {
this.config = {
chunkSize: 500,
chunkOverlap: 50,
...config
};
this.client = new ChromaClient();
}
async initialize(): Promise<void> {
this.collection = await this.client.getOrCreateCollection({
name: this.config.collectionName
});
}
async addDocument(doc: KnowledgeDocument): Promise<void> {
const chunks = this.chunkText(doc.content);
await this.collection?.add({
ids: chunks.map((_, i) => `${doc.id}_chunk_${i}`),
documents: chunks,
metadatas: chunks.map(() => ({
source_id: doc.id,
...doc.metadata
}))
});
}
async query(queryText: string, nResults = 5): Promise<string[]> {
const results = await this.collection?.query({
queryTexts: [queryText],
nResults
});
return results?.documents?.[0] || [];
}
private chunkText(text: string): string[] {
const chunks: string[] = [];
const { chunkSize, chunkOverlap } = this.config;
for (let i = 0; i < text.length; i += chunkSize! - chunkOverlap!) {
chunks.push(text.slice(i, i + chunkSize!));
}
return chunks;
}
}Integration with PraisonAI Agentimport { Agent } from "praisonai";
const knowledge = new Knowledge({ collectionName: "docs" });
await knowledge.initialize();
await knowledge.addDocument({
id: "doc1",
content: "Your document content..."
});
const agent = new Agent({
name: "KnowledgeAgent",
instructions: "You have access to a knowledge base.",
tools: [
{
name: "search_knowledge",
description: "Search the knowledge base",
execute: async (query: string) => {
const results = await knowledge.query(query);
return results.join("\n\n");
}
}
]
});Key Features to Add
More patterns: https://github.com/KeepALifeUS/autonomous-agents |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I’m working with the PraisonAI Node.js/TypeScript library and want to use the
knowledgeoption.Compare to the python version for NodeJS/TypeScript version I can't find that option.
Anyone run it in TS?
Besides that I would like to:
Basically:
Any idea?
Beta Was this translation helpful? Give feedback.
All reactions