Skip to content

Fix Cypher generation iterative loop#37

Merged
swilly22 merged 5 commits into
mainfrom
fix-cypher-loop
Nov 19, 2024
Merged

Fix Cypher generation iterative loop#37
swilly22 merged 5 commits into
mainfrom
fix-cypher-loop

Conversation

@galshubeli
Copy link
Copy Markdown
Collaborator

@galshubeli galshubeli commented Nov 18, 2024

Summary by CodeRabbit

  • New Features

    • Added new terms "openai" and "Ollama" to the word list.
    • Introduced constants for enhanced ontology creation and data extraction processes.
    • Added methods to delete the last message in various chat session classes.
  • Bug Fixes

    • Improved error handling and retry logic in the Graph Query Generation step.
  • Documentation

    • Updated constants and methods to enhance clarity in ontology management.
  • Refactor

    • Restructured attribute initialization in the Orchestrator class for better encapsulation.

@galshubeli galshubeli requested a review from swilly22 November 18, 2024 17:31
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Nov 18, 2024

Walkthrough

This pull request introduces several modifications across multiple files. New entries "openai" and "Ollama" were added to the .wordlist.txt file. Additionally, a series of constants related to ontology creation and data extraction were introduced in graphrag_sdk/fixtures/prompts.py. The delete_last_message method was added to multiple chat session classes in graphrag_sdk/models, and changes were made to the GraphQueryGenerationStep class to alter its retry logic and error handling.

Changes

File Path Change Summary
.wordlist.txt Added new entries: openai, Ollama.
graphrag_sdk/fixtures/prompts.py Added multiple constants for ontology creation and data extraction, including CREATE_ONTOLOGY_SYSTEM, CYPHER_GEN_PROMPT, etc.
graphrag_sdk/models/azure_openai.py Added method: delete_last_message(self) in AzureOpenAiChatSession.
graphrag_sdk/models/gemini.py Added method: delete_last_message(self) in GeminiChatSession.
graphrag_sdk/models/ollama.py Added method: delete_last_message(self) in OllamaChatSession.
graphrag_sdk/models/openai.py Added method: delete_last_message(self) in OpenAiChatSession.
graphrag_sdk/orchestrator/orchestrator.py Moved initialization of _agents and _chat attributes to the constructor.
graphrag_sdk/steps/graph_query_step.py Updated run method's retries parameter default from 5 to 10 and modified the error handling logic.

Possibly related PRs

Suggested reviewers

  • swilly22

Poem

In the meadow where words do play,
"OpenAI" hops in, bright as day.
"Ollama" joins the cheerful throng,
With prompts and constants, we sing along.
A chat session's history, trimmed with care,
In the world of graphs, we dance and share! 🐇✨


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 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.

Copy link
Copy Markdown

@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: 7

🧹 Outside diff range and nitpick comments (7)
.wordlist.txt (1)

28-28: Consider maintaining alphabetical order

While the addition of "Ollama" is appropriate, consider maintaining alphabetical order in the wordlist for better maintainability and easier lookups.

 html
 https
-www
-faq
 Ollama
+www
+faq
graphrag_sdk/orchestrator/orchestrator.py (3)

3-3: Remove unused import OutputMethod

The OutputMethod import is not used anywhere in this file. If it's not needed for future implementation, it should be removed to keep the codebase clean.

-from graphrag_sdk.models.model import OutputMethod
🧰 Tools
🪛 Ruff

3-3: graphrag_sdk.models.model.OutputMethod imported but unused

Remove unused import: graphrag_sdk.models.model.OutputMethod

(F401)


23-24: Add type hints for instance variables

Consider adding type hints to improve code maintainability and IDE support:

-        self._agents = []
-        self._chat = None
+        self._agents: list[Agent] = []
+        self._chat: Optional[ChatSession] = None

Don't forget to add the necessary imports:

from typing import Optional
from graphrag_sdk.models.chat import ChatSession

Line range hint 52-67: Enhance error handling for Cypher generation

