Skip to content

Commit eefb154

Browse files
committed
rag provider
1 parent 1780b71 commit eefb154

File tree

2 files changed

+109
-112
lines changed

2 files changed

+109
-112
lines changed

packages/plugin-raggraph/src/index.ts

+7-112
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,12 @@
1-
import { elizaLogger } from "@ai16z/eliza";
2-
import {
3-
Action,
4-
HandlerCallback,
5-
IAgentRuntime,
6-
Memory,
7-
Plugin,
8-
State,
9-
} from "@ai16z/eliza";
10-
import { createGraphRAG } from "./driver";
11-
import { validateRaggraphConfig } from "./environment";
12-
13-
export async function initializeRAGGraph(runtime: IAgentRuntime) {
14-
const config = await validateRaggraphConfig(runtime);
15-
return createGraphRAG({
16-
neo4jUri: config.NEO4J_URI,
17-
neo4jUser: config.NEO4J_USER,
18-
neo4jPassword: config.NEO4J_PASSWORD,
19-
});
20-
}
21-
22-
const ragQuery: Action = {
23-
name: "RAG_QUERY",
24-
similes: [
25-
"SEARCH_KNOWLEDGE",
26-
"QUERY_GRAPH",
27-
"FIND_RELATED",
28-
"SEARCH_DOCUMENTS",
29-
"RETRIEVE_INFO",
30-
],
31-
description: "Query the knowledge graph for relevant information",
32-
validate: async (runtime: IAgentRuntime, _message: Memory) => {
33-
try {
34-
await validateRaggraphConfig(runtime);
35-
return true;
36-
} catch {
37-
return false;
38-
}
39-
},
40-
handler: async (
41-
runtime: IAgentRuntime,
42-
message: Memory,
43-
state: State,
44-
_options: Record<string, any>,
45-
callback: HandlerCallback
46-
) => {
47-
elizaLogger.log("Composing state for message:", message);
48-
state = (await runtime.composeState(message)) as State;
49-
50-
try {
51-
const rag = await initializeRAGGraph(runtime);
52-
const query = message.content.text;
53-
54-
elizaLogger.log("Executing RAG query:", query);
55-
const result = await rag.query(query);
56-
57-
// Close the connection after query
58-
await rag.close();
59-
60-
callback({
61-
text: "Here's what I found:",
62-
attachments: [
63-
{
64-
id: crypto.randomUUID(),
65-
title: "Search Results",
66-
source: "raggraph",
67-
description: "Knowledge Graph Query Results",
68-
text: result.fullContext,
69-
metadata: {
70-
confidence: result.confidence,
71-
sources: result.sources,
72-
},
73-
},
74-
],
75-
});
76-
} catch (error) {
77-
elizaLogger.error("RAG query failed:", error);
78-
throw error;
79-
}
80-
},
81-
examples: [
82-
[
83-
{
84-
user: "{{user1}}",
85-
content: { text: "What do you know about neural networks?" },
86-
},
87-
{
88-
user: "{{agentName}}",
89-
content: {
90-
text: "Let me search our knowledge base for information about neural networks.",
91-
action: "RAG_QUERY",
92-
},
93-
},
94-
],
95-
[
96-
{
97-
user: "{{user1}}",
98-
content: { text: "Find information about machine learning" },
99-
},
100-
{
101-
user: "{{agentName}}",
102-
content: {
103-
text: "I'll search our knowledge graph for machine learning information.",
104-
action: "RAG_QUERY",
105-
},
106-
},
107-
],
108-
],
109-
} as Action;
1+
import { Plugin } from "@ai16z/eliza";
2+
import { ragGraphProvider } from "./provider";
1103

1114
export const raggraphPlugin: Plugin = {
1125
name: "raggraph",
113-
description: "Knowledge graph querying and retrieval",
114-
actions: [ragQuery],
6+
description: "RAGGraph Plugin for Eliza",
7+
actions: [],
1158
evaluators: [],
116-
providers: [],
9+
providers: [ragGraphProvider],
11710
};
11+
12+
export default raggraphPlugin;
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import {
2+
IAgentRuntime,
3+
ICacheManager,
4+
Memory,
5+
Provider,
6+
State,
7+
} from "@ai16z/eliza";
8+
import { createGraphRAG } from "./driver";
9+
import { GraphRAGResponse } from "./types";
10+
import NodeCache from "node-cache";
11+
import * as path from "path";
12+
13+
const PROVIDER_CONFIG = {
14+
MAX_RETRIES: 3,
15+
RETRY_DELAY: 2000,
16+
};
17+
18+
export class RAGGraphProvider {
19+
private cache: NodeCache;
20+
private cacheKey: string = "raggraph";
21+
private graphRAG;
22+
23+
constructor(
24+
private neo4jUri: string,
25+
private neo4jUser: string,
26+
private neo4jPassword: string,
27+
private cacheManager: ICacheManager
28+
) {
29+
this.cache = new NodeCache({ stdTTL: 300 }); // Cache TTL set to 5 minutes
30+
this.graphRAG = createGraphRAG({
31+
neo4jUri: this.neo4jUri,
32+
neo4jUser: this.neo4jUser,
33+
neo4jPassword: this.neo4jPassword,
34+
});
35+
}
36+
37+
private async readFromCache<T>(key: string): Promise<T | null> {
38+
const cached = await this.cacheManager.get<T>(
39+
path.join(this.cacheKey, key)
40+
);
41+
return cached;
42+
}
43+
44+
private async writeToCache<T>(key: string, data: T): Promise<void> {
45+
await this.cacheManager.set(path.join(this.cacheKey, key), data, {
46+
expires: Date.now() + 5 * 60 * 1000,
47+
});
48+
}
49+
50+
async query(userQuery: string): Promise<GraphRAGResponse> {
51+
const cacheKey = `query-${userQuery}`;
52+
53+
// Try to get from cache first
54+
const cached = await this.readFromCache<GraphRAGResponse>(cacheKey);
55+
if (cached) {
56+
return cached;
57+
}
58+
59+
// If not in cache, perform the query
60+
const response = await this.graphRAG.query(userQuery);
61+
62+
// Cache the result
63+
await this.writeToCache(cacheKey, response);
64+
65+
return response;
66+
}
67+
68+
async close(): Promise<void> {
69+
await this.graphRAG.close();
70+
}
71+
}
72+
73+
const ragGraphProvider: Provider = {
74+
get: async (
75+
runtime: IAgentRuntime,
76+
message: Memory,
77+
_state?: State
78+
): Promise<string | null> => {
79+
try {
80+
const neo4jUri = runtime.getSetting("NEO4J_URI");
81+
const neo4jUser = runtime.getSetting("NEO4J_USER");
82+
const neo4jPassword = runtime.getSetting("NEO4J_PASSWORD");
83+
84+
const provider = new RAGGraphProvider(
85+
neo4jUri,
86+
neo4jUser,
87+
neo4jPassword,
88+
runtime.cacheManager
89+
);
90+
91+
const response = await provider.query(message.content);
92+
await provider.close();
93+
94+
return response.fullContext;
95+
} catch (error) {
96+
console.error("Error in RAG graph provider:", error);
97+
return null;
98+
}
99+
},
100+
};
101+
102+
export { ragGraphProvider };

0 commit comments

Comments
 (0)