yqkcn commited on
Commit
2493f1d
·
1 Parent(s): d78cac8

Fix mutable default argument (#2635)

Browse files

### 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)

Files changed (1) hide show
  1. graphrag/utils.py +9 -7
graphrag/utils.py CHANGED
@@ -14,23 +14,25 @@ ErrorHandlerFn = Callable[[BaseException | None, str | None, dict | None], None]
14
 
15
 
16
  def perform_variable_replacements(
17
- input: str, history: list[dict]=[], variables: dict | None ={}
18
  ) -> str:
19
  """Perform variable replacements on the input string and in a chat log."""
 
 
 
 
20
  result = input
21
 
22
  def replace_all(input: str) -> str:
23
  result = input
24
- if variables:
25
- for entry in variables:
26
- result = result.replace(f"{{{entry}}}", variables[entry])
27
  return result
28
 
29
  result = replace_all(result)
30
- for i in range(len(history)):
31
- entry = history[i]
32
  if entry.get("role") == "system":
33
- history[i]["content"] = replace_all(entry.get("content") or "")
34
 
35
  return result
36
 
 
14
 
15
 
16
  def perform_variable_replacements(
17
+ input: str, history: list[dict] | None = None, variables: dict | None = None
18
  ) -> str:
19
  """Perform variable replacements on the input string and in a chat log."""
20
+ if history is None:
21
+ history = []
22
+ if variables is None:
23
+ variables = {}
24
  result = input
25
 
26
  def replace_all(input: str) -> str:
27
  result = input
28
+ for k, v in variables.items():
29
+ result = result.replace(f"{{{k}}}", v)
 
30
  return result
31
 
32
  result = replace_all(result)
33
+ for i, entry in enumerate(history):
 
34
  if entry.get("role") == "system":
35
+ entry["content"] = replace_all(entry.get("content") or "")
36
 
37
  return result
38