Given that this PR aims to fix Cypher generation iterative loop issues, consider implementing more specific error handling:

  1. The current generic exception handling doesn't provide much value as it immediately re-raises the error
  2. Consider catching specific exceptions that might occur during plan creation
     def _create_execution_plan(self, question: str):
         try:
             response = self._get_chat().send_message(
                 ORCHESTRATOR_EXECUTION_PLAN_PROMPT.replace("#QUESTION", question)
             )
 
             logger.debug(f"Execution plan response: {response.text}")
 
             plan = ExecutionPlan.from_json(
                 extract_json(response.text, skip_repair=True)
             )
 
             logger.debug(f"Execution plan: {plan}")
 
             return plan
-        except Exception as e:
-            logger.error(f"Failed to create plan: {e}")
-            raise e
+        except ValueError as e:
+            logger.error(f"Invalid JSON in execution plan response: {e}")
+            raise
+        except KeyError as e:
+            logger.error(f"Missing required field in execution plan: {e}")
+            raise
+        except Exception as e:
+            logger.error(f"Unexpected error creating execution plan: {e}")
+            raise
🧰 Tools
🪛 Ruff

3-3: graphrag_sdk.models.model.OutputMethod imported but unused

Remove unused import: graphrag_sdk.models.model.OutputMethod

(F401)

graphrag_sdk/steps/graph_query_step.py (1)

45-45: Rename unused loop variable.

The loop control variable i is not used within the loop body.

-        for i in range(retries):
+        for _i in range(retries):
🧰 Tools
🪛 Ruff

45-45: Loop control variable i not used within loop body

Rename unused i to _i

(B007)

README.md (1)

62-67: Enhance Azure-OpenAI configuration examples

The Azure-OpenAI configuration section would be more helpful with concrete example values.

-   AZURE_OPENAI_API_KEY="AZURE_OPENAI_API_KEY"
-   AZURE_ENDPOINT="AZURE_ENDPOINT"
-   AZURE_API_VERSION="AZURE_API_VERSION"
+   AZURE_OPENAI_API_KEY="00000000000000000000000000000000"
+   AZURE_ENDPOINT="https://my-resource.openai.azure.com"
+   AZURE_API_VERSION="2023-05-15"
graphrag_sdk/fixtures/prompts.py (1)

444-447: Correct grammatical errors in CYPHER_GEN_PROMPT for clarity

There are grammatical issues in the CYPHER_GEN_PROMPT. Specifically:

  • Use "an OpenCypher statement" instead of "a OpenCypher statement".
  • Rephrase "Please think if your answer is a valid Cypher query, and correct it if it is not." to "Please ensure that your answer is a valid Cypher query, and correct it if it is not." for better clarity.

Apply this diff to correct the grammatical errors:

-If you cannot generate a OpenCypher statement for any reason, return an empty string.
+If you cannot generate an OpenCypher statement for any reason, return an empty string.

-Please think if your answer is a valid Cypher query, and correct it if it is not.
+Please ensure that your answer is a valid Cypher query, and correct it if it is not.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ca3aa2f and 90b37cc.

📒 Files selected for processing (9)
  • .wordlist.txt (2 hunks)
  • README.md (2 hunks)
  • graphrag_sdk/fixtures/prompts.py (1 hunks)
  • graphrag_sdk/models/azure_openai.py (1 hunks)
  • graphrag_sdk/models/gemini.py (1 hunks)
  • graphrag_sdk/models/ollama.py (1 hunks)
  • graphrag_sdk/models/openai.py (1 hunks)
  • graphrag_sdk/orchestrator/orchestrator.py (2 hunks)
  • graphrag_sdk/steps/graph_query_step.py (2 hunks)
🧰 Additional context used
🪛 Ruff
graphrag_sdk/orchestrator/orchestrator.py

3-3: graphrag_sdk.models.model.OutputMethod imported but unused

Remove unused import: graphrag_sdk.models.model.OutputMethod

