Skip to content

Commit

Permalink
Fix mutable default argument (#2635)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

The default value of Python function parameters cannot be mutable.
Modifying this parameter inside the function will permanently change the
default value

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
  • Loading branch information
yqkcn authored Sep 29, 2024
1 parent c103dd2 commit 604061c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions graphrag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@


def perform_variable_replacements(
input: str, history: list[dict]=[], variables: dict | None ={}
input: str, history: list[dict] | None = None, variables: dict | None = None
) -> str:
"""Perform variable replacements on the input string and in a chat log."""
if history is None:
history = []
if variables is None:
variables = {}
result = input

def replace_all(input: str) -> str:
result = input
if variables:
for entry in variables:
result = result.replace(f"{{{entry}}}", variables[entry])
for k, v in variables.items():
result = result.replace(f"{{{k}}}", v)
return result

result = replace_all(result)
for i in range(len(history)):
entry = history[i]
for i, entry in enumerate(history):
if entry.get("role") == "system":
history[i]["content"] = replace_all(entry.get("content") or "")
entry["content"] = replace_all(entry.get("content") or "")

return result

Expand Down

0 comments on commit 604061c

Please sign in to comment.