file_search_tool.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from typing import Type, List
  2. from pydantic import BaseModel, Field
  3. from sqlalchemy.orm import Session
  4. from app.core.tools.base_tool import BaseTool
  5. from app.models.run import Run
  6. from app.services.file.file import FileService
  7. from app.services.assistant.assistant import AssistantService
  8. # return '## important:You can use the "retrieval" tool to search for relevant information.\n If you are asking about the content of the files, please specify any keywords, topics, or context you are looking for to help retrieve the most relevant content.'
  9. # query: str = Field(
  10. # ...,
  11. # description="query to look up in retrieval",
  12. # )
  13. # asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  14. # query: str = Field(..., description="query to look up in retrieval")
  15. class FileSearchToolInput(BaseModel):
  16. query: str = Field(
  17. ...,
  18. description="query to look up in retrieval",
  19. )
  20. class FileSearchTool(BaseTool):
  21. name: str = "file_search"
  22. description: str = (
  23. "Can be used to look up knowledge base or files content that was uploaded to this assistant."
  24. + "If the user is retrieve specified content from the knowledge base or file content, that is often a good hint that information may be here."
  25. + "Singleton operation: Strictly 1 invocation per API call"
  26. )
  27. args_schema: Type[BaseModel] = FileSearchToolInput
  28. def __init__(self) -> None:
  29. super().__init__()
  30. self.__filenames = []
  31. self.__keys = []
  32. self.__dirkeys = []
  33. self.loop = None
  34. self.index = 0
  35. def configure(self, session: Session, run: Run, **kwargs):
  36. # 获取当前事件循环
  37. # document_id = []
  38. file_key = []
  39. # filesinfo = []
  40. # 后语要从知识库里选择文件,所以在openassistant的数据库里可能不存在
  41. for key in run.file_ids:
  42. if len(key) == 36:
  43. self.__keys.append(key) # 添加文件id 作为检索
  44. else:
  45. file_key.append(
  46. key
  47. ) ## assiatant的id数据,在r2r里没办法检索需要提取filekey字段
  48. print(
  49. "document_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_id"
  50. )
  51. # print(document_id)
  52. print(file_key)
  53. files = []
  54. # 这种情况是uuid.ex 这种格式的在最早的时候存在的,后续要去掉
  55. if len(file_key) > 0:
  56. ## 获取文件信息
  57. files = FileService.get_file_list_by_ids(session=session, file_ids=file_key)
  58. for file in files:
  59. self.__keys.append(file.key)
  60. print(files)
  61. # 读取assistant的数据,获取文件夹的id
  62. db_asst = AssistantService.get_assistant_sync(
  63. session=session, assistant_id=run.assistant_id
  64. )
  65. if db_asst.tool_resources and "file_search" in db_asst.tool_resources:
  66. ##{"file_search": {"vector_store_ids": [{"file_ids": []}]}}
  67. asst_folder_ids = (
  68. db_asst.tool_resources.get("file_search")
  69. .get("vector_stores")[0]
  70. .get("folder_ids")
  71. )
  72. print(asst_folder_ids)
  73. # folder_fileinfo = []
  74. if asst_folder_ids:
  75. self.__dirkeys = asst_folder_ids
  76. # pre-cache data to prevent thread conflicts that may occur later on.
  77. print(
  78. "---------ssssssssssss-----------------sssssssssssss---------------ssssssssssssss-------------sssssssssssss-------------ss-------"
  79. )
  80. print(self.__dirkeys)
  81. print(self.__keys)
  82. # indexes: List[int],
  83. def run(self, query: str) -> dict:
  84. print(
  85. "file_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keys"
  86. )
  87. print(self.__keys)
  88. print(self.__dirkeys)
  89. files = []
  90. ## 必须有总结的内容query和才能触发
  91. if self.index == 0 and query:
  92. files = FileService.search_in_files(
  93. query=query, file_keys=self.__keys, folder_keys=self.__dirkeys
  94. )
  95. self.index = 1
  96. # print(files)
  97. return files
  98. def instruction_supplement(self) -> str:
  99. """
  100. 为 Retrieval 提供文件选择信息,用于 llm 调用抉择
  101. """
  102. if (self.__keys and len(self.__keys) > 0) or (
  103. self.__dirkeys and len(self.__dirkeys) > 0
  104. ):
  105. return ""
  106. else:
  107. return ""