(F401)

graphrag_sdk/steps/graph_query_step.py

45-45: Loop control variable i not used within loop body

Rename unused i to _i

(B007)

🔇 Additional comments (7)
graphrag_sdk/orchestrator/orchestrator.py (1)

Line range hint 1-67: Verify completeness of Cypher generation fix

While the structural changes in this file look good, I don't see direct changes addressing the Cypher generation iterative loop fix mentioned in the PR title.

Let's verify if there are related changes in other files:

✅ Verification successful

Let me analyze the changes to verify if they address the Cypher generation iterative loop fix. The key changes appear to be in graphrag_sdk/steps/graph_query_step.py where the Cypher generation loop was modified. Let me verify the specific changes to the loop logic.


Let me check the specific changes in the Cypher generation loop implementation by examining the relevant files.


Cypher generation loop has been successfully fixed

The changes in graphrag_sdk/steps/graph_query_step.py show a significant improvement to the Cypher generation loop:

  1. Changed from a while loop with error flag to a cleaner for loop with fixed number of retries
  2. Removed redundant error-based prompt selection logic
  3. Added delete_last_message() call to clean up failed attempts
  4. Improved error handling and validation flow

The changes make the Cypher generation more robust by:

  • Using a deterministic number of retries (10 attempts)
  • Cleaning up the conversation context after failed attempts
  • Simplifying the prompt selection logic
  • Maintaining proper error propagation
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for Cypher-related code in the codebase
rg -l "cypher" --type py

# Search for any iterative or loop-related changes in recent commits
git log -p --since="1 week ago" | rg -A 5 -B 5 "(?i)(cypher|loop|iterate)"

Length of output: 83162


Script:

#!/bin/bash
# Get the complete implementation of GraphQueryGenerationStep class
ast-grep --pattern 'class GraphQueryGenerationStep {
  $$$
}'

# Search for any other files that might contain Cypher generation logic
rg -l "cypher.*generation" --type py

Length of output: 184


Script:

#!/bin/bash
# Get the content of files with Cypher generation logic
cat graphrag_sdk/model_config.py graphrag_sdk/chat_session.py tests/test_kg_ollama.py

# Get the content of graph_query_step.py since it wasn't found by ast-grep
cat graphrag_sdk/steps/graph_query_step.py

Length of output: 11726

graphrag_sdk/steps/graph_query_step.py (2)

Line range hint 45-78: LGTM! Improved error handling and control flow.

The changes improve the code in several ways:

  1. More predictable loop structure with fixed iterations
  2. Better error handling with chat session cleanup
  3. Simplified prompt selection logic based on last_answer
🧰 Tools
🪛 Ruff

45-45: Loop control variable i not used within loop body

Rename unused i to _i

(B007)


43-43: Verify the necessity of increasing default retries to 10.

While increasing retries can help handle transient failures, it could also mask underlying issues and increase latency. Please confirm if this change is based on observed failure patterns in production.

✅ Verification successful

The retry count increase is part of a larger error handling improvement

The change from 5 to 10 retries was made alongside significant improvements to the error handling logic:

  • Replaced error-prone while loop with a cleaner for loop
  • Removed the complex error-based prompt selection logic
  • Added chat session cleanup on errors to prevent context pollution
  • Simplified the overall retry mechanism

These changes make the increased retry count reasonable as the error handling is now more robust and cleaner, with proper cleanup between attempts.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any related issues or discussions about retry failures
gh issue list --state all --search "retry cypher generation"

# Check commit history for context about retry-related changes
git log -p --grep="retry" -- graphrag_sdk/steps/graph_query_step.py

Length of output: 130


Script:

#!/bin/bash
# Let's try a broader search to understand the context and usage of retries

# Search for retry-related patterns in the file
rg -A 5 -B 5 "retries" graphrag_sdk/steps/graph_query_step.py

# Look for any error handling or failure patterns
rg -A 5 "except|error|fail" graphrag_sdk/steps/graph_query_step.py

