citation_tool.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. 学术引用生成工具
  3. 支持 GB/T 7714、APA 7th、MLA 9th 三种主流学术引用格式。
  4. """
  5. from typing import Dict, Any, List
  6. from hello_agents.tools import Tool, ToolParameter, ToolResponse, ToolStatus
  7. class CitationTool(Tool):
  8. """学术引用生成工具
  9. 根据论文元数据生成指定格式的学术引用。
  10. """
  11. def __init__(self):
  12. super().__init__(
  13. name="citation_generator",
  14. description="根据论文信息生成指定格式的学术引用。"
  15. "支持 GB/T 7714(中文期刊标准)、APA 第7版、MLA 第9版。"
  16. "当需要生成参考文献引用时使用此工具。"
  17. )
  18. def _format_authors(self, authors_str: str, format_type: str) -> str:
  19. authors = [a.strip() for a in authors_str.split(",")]
  20. if format_type == "gbt7714":
  21. return ", ".join(authors)
  22. elif format_type == "apa":
  23. if len(authors) == 1:
  24. return authors[0]
  25. elif len(authors) == 2:
  26. return f"{authors[0]}, & {authors[1]}"
  27. else:
  28. return ", ".join(authors[:-1]) + f", & {authors[-1]}"
  29. elif format_type == "mla":
  30. if len(authors) == 1:
  31. return authors[0]
  32. elif len(authors) == 2:
  33. return f"{authors[0]}, and {authors[1]}"
  34. else:
  35. return f"{authors[0]}, et al"
  36. return authors_str
  37. def run(self, parameters: Dict[str, Any]) -> ToolResponse:
  38. title = parameters.get("title", "")
  39. authors_str = parameters.get("authors", "")
  40. journal = parameters.get("journal", "")
  41. year = parameters.get("year", "")
  42. volume = parameters.get("volume", "")
  43. pages = parameters.get("pages", "")
  44. doi = parameters.get("doi", "")
  45. format_type = parameters.get("format", "gbt7714")
  46. if not title or not authors_str:
  47. return ToolResponse.error(
  48. code="INVALID_PARAM",
  49. message="标题和作者为必填项"
  50. )
  51. formatted_authors = self._format_authors(authors_str, format_type)
  52. if format_type == "gbt7714":
  53. citation = f"{formatted_authors}. {title}[J]. {journal}, {year}, {volume}: {pages}."
  54. elif format_type == "apa":
  55. citation = f"{formatted_authors} ({year}). {title}. {journal}, {volume}, {pages}."
  56. if doi:
  57. citation += f" https://doi.org/{doi}"
  58. elif format_type == "mla":
  59. citation = f'{formatted_authors}. "{title}." {journal}, vol. {volume}, {year}, pp. {pages}.'
  60. else:
  61. return ToolResponse.error(
  62. code="INVALID_PARAM",
  63. message=f"不支持的引用格式: {format_type},支持: gbt7714, apa, mla"
  64. )
  65. return ToolResponse.success(
  66. text=citation,
  67. data={"format": format_type, "citation": citation}
  68. )
  69. def get_parameters(self) -> List[ToolParameter]:
  70. return [
  71. ToolParameter(name="title", type="string",
  72. description="论文标题", required=True),
  73. ToolParameter(name="authors", type="string",
  74. description="作者列表,用逗号分隔",
  75. required=True),
  76. ToolParameter(name="journal", type="string",
  77. description="期刊/会议名称", required=False),
  78. ToolParameter(name="year", type="string",
  79. description="发表年份", required=False),
  80. ToolParameter(name="volume", type="string",
  81. description="卷号", required=False),
  82. ToolParameter(name="pages", type="string",
  83. description="页码", required=False),
  84. ToolParameter(name="doi", type="string",
  85. description="DOI 号", required=False),
  86. ToolParameter(name="format", type="string",
  87. description="引用格式:gbt7714 / apa / mla",
  88. required=False),
  89. ]