Spaces:
Sleeping
Sleeping
Upload CustomRetriever.py
Browse files- llm/CustomRetriever.py +29 -0
llm/CustomRetriever.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.vectorstores import VectorStore
|
2 |
+
from langchain.schema.retriever import BaseRetriever
|
3 |
+
from langchain_core.documents import Document
|
4 |
+
from typing import List
|
5 |
+
|
6 |
+
from langchain.callbacks.manager import CallbackManagerForRetrieverRun
|
7 |
+
|
8 |
+
from langchain_core.documents import Document
|
9 |
+
from langchain_core.runnables import chain
|
10 |
+
|
11 |
+
class CustomRetriever(BaseRetriever):
|
12 |
+
vectorstore:VectorStore
|
13 |
+
thold:float
|
14 |
+
|
15 |
+
def _get_relevant_documents(
|
16 |
+
self, query: str, *, run_manager: CallbackManagerForRetrieverRun
|
17 |
+
) -> List[Document]:
|
18 |
+
|
19 |
+
docs, scores = zip(*self.vectorstore.similarity_search_with_relevance_scores(query, callbacks=run_manager.get_child()))#get_relevant_documents(query, callbacks=run_manager.get_child())
|
20 |
+
|
21 |
+
result=[]
|
22 |
+
for doc, score in zip(docs, scores):
|
23 |
+
if score>self.thold:
|
24 |
+
doc.metadata["score"] = score
|
25 |
+
result.append(doc)
|
26 |
+
if len(result)==0:
|
27 |
+
result.append(Document(metadata={}, page_content='No data'))
|
28 |
+
|
29 |
+
return result
|