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 data visualization for Anthropic #432

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open

Conversation

Vasilije1990
Copy link
Contributor

@Vasilije1990 Vasilije1990 commented Jan 10, 2025

Summary by CodeRabbit

  • New Features

    • Added visualization tool for knowledge graph.
    • Implemented ability to export graph visualization as PNG image.
    • Enhanced server's image processing capabilities.
  • Improvements

    • Added functionality to retrieve the most recent PNG file.
    • Integrated image handling with PIL library.

Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

Walkthrough

The pull request introduces enhanced image processing and visualization capabilities for the Cognee knowledge graph system. The changes span two files: cognee_mcp/server.py and cognee/shared/utils.py. The modifications enable users to visualize the knowledge graph, save graph visualizations as PNG files, and retrieve the most recently created visualization image. The implementation adds a new "visualize" tool to the server and extends the graph visualization function to export PNG files with timestamped filenames.

Changes

File Change Summary
cognee_mcp/server.py - Added get_freshest_png() function to retrieve the latest PNG file
- Introduced "visualize" tool in handle_list_tools
- Updated handle_call_tool to support visualization
cognee/shared/utils.py - Imported export_png from bokeh.io
- Added timestamp-based PNG export in create_cognee_style_network_with_logo()

Sequence Diagram

sequenceDiagram
    participant User
    participant Server
    participant Cognee
    participant FileSystem

    User->>Server: Request graph visualization
    Server->>Cognee: Call visualize method
    Cognee->>FileSystem: Generate PNG visualization
    FileSystem-->>Cognee: Save PNG with timestamp
    Cognee-->>Server: Return visualization image
    Server-->>User: Display visualization
Loading

Possibly related PRs

Suggested labels

run-checks

Suggested reviewers

  • borisarzentar
  • hajdul88

Poem

🐰 A rabbit's tale of graphs so bright,
Visualizing knowledge with pure delight,
PNG images, fresh and new,
Timestamps dancing, a graphing breakthrough!
Cognee's magic, now clear to see! 🖼️

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Vasilije1990 Vasilije1990 changed the title Add data visualization for Anthropic feat: Add data visualization for Anthropic Jan 10, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e983c21 and daf2d54.