# Check the git history with a broader context
git log -p graphrag_sdk/steps/graph_query_step.py

# Look for any test files that might provide insights
fd "test.*graph.*query.*step.*" -t f

Length of output: 6945

graphrag_sdk/models/openai.py (1)

110-111: Verify integration with Cypher generation retry logic

The PR title suggests this is part of fixing Cypher generation loop. Let's verify how this method is used in the retry mechanism.

✅ Verification successful

Let me gather more information about the usage of delete_last_message specifically.


Let me check the implementation of the Cypher generation method to understand the retry logic better.


Integration with Cypher generation retry logic is correctly implemented

The delete_last_message() method is used within the retry loop of Cypher generation. When an exception occurs during Cypher generation, the last message is deleted before retrying, allowing for a clean slate in the next attempt. This is the expected behavior for handling failed attempts in the retry mechanism.

Key implementation details:

  • The retry logic uses a for-loop with configurable retries (default 10)
  • delete_last_message() is called in the exception handler before the next retry attempt
  • The method is consistently implemented across all LLM providers (OpenAI, Gemini, Azure, Ollama)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of delete_last_message in Cypher generation related code
rg -l "delete_last_message" | xargs rg -A 5 "Cypher|retry"

# Look for retry logic in GraphQueryGenerationStep
ast-grep --pattern 'class GraphQueryGenerationStep {
  $$$
  retry($$$) {
    $$$
  }
  $$$
}'

Length of output: 1310


Script:

#!/bin/bash
# Search for direct usage of delete_last_message
rg -B 5 -A 5 "delete_last_message"

# Look for retry-related logic in graph_query_step.py
rg -B 5 -A 5 "retry|attempt" graphrag_sdk/steps/graph_query_step.py

Length of output: 2484


Script:

#!/bin/bash
# Look for the main Cypher generation method
ast-grep --pattern 'def generate_cypher($$$) {
  $$$
}'

# Also check for any while loops or iteration logic in graph_query_step.py
rg -B 5 -A 10 "while|for" graphrag_sdk/steps/graph_query_step.py

Length of output: 1062

graphrag_sdk/models/ollama.py (2)

220-221: Verify consistent implementation across chat sessions

Let's ensure the delete_last_message implementation is consistent across all chat session classes.

#!/bin/bash
# Description: Check delete_last_message implementation in other chat sessions
# Expected: Similar implementations in other chat session classes

# Find all chat session classes and their delete_last_message implementations
ast-grep --pattern 'class $_ChatSession($_) {
  $$$
  delete_last_message($$$) {
    $$$
  }
  $$$
}'

# Backup: Search using ripgrep
rg -A 5 "def delete_last_message" graphrag_sdk/models/

220-221: Verify usage in Cypher generation retry logic

Let's understand how this method is used in the Cypher generation retry loop mentioned in the PR title.

✅ Verification successful

delete_last_message() is correctly used in Cypher generation retry logic

The method is properly integrated within the retry loop of GraphQueryGenerationStep.run(). When a Cypher query generation attempt fails (due to validation errors or execution issues), delete_last_message() is called to remove the failed attempt from the chat history before the next retry iteration. This ensures a clean slate for subsequent attempts within the maximum retry limit of 10.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find where delete_last_message is used in Cypher generation
# Expected: Usage in retry logic for Cypher generation

# Search for GraphQueryGenerationStep class and its retry logic
ast-grep --pattern 'class GraphQueryGenerationStep {
  $$$
  $_($$$) {
    $$$
    delete_last_message
    $$$
  }
  $$$
}'

# Backup: Search using ripgrep
rg -B 10 -A 10 "delete_last_message" graphrag_sdk/steps/

Length of output: 1396


Script:

#!/bin/bash
# Let's get more context around the usage in graph_query_step.py
rg -B 20 -A 10 "delete_last_message" graphrag_sdk/steps/graph_query_step.py

