ernie_bot_retriever.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from .base import BaseRetriever
  15. import os
  16. from langchain.docstore.document import Document
  17. from langchain.text_splitter import RecursiveCharacterTextSplitter
  18. from langchain_community.embeddings import QianfanEmbeddingsEndpoint
  19. from langchain_community.vectorstores import FAISS
  20. from langchain_community import vectorstores
  21. from erniebot_agent.extensions.langchain.embeddings import ErnieEmbeddings
  22. import time
  23. class ErnieBotRetriever(BaseRetriever):
  24. """Ernie Bot Retriever"""
  25. entities = [
  26. "ernie-4.0",
  27. "ernie-3.5",
  28. "ernie-3.5-8k",
  29. "ernie-lite",
  30. "ernie-tiny-8k",
  31. "ernie-speed",
  32. "ernie-speed-128k",
  33. "ernie-char-8k",
  34. ]
  35. def __init__(self, config):
  36. super().__init__()
  37. model_name = config.get("model_name", None)
  38. api_type = config.get("api_type", None)
  39. ak = config.get("ak", None)
  40. sk = config.get("sk", None)
  41. access_token = config.get("access_token", None)
  42. if model_name not in self.entities:
  43. raise ValueError(f"model_name must be in {self.entities} of ErnieBotChat.")
  44. if api_type not in ["aistudio", "qianfan"]:
  45. raise ValueError("api_type must be one of ['aistudio', 'qianfan']")
  46. if api_type == "aistudio" and access_token is None:
  47. raise ValueError("access_token cannot be empty when api_type is aistudio.")
  48. if api_type == "qianfan" and (ak is None or sk is None):
  49. raise ValueError("ak and sk cannot be empty when api_type is qianfan.")
  50. self.model_name = model_name
  51. self.config = config
  52. def generate_vector_database(
  53. self,
  54. text_list,
  55. block_size=300,
  56. separators=["\t", "\n", "。", "\n\n", ""],
  57. sleep_time=0.5,
  58. ):
  59. """
  60. args:
  61. return:
  62. """
  63. text_splitter = RecursiveCharacterTextSplitter(
  64. chunk_size=block_size, chunk_overlap=20, separators=separators
  65. )
  66. texts = text_splitter.split_text("\t".join(text_list))
  67. all_splits = [Document(page_content=text) for text in texts]
  68. api_type = self.config["api_type"]
  69. if api_type == "qianfan":
  70. os.environ["QIANFAN_AK"] = os.environ.get("EB_AK", self.config["ak"])
  71. os.environ["QIANFAN_SK"] = os.environ.get("EB_SK", self.config["sk"])
  72. user_ak = os.environ.get("EB_AK", self.config["ak"])
  73. user_id = hash(user_ak)
  74. vectorstore = FAISS.from_documents(
  75. documents=all_splits, embedding=QianfanEmbeddingsEndpoint()
  76. )
  77. elif api_type == "aistudio":
  78. token = self.config["access_token"]
  79. vectorstore = FAISS.from_documents(
  80. documents=all_splits[0:1],
  81. embedding=ErnieEmbeddings(aistudio_access_token=token),
  82. )
  83. #### ErnieEmbeddings.chunk_size = 16
  84. step = min(16, len(all_splits) - 1)
  85. for shot_splits in [
  86. all_splits[i : i + step] for i in range(1, len(all_splits), step)
  87. ]:
  88. time.sleep(sleep_time)
  89. vectorstore_slice = FAISS.from_documents(
  90. documents=shot_splits,
  91. embedding=ErnieEmbeddings(aistudio_access_token=token),
  92. )
  93. vectorstore.merge_from(vectorstore_slice)
  94. else:
  95. raise ValueError(f"Unsupported api_type: {api_type}")
  96. return vectorstore
  97. def encode_vector_store_to_bytes(self, vectorstore):
  98. vectorstore = self.encode_vector_store(vectorstore.serialize_to_bytes())
  99. return vectorstore
  100. def decode_vector_store_from_bytes(self, vectorstore):
  101. if not self.is_vector_store(vectorstore):
  102. raise ValueError("The retrieved vectorstore is not for PaddleX.")
  103. api_type = self.config["api_type"]
  104. if api_type == "aistudio":
  105. access_token = self.config["access_token"]
  106. embeddings = ErnieEmbeddings(aistudio_access_token=access_token)
  107. elif api_type == "qianfan":
  108. ak = self.config["ak"]
  109. sk = self.config["sk"]
  110. embeddings = QianfanEmbeddingsEndpoint(qianfan_ak=ak, qianfan_sk=sk)
  111. else:
  112. raise ValueError(f"Unsupported api_type: {api_type}")
  113. vector = vectorstores.FAISS.deserialize_from_bytes(
  114. self.decode_vector_store(vectorstore), embeddings
  115. )
  116. return vector
  117. def similarity_retrieval(self, query_text_list, vectorstore, sleep_time=0.5):
  118. # 根据提问匹配上下文
  119. C = []
  120. for query_text in query_text_list:
  121. QUESTION = query_text
  122. time.sleep(sleep_time)
  123. docs = vectorstore.similarity_search_with_relevance_scores(QUESTION, k=2)
  124. context = [(document.page_content, score) for document, score in docs]
  125. context = sorted(context, key=lambda x: x[1])
  126. C.extend([x[0] for x in context[::-1]])
  127. C = list(set(C))
  128. all_C = " ".join(C)
  129. return all_C