-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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: base-ragsearch-plugin #944
Conversation
@alextitonis I just reviewed this PR, and I have a suggestion: This implementation seems more suited to being an Adapter, similar to existing ones like PostgreSQL, Supabase, or SQL Adapters. Could you refactor it into an Adapter with all the relevant functions required for seamless integration? Additionally, to ensure usability and proper testing, could you include an example demonstrating how this Adapter can be used with an Agent Client for testing purposes? This will help verify the implementation and provide a practical use case. |
It's not meant to be used as a normal database as it's using a graph for storing data, but as an extra layer, that's why i made it like that. |
Looks great to me! Could you integrate this plugin with the Agent Client, following the same approach used for other plugin integrations? |
Tried to follow the other plugins, is there something wrong? can fix it. |
Hi @alextitonis, Could you include the plugin initialization in agent/src/index.ts, similar to how the other plugins have been integrated? This will ensure consistency across the project. Also, it might be a good idea to define some actions so your plugin can be utilized effectively. By attaching the plugin to specific actions, it can seamlessly integrate into the workflow. Let me know if you’d like any support with this! |
3163ce2
to
b8b9ae9
Compare
Rebased to developer and added initialization, would be perfect if you could help with the action example |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add actions to make it work!
@wtfsayo @samarth30 added an action |
@coderabbitai review |
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThe pull request introduces a new Changes
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 11
🧹 Nitpick comments (5)
packages/plugin-raggraph/src/provider.ts (1)
18-19
: Remove unusedthis.cache
propertyThe
this.cache
property is initialized but never used. Remove it to clean up the code.- private cache: NodeCache;
- this.cache = new NodeCache({ stdTTL: 300 }); // Cache TTL set to 5 minutes
Also applies to: 28-28
packages/plugin-raggraph/src/driver.ts (1)
164-190
: Centralize error handling inquery
functionInconsistent error handling across functions. Centralize error handling to improve maintainability and readability.
Consider creating an error handling wrapper function or middleware.
packages/plugin-raggraph/tsup.config.ts (1)
8-8
: Fix misleading comment about CommonJS.The comment mentions CommonJS but the format is set to ESM only.
- format: ["esm"], // Ensure you're targeting CommonJS + format: ["esm"], // Using ESM format for modern module supportpackages/plugin-raggraph/src/environment.ts (1)
4-8
: Enhance URI validation.Consider adding URL format validation for NEO4J_URI.
- NEO4J_URI: z.string(), + NEO4J_URI: z.string().url().startsWith('neo4j://'),packages/plugin-raggraph/src/types.ts (1)
1-1
: Consider stricter typing for additional properties.Instead of disabling eslint and using
any
, consider using a more specific type for additional properties.-/* eslint-disable @typescript-eslint/no-explicit-any */ - [key: string]: any; + metadata?: Record<string, string | number | boolean>;Also applies to: 14-14
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
agent/src/index.ts
(2 hunks)packages/plugin-raggraph/.npmignore
(1 hunks)packages/plugin-raggraph/eslint.config.mjs
(1 hunks)packages/plugin-raggraph/package.json
(1 hunks)packages/plugin-raggraph/src/actions.ts
(1 hunks)packages/plugin-raggraph/src/driver.ts
(1 hunks)packages/plugin-raggraph/src/environment.ts
(1 hunks)packages/plugin-raggraph/src/graphRagError.ts
(1 hunks)packages/plugin-raggraph/src/index.ts
(1 hunks)packages/plugin-raggraph/src/provider.ts
(1 hunks)packages/plugin-raggraph/src/types.ts
(1 hunks)packages/plugin-raggraph/tsconfig.json
(1 hunks)packages/plugin-raggraph/tsup.config.ts
(1 hunks)
✅ Files skipped from review due to trivial changes (3)
- packages/plugin-raggraph/eslint.config.mjs
- packages/plugin-raggraph/tsconfig.json
- packages/plugin-raggraph/.npmignore
🧰 Additional context used
🪛 GitHub Actions: smoke-test
packages/plugin-raggraph/package.json
[error] Dependency '@ai16z/eliza' is listed in dependencies but not present in the workspace
[warning] node_modules missing while local package.json exists. Run package installation
🔇 Additional comments (5)
packages/plugin-raggraph/src/provider.ts (1)
169-177
: Validate Neo4j settings before useEnsure that
NEO4J_URI
,NEO4J_USER
, andNEO4J_PASSWORD
are defined and valid before initializing the provider to prevent runtime errors.Add validation logic:
const neo4jUri = runtime.getSetting("NEO4J_URI"); const neo4jUser = runtime.getSetting("NEO4J_USER"); const neo4jPassword = runtime.getSetting("NEO4J_PASSWORD"); + if (!neo4jUri || !neo4jUser || !neo4jPassword) { + throw new Error("Missing Neo4j configuration settings"); + }packages/plugin-raggraph/src/graphRagError.ts (1)
1-9
: LGTM!The
GraphRAGError
class is implemented correctly, providing a custom error type with a code.packages/plugin-raggraph/src/index.ts (1)
4-10
: LGTM on plugin structure.Plugin follows the standard interface with clear name and description.
packages/plugin-raggraph/src/types.ts (1)
33-42
: Well-documented relationship types.The DocumentRelationType enum provides clear and comprehensive relationship definitions.
agent/src/index.ts (1)
869-873
: LGTM!The plugin initialization follows the established pattern and correctly checks for required environment variables.
private async readFromCache<T>(key: string): Promise<T | null> { | ||
const cached = await this.cacheManager.get<T>( | ||
path.join(this.cacheKey, key) | ||
); | ||
return cached; | ||
} | ||
|
||
private async writeToCache<T>(key: string, data: T): Promise<void> { | ||
await this.cacheManager.set(path.join(this.cacheKey, key), data, { | ||
expires: Date.now() + 5 * 60 * 1000, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent caching implementation
The methods readFromCache
and writeToCache
use cacheManager
, whereas this.cache
is unused. Ensure consistent use of caching mechanisms.
Consider using this.cacheManager
consistently or remove unnecessary caching components.
await this.graphRAG.createVectorIndex(); | ||
} catch (error) { | ||
console.error("Error creating vector index:", error); | ||
throw error; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle sensitive information carefully in error logs
Logging errors with console.error
may expose sensitive information, such as database credentials or user data. Replace console.error
with a logging framework that can control log levels and redact sensitive data.
Apply this change to replace console.error
with a secure logging mechanism:
-import { createGraphRAG } from "./driver";
+import { createGraphRAG } from "./driver";
+import logger from "your-logging-library";
And update error logging:
- console.error("Error message:", error);
+ logger.error("Error message:", { error });
Also applies to: 91-95, 100-104, 115-119, 124-128, 137-141, 150-154, 169-171
async query(userQuery: string): Promise<GraphRAGResponse> { | ||
const cacheKey = `query-${userQuery}`; | ||
|
||
// Try to get from cache first | ||
const cached = await this.readFromCache<GraphRAGResponse>(cacheKey); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
// If not in cache, perform the query | ||
const response = await this.graphRAG.query(userQuery); | ||
|
||
// Cache the result | ||
await this.writeToCache(cacheKey, response); | ||
|
||
return response; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve error handling in query
method
The query
method lacks error handling for this.graphRAG.query(userQuery)
. Wrap it in a try-catch block to handle potential exceptions.
// If not in cache, perform the query
- const response = await this.graphRAG.query(userQuery);
+ let response;
+ try {
+ response = await this.graphRAG.query(userQuery);
+ } catch (error) {
+ // Handle error appropriately
+ throw new Error(`Graph query failed: ${error.message}`);
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
async query(userQuery: string): Promise<GraphRAGResponse> { | |
const cacheKey = `query-${userQuery}`; | |
// Try to get from cache first | |
const cached = await this.readFromCache<GraphRAGResponse>(cacheKey); | |
if (cached) { | |
return cached; | |
} | |
// If not in cache, perform the query | |
const response = await this.graphRAG.query(userQuery); | |
// Cache the result | |
await this.writeToCache(cacheKey, response); | |
return response; | |
} | |
async query(userQuery: string): Promise<GraphRAGResponse> { | |
const cacheKey = `query-${userQuery}`; | |
// Try to get from cache first | |
const cached = await this.readFromCache<GraphRAGResponse>(cacheKey); | |
if (cached) { | |
return cached; | |
} | |
// If not in cache, perform the query | |
let response; | |
try { | |
response = await this.graphRAG.query(userQuery); | |
} catch (error) { | |
// Handle error appropriately | |
throw new Error(`Graph query failed: ${error.message}`); | |
} | |
// Cache the result | |
await this.writeToCache(cacheKey, response); | |
return response; | |
} |
const setClause = [ | ||
updates.title ? "n.title = $title" : null, | ||
updates.content ? "n.content = $content" : null, | ||
embedding ? "n.embedding = $embedding" : null, | ||
] | ||
.filter(Boolean) | ||
.join(", "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent dynamic query construction risks
Building the setClause
dynamically can introduce injection vulnerabilities. Refactor to use parameterized queries without dynamic query parts.
Refactor the update logic:
- const setClause = [
- updates.title ? "n.title = $title" : null,
- updates.content ? "n.content = $content" : null,
- embedding ? "n.embedding = $embedding" : null,
- ].filter(Boolean).join(", ");
await session.run(
`
MATCH (n:Document {id: $id})
- SET ${setClause}
+ SET n += $updates
`,
{
id,
- ...updates,
- embedding,
+ updates: {
+ ...(updates.title && { title: updates.title }),
+ ...(updates.content && { content: updates.content }),
+ ...(embedding && { embedding }),
+ },
}
);
Also applies to: 303-313
MATCH path = (start)-[*1..${depth}]-(related) | ||
WHERE id(start) IN $nodeIds | ||
RETURN path | ||
`, | ||
{ nodeIds } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid query injection vulnerabilities
Interpolating depth
directly into the Cypher query can lead to injection risks. Parameterize depth
instead of using string interpolation.
- MATCH path = (start)-[*1..${depth}]-(related)
+ MATCH path = (start)-[*1..$depth]-(related)
And pass depth
as a parameter:
`,
{ nodeIds }
+ { nodeIds, depth }
Committable suggestion skipped: line range outside the PR's diff.
actions: [], | ||
evaluators: [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Import and add searchAction to the plugin
- Add
import { searchAction } from './actions'
- Update actions array:
actions: [searchAction]
🔗 Analysis chain
Add the search action mentioned in PR comments.
The actions array is empty, but PR comments indicate that a search action was implemented.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for any action implementations in the codebase
rg -t ts "searchAction" packages/plugin-raggraph/
Length of output: 127
Script:
#!/bin/bash
# Check the action implementation and imports
cat packages/plugin-raggraph/src/actions.ts
cat packages/plugin-raggraph/src/index.ts
Length of output: 2944
export const searchTemplate = `Respond with a JSON object containing the information that you want to search with. | ||
|
||
Example response: | ||
\`\`\`json | ||
{ | ||
"reasoning": "Why you selected this query", | ||
"query": "The query you want to search with" | ||
} | ||
\`\`\` | ||
{{recentMessages}} | ||
Respond with a JSON markdown block containing both city and country.`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix inconsistency in search template example.
The example response mentions "city" and "country", but the actual implementation uses "query" and "reasoning".
Apply this diff to fix the template:
-Respond with a JSON markdown block containing both city and country.`;
+Respond with a JSON markdown block containing both query and reasoning.`;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
export const searchTemplate = `Respond with a JSON object containing the information that you want to search with. | |
Example response: | |
\`\`\`json | |
{ | |
"reasoning": "Why you selected this query", | |
"query": "The query you want to search with" | |
} | |
\`\`\` | |
{{recentMessages}} | |
Respond with a JSON markdown block containing both city and country.`; | |
export const searchTemplate = `Respond with a JSON object containing the information that you want to search with. | |
Example response: | |
\`\`\`json | |
{ | |
"reasoning": "Why you selected this query", | |
"query": "The query you want to search with" | |
} | |
\`\`\` | |
{{recentMessages}} | |
Respond with a JSON markdown block containing both query and reasoning.`; |
elizaLogger.success( | ||
`Successfully fetched weather for ${content.city}, ${content.country}` | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix incorrect log message.
The log message references non-existent variables city
and country
.
Apply this diff to fix the log message:
- `Successfully fetched weather for ${content.city}, ${content.country}`
+ `Successfully executed query: ${content.query}`
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
elizaLogger.success( | |
`Successfully fetched weather for ${content.city}, ${content.country}` | |
); | |
elizaLogger.success( | |
`Successfully executed query: ${content.query}` | |
); |
"type": "module", | ||
"types": "dist/index.d.ts", | ||
"dependencies": { | ||
"@ai16z/eliza": "workspace:*", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix workspace dependency resolution.
The pipeline failure indicates that @ai16z/eliza
workspace dependency is not found.
Apply this diff to use a published version instead:
- "@ai16z/eliza": "workspace:*",
+ "@ai16z/eliza": "^0.1.5-alpha.5",
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
"@ai16z/eliza": "workspace:*", | |
"@ai16z/eliza": "^0.1.5-alpha.5", |
🧰 Tools
🪛 GitHub Actions: smoke-test
[error] Dependency '@ai16z/eliza' is listed in dependencies but not present in the workspace
[warning] node_modules missing while local package.json exists. Run package installation
"dependencies": { | ||
"@ai16z/eliza": "workspace:*", | ||
"tsup": "8.3.5", | ||
"neo4j-driver": "^5.26.0" | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing dev dependencies.
The package uses TypeScript and ESLint but they are not listed in devDependencies.
Add the following dependencies:
"dependencies": {
"@ai16z/eliza": "^0.1.5-alpha.5",
"tsup": "8.3.5",
"neo4j-driver": "^5.26.0"
- },
+ },
+ "devDependencies": {
+ "typescript": "^5.3.3",
+ "eslint": "^8.56.0"
+ },
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
"dependencies": { | |
"@ai16z/eliza": "workspace:*", | |
"tsup": "8.3.5", | |
"neo4j-driver": "^5.26.0" | |
}, | |
"dependencies": { | |
"@ai16z/eliza": "workspace:*", | |
"tsup": "8.3.5", | |
"neo4j-driver": "^5.26.0" | |
}, | |
"devDependencies": { | |
"typescript": "^5.3.3", | |
"eslint": "^8.56.0" | |
}, |
🧰 Tools
🪛 GitHub Actions: smoke-test
[error] Dependency '@ai16z/eliza' is listed in dependencies but not present in the workspace
[warning] node_modules missing while local package.json exists. Run package installation
Relates to:
Risks
Low, it's an external plugin
Background
What does this PR do?
Adds support for neo4j, users can add documents etc and do rag graph using the provider
What kind of change is this?
Documentation changes needed?
Testing
You can add the provider and try adding some docs and then use the query
Where should a reviewer start?
Detailed testing steps