Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

utils.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (c) 2024 Microsoft Corporation.
  2. # Licensed under the MIT License
  3. """
  4. Reference:
  5. - [graphrag](https://github.com/microsoft/graphrag)
  6. """
  7. import html
  8. import re
  9. from collections.abc import Callable
  10. from typing import Any
  11. ErrorHandlerFn = Callable[[BaseException | None, str | None, dict | None], None]
  12. def perform_variable_replacements(
  13. input: str, history: list[dict]=[], variables: dict | None ={}
  14. ) -> str:
  15. """Perform variable replacements on the input string and in a chat log."""
  16. result = input
  17. def replace_all(input: str) -> str:
  18. result = input
  19. if variables:
  20. for entry in variables:
  21. result = result.replace(f"{{{entry}}}", variables[entry])
  22. return result
  23. result = replace_all(result)
  24. for i in range(len(history)):
  25. entry = history[i]
  26. if entry.get("role") == "system":
  27. history[i]["content"] = replace_all(entry.get("content") or "")
  28. return result
  29. def clean_str(input: Any) -> str:
  30. """Clean an input string by removing HTML escapes, control characters, and other unwanted characters."""
  31. # If we get non-string input, just give it back
  32. if not isinstance(input, str):
  33. return input
  34. result = html.unescape(input.strip())
  35. # https://stackoverflow.com/questions/4324790/removing-control-characters-from-a-string-in-python
  36. return re.sub(r"[\"\x00-\x1f\x7f-\x9f]", "", result)
  37. def dict_has_keys_with_types(
  38. data: dict, expected_fields: list[tuple[str, type]]
  39. ) -> bool:
  40. """Return True if the given dictionary has the given keys with the given types."""
  41. for field, field_type in expected_fields:
  42. if field not in data:
  43. return False
  44. value = data[field]
  45. if not isinstance(value, field_type):
  46. return False
  47. return True