prompt_utils.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import json
  2. from io import StringIO
  3. from typing import Dict
  4. import yaml
  5. def format_agent_profile(profile: Dict) -> str:
  6. fields = [
  7. ('name', '名字'),
  8. ('gender', '性别'),
  9. ('age', '年龄'),
  10. ('region', '所在地'),
  11. ('previous_location', '之前所在地'),
  12. ('education', '学历'),
  13. ('occupation', '职业'),
  14. ('work_experience', '工作经历'),
  15. ('family_members', '家庭成员'),
  16. ('family_occupation', '家庭成员职业')
  17. ]
  18. strings_to_join = []
  19. for field in fields:
  20. if profile.get(field[0], None) is None:
  21. continue
  22. cur_string = f"- {field[1]}:{profile[field[0]]}"
  23. strings_to_join.append(cur_string)
  24. return "\n".join(strings_to_join)
  25. def format_agent_profile_v2(profile: Dict) -> str:
  26. str_stream = StringIO()
  27. yaml.dump(profile, str_stream, indent=2, allow_unicode=True)
  28. return str_stream.getvalue()
  29. def format_user_profile(profile: Dict) -> str:
  30. """
  31. :param profile:
  32. :return: formatted string.
  33. example:
  34. - 微信昵称:{nickname}
  35. - 姓名:{name}
  36. - 头像:{avatar}
  37. - 偏好的称呼:{preferred_nickname}
  38. - 年龄:{age}
  39. - 地区:{region}
  40. - 健康状况:{health_conditions}
  41. - 用药信息:{medications}
  42. - 兴趣爱好:{interests}
  43. """
  44. fields = [
  45. ('nickname', '微信昵称'),
  46. ('preferred_nickname', '希望对其的称呼'),
  47. ('name', '姓名'),
  48. ('avatar', '头像'),
  49. ('gender', '性别'),
  50. ('age', '年龄'),
  51. ('region', '地区'),
  52. ('health_conditions', '健康状况'),
  53. ('interests', '兴趣爱好'),
  54. ('interaction_frequency', '联系频率'),
  55. ('flexible_params', '动态特征'),
  56. ]
  57. strings_to_join = []
  58. for field in fields:
  59. value = profile.get(field[0], None)
  60. if not value:
  61. value = '未知'
  62. elif isinstance(value, list):
  63. value = ','.join(value)
  64. elif isinstance(value, dict):
  65. value = ';'.join(f"{k}: {v}" for k, v in value.items())
  66. else:
  67. value = profile[field[0]]
  68. cur_string = f"- {field[1]}:{value}"
  69. strings_to_join.append(cur_string)
  70. return "\n".join(strings_to_join)