helper.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. import re
  3. from typing import Iterable, AsyncIterator
  4. def filter_json(text: str) -> str:
  5. """
  6. Parses JSON code block from a string.
  7. Args:
  8. text (str): A string containing a JSON code block.
  9. Returns:
  10. dict: A dictionary parsed from the JSON code block.
  11. """
  12. match = re.search(r"```(json|)\n(?P<code>[\S\s]+?)\n```", text)
  13. if match:
  14. return match.group("code")
  15. return text
  16. def find_stop(stop, content: str, chunk: str = None):
  17. first = -1
  18. word = None
  19. if stop is not None:
  20. for word in list(stop):
  21. first = content.find(word)
  22. if first != -1:
  23. content = content[:first]
  24. break
  25. if chunk is not None and first != -1:
  26. first = chunk.find(word)
  27. if first != -1:
  28. chunk = chunk[:first]
  29. else:
  30. first = 0
  31. return first, content, chunk
  32. def filter_none(**kwargs) -> dict:
  33. return {
  34. key: value
  35. for key, value in kwargs.items()
  36. if value is not None
  37. }
  38. async def cast_iter_async(iter: Iterable) -> AsyncIterator:
  39. for chunk in iter:
  40. yield chunk