📒 Files selected for processing (2)
  • cognee-mcp/cognee_mcp/server.py (3 hunks)
  • cognee/shared/utils.py (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: ruff format
cognee-mcp/cognee_mcp/server.py

[warning] File requires formatting. Code does not conform to Ruff formatting standards.

🔇 Additional comments (1)
cognee-mcp/cognee_mcp/server.py (1)

92-101: LGTM: Tool definition looks good

The "visualize" tool is properly defined with a clear description and appropriate schema.

🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff formatting standards.

Comment on lines +13 to +14
from PIL import Image
from PIL import Image as PILImage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove duplicate PIL Image import

The Image module from PIL is imported twice with different aliases. This is redundant and could cause confusion.

-from PIL import Image
-from PIL import Image as PILImage
+from PIL import Image
📝 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.

Suggested change
from PIL import Image
from PIL import Image as PILImage
from PIL import Image
🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff formatting standards.

cognee-mcp/cognee_mcp/server.py Outdated Show resolved Hide resolved
cognee-mcp/cognee_mcp/server.py Outdated Show resolved Hide resolved
Comment on lines +447 to +454
# Get the latest Unix timestamp as an integer
timestamp = int(time.time())

# Construct your filename
filename = f"{timestamp}.png"

export_png(p, filename=filename)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for PNG export

The PNG export functionality lacks error handling and cleanup of old files.

     # Get the latest Unix timestamp as an integer
     timestamp = int(time.time())

     # Construct your filename
     filename = f"{timestamp}.png"

-    export_png(p, filename=filename)
+    try:
+        # Cleanup old PNG files to prevent disk space issues
+        cleanup_old_pngs(directory=".", keep_latest=5)
+        
+        # Export the new PNG
+        export_png(p, filename=filename)
+    except Exception as e:
+        logging.error(f"Failed to export PNG: {str(e)}")
+        raise

Consider adding a helper function to cleanup old PNG files:

def cleanup_old_pngs(directory: str, keep_latest: int = 5):
    """Cleanup old PNG files, keeping only the N latest files."""
    png_files = [f for f in os.listdir(directory) if f.endswith('.png')]
    if len(png_files) <= keep_latest:
        return
        
    # Sort by timestamp in filename
    sorted_files = sorted(png_files, key=lambda x: int(x.replace(".png", "")))
    
    # Remove older files
    for f in sorted_files[:-keep_latest]:
        try:
            os.remove(os.path.join(directory, f))
        except OSError as e:
            logging.warning(f"Failed to remove old PNG file {f}: {str(e)}")

Vasilije1990 and others added 2 commits January 11, 2025 16:57
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🔭 Outside diff range comments (1)
cognee-mcp/cognee_mcp/server.py (1)

Fix import statements organization

The file has the following formatting issues:

  • Remove duplicate PIL import: from PIL import Image as PILImage is redundant since Image is already imported
  • Organize imports according to PEP 8 style guide:
    1. Standard library imports
    2. Third-party imports
    3. Local application imports
🔗 Analysis chain

Line range hint 1-230: Fix formatting issues

The file requires formatting according to Ruff's standards. Please run the formatter:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

ruff format cognee-mcp/cognee_mcp/server.py

Length of output: 140


Script:

#!/bin/bash
# Check for basic formatting patterns
echo "=== Checking import statements ==="
rg "^import|^from.*import" cognee-mcp/cognee_mcp/server.py

echo -e "\n=== Checking blank lines between functions ==="
rg "^def|^async def" -A 1 cognee-mcp/cognee_mcp/server.py

echo -e "\n=== Checking indentation ==="
rg "^[[:space:]]+[^[:space:]]" cognee-mcp/cognee_mcp/server.py

Length of output: 8158

🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

🧹 Nitpick comments (1)
cognee-mcp/cognee_mcp/server.py (1)

105-130: Add docstring to document timestamp format requirement

The function assumes PNG files are named with timestamps but this requirement isn't documented.

 def get_freshest_png(directory: str) -> Image.Image:
+    """
+    Retrieve the most recently created PNG file from a directory.
+    
+    Args:
+        directory (str): Path to directory containing PNG files
+        
+    Returns:
+        Image.Image: PIL Image object of the most recent PNG
+        
+    Raises:
+        FileNotFoundError: If directory doesn't exist or no PNG files found
+        ValueError: If PNG filenames don't follow timestamp format (e.g., '1673185134.png')
+        IOError: If PNG file can't be opened
+        
+    Note:
+        PNG files must be named with Unix timestamps (e.g., '1673185134.png')
+    """
     if not os.path.exists(directory):
🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between daf2d54 and 7b0bfe9.

📒 Files selected for processing (1)
  • cognee-mcp/cognee_mcp/server.py (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: ruff format
cognee-mcp/cognee_mcp/server.py

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: run_notebook_test / test
  • GitHub Check: run_notebook_test / test
  • GitHub Check: run_notebook_test / test
  • GitHub Check: docker-compose-test
🔇 Additional comments (1)
cognee-mcp/cognee_mcp/server.py (1)

13-14: Remove duplicate PIL Image import

The Image module from PIL is imported twice with different aliases. This is redundant and could cause confusion.

-from PIL import Image
-from PIL import Image as PILImage
+from PIL import Image
🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

with open(os.devnull, "w") as fnull:
with redirect_stdout(fnull), redirect_stderr(fnull):
try:
await cognee.visualize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix suspicious await on property access

The line await cognee.visualize looks incorrect. Properties typically don't need await. This might be a typo where the parentheses for method invocation are missing.

-                    await cognee.visualize
+                    await cognee.visualize()
📝 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.

Suggested change
await cognee.visualize
await cognee.visualize()
🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

with redirect_stdout(fnull), redirect_stderr(fnull):
try:
await cognee.visualize
img = get_freshest_png(".")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Use configured output directory instead of current directory

Using "." as the directory path is brittle and depends on the current working directory when the server starts. Consider using a configured output directory.

-                    img = get_freshest_png(".")
+                    img = get_freshest_png(cognee.config.visualization_output_dir)

You'll need to ensure the output directory is properly configured in the Cognee configuration.

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

Comment on lines +92 to +101
types.Tool(
name="visualize",
description="Visualize the knowledge graph.",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string"},
},
},
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Clarify query parameter requirement in visualization tool schema

The tool's schema defines a "query" property but doesn't mark it as required. Either:

  1. Mark it as required if visualization needs a query parameter
  2. Document that it's optional if visualization can work without a query
         types.Tool(
             name="visualize",
             description="Visualize the knowledge graph.",
             inputSchema={
                 "type": "object",
                 "properties": {
                     "query": {"type": "string"},
                 },
+                "required": ["query"],  # Add this if query is required
             },
         ),
📝 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.

Suggested change
types.Tool(
name="visualize",
description="Visualize the knowledge graph.",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string"},
},
},
),
types.Tool(
name="visualize",
description="Visualize the knowledge graph.",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string"},
},
"required": ["query"], # Add this if query is required
},
),
🧰 Tools
🪛 GitHub Actions: ruff format

[warning] File requires formatting. Code does not conform to Ruff's formatting standards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant