Spaces:
Build error
Build error
Update retriever.py
Browse files- retriever.py +32 -25
retriever.py
CHANGED
|
@@ -1,25 +1,32 @@
|
|
| 1 |
-
from langchain.vectorstores import Qdrant
|
| 2 |
-
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 3 |
-
from qdrant_client import QdrantClient
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
client = QdrantClient(
|
| 10 |
-
url=
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.vectorstores import Qdrant
|
| 2 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 3 |
+
from qdrant_client import QdrantClient
|
| 4 |
+
import os # Added for environment variables
|
| 5 |
+
|
| 6 |
+
embeddings = SentenceTransformerEmbeddings(model_name="NeuML/pubmedbert-base-embeddings")
|
| 7 |
+
|
| 8 |
+
# Use environment variables for cloud configuration
|
| 9 |
+
client = QdrantClient(
|
| 10 |
+
url=os.getenv("QDRANT_URL", "https://YOUR_QDRANT_CLUSTER_URL.aws.cloud.qdrant.io"),
|
| 11 |
+
api_key=os.getenv("QDRANT_API_KEY"),
|
| 12 |
+
prefer_grpc=False
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
print(client)
|
| 16 |
+
print("##############")
|
| 17 |
+
|
| 18 |
+
db = Qdrant(client=client, embeddings=embeddings, collection_name="vector_db")
|
| 19 |
+
|
| 20 |
+
print(db)
|
| 21 |
+
print("######")
|
| 22 |
+
|
| 23 |
+
query = "What is Metastatic disease?"
|
| 24 |
+
|
| 25 |
+
# Updated similarity search (newer LangChain versions)
|
| 26 |
+
docs = db.similarity_search_with_relevance_scores(query=query, k=3)
|
| 27 |
+
for doc, score in docs:
|
| 28 |
+
print({
|
| 29 |
+
"score": score,
|
| 30 |
+
"content": doc.page_content,
|
| 31 |
+
"metadata": doc.metadata
|
| 32 |
+
})
|