# Also check the class definition and retry mechanism
ast-grep --pattern 'class GraphQueryGenerationStep {
  $$$
  execute($$$) {
    $$$
  }
}'

Length of output: 1161


Script:

#!/bin/bash
# Let's get the full context of the retry loop and class
cat graphrag_sdk/steps/graph_query_step.py

Length of output: 2791

graphrag_sdk/models/azure_openai.py (1)

231-232: Verify consistency with other chat session implementations

Let's ensure the delete_last_message implementation is consistent across all chat session classes.

✅ Verification successful

Implementation is consistent across all chat session classes

The verification shows that delete_last_message is implemented consistently across all chat session classes:

  • All implementations have identical method signatures: def delete_last_message(self)
  • All implementations follow the same pattern of removing the last two items from their respective history arrays:
    • OpenAI: self._history[:-2]
    • Gemini: self._chat_session.history[:-2]
    • Azure OpenAI: self._history[:-2]
    • Ollama: self._chat_history[:-2]
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check implementation consistency across chat session classes

# Find all chat session classes and their delete_last_message implementations
ast-grep --pattern 'class $CHAT_CLASS {
  $$$
  delete_last_message($$$) {
    $IMPL
  }
  $$$
}'

# Check for any differences in method signatures
rg -A 1 "def delete_last_message"

Length of output: 744

Comment thread .wordlist.txt
Comment thread graphrag_sdk/models/openai.py Outdated
Comment thread graphrag_sdk/models/gemini.py Outdated
Comment thread graphrag_sdk/models/ollama.py Outdated
Comment thread graphrag_sdk/models/azure_openai.py Outdated
Comment thread graphrag_sdk/models/azure_openai.py
Comment thread README.md Outdated
Copy link
Copy Markdown

@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: 0

🧹 Outside diff range and nitpick comments (1)
graphrag_sdk/models/ollama.py (1)

220-237: Implementation looks good with room for minor improvement

The implementation effectively addresses the key concerns:

  • Proper documentation with docstring
  • System message preservation
  • Safe handling of chat history

However, consider adding error handling for edge cases:

     def delete_last_message(self):
         """
         Deletes the last message exchange (user message and assistant response) from the chat history.
         Preserves the system message if present.
         
         Note: Does nothing if the chat history is empty or contains only a system message.
         """
+        if not self._chat_history:
+            return
+            
         # Keep at least the system message if present
         min_length = 1 if self._model.system_instruction else 0
-        if len(self._chat_history) - 2 >= min_length:
+        if (len(self._chat_history) >= 2 and  # Ensure we have at least a message pair
+            self._chat_history[-2]["role"] == "user" and  # Validate message pair structure
+            self._chat_history[-1]["role"] == "assistant" and
+            len(self._chat_history) - 2 >= min_length):  # Check system message preservation
             self._chat_history = self._chat_history[:-2]
         else:
             # Reset to initial state with just system message if present
             self._chat_history = (
                 [{"role": "system", "content": self._model.system_instruction}]
                 if self._model.system_instruction is not None
                 else []
             )

The suggested changes add validation to ensure:

  1. The chat history exists
  2. We have a complete message pair (user + assistant)
  3. The message pair has the correct structure
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 90b37cc and dafd373.

📒 Files selected for processing (4)
  • graphrag_sdk/models/azure_openai.py (1 hunks)
  • graphrag_sdk/models/gemini.py (1 hunks)
  • graphrag_sdk/models/ollama.py (1 hunks)
  • graphrag_sdk/models/openai.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • graphrag_sdk/models/azure_openai.py
  • graphrag_sdk/models/gemini.py
  • graphrag_sdk/models/openai.py

Comment thread README.md
Comment thread graphrag_sdk/fixtures/prompts.py
Comment thread graphrag_sdk/models/azure_openai.py
Comment thread graphrag_sdk/models/azure_openai.py Outdated
Comment thread graphrag_sdk/models/ollama.py
@galshubeli galshubeli requested a review from swilly22 November 19, 2024 11:08
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.

2 participants