Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,122 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
def greet_json():
|
7 |
return {"Hello": "World!"}
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from langchain_qdrant import QdrantVectorStore
|
3 |
+
from qdrant_client import QdrantClient
|
4 |
+
from qdrant_client.http.models import Distance, VectorParams
|
5 |
+
|
6 |
+
from langchain_qdrant import FastEmbedSparse, QdrantVectorStore, RetrievalMode
|
7 |
+
from qdrant_client import QdrantClient, models
|
8 |
+
from qdrant_client.http.models import Distance, SparseVectorParams, VectorParams
|
9 |
+
|
10 |
+
from uuid import uuid4
|
11 |
+
|
12 |
+
from langchain_core.documents import Document
|
13 |
+
|
14 |
+
document_1 = Document(
|
15 |
+
page_content="I had chocolate chip pancakes and scrambled eggs for breakfast this morning.",
|
16 |
+
metadata={"source": "tweet"},
|
17 |
+
)
|
18 |
+
|
19 |
+
document_2 = Document(
|
20 |
+
page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees Fahrenheit.",
|
21 |
+
metadata={"source": "news"},
|
22 |
+
)
|
23 |
+
|
24 |
+
document_3 = Document(
|
25 |
+
page_content="Building an exciting new project with LangChain - come check it out!",
|
26 |
+
metadata={"source": "tweet"},
|
27 |
+
)
|
28 |
+
|
29 |
+
document_4 = Document(
|
30 |
+
page_content="Robbers broke into the city bank and stole $1 million in cash.",
|
31 |
+
metadata={"source": "news"},
|
32 |
+
)
|
33 |
+
|
34 |
+
document_5 = Document(
|
35 |
+
page_content="Wow! That was an amazing movie. I can't wait to see it again.",
|
36 |
+
metadata={"source": "tweet"},
|
37 |
+
)
|
38 |
+
|
39 |
+
document_6 = Document(
|
40 |
+
page_content="Is the new iPhone worth the price? Read this review to find out.",
|
41 |
+
metadata={"source": "website"},
|
42 |
+
)
|
43 |
+
|
44 |
+
document_7 = Document(
|
45 |
+
page_content="The top 10 soccer players in the world right now.",
|
46 |
+
metadata={"source": "website"},
|
47 |
+
)
|
48 |
+
|
49 |
+
document_8 = Document(
|
50 |
+
page_content="LangGraph is the best framework for building stateful, agentic applications!",
|
51 |
+
metadata={"source": "tweet"},
|
52 |
+
)
|
53 |
+
|
54 |
+
document_9 = Document(
|
55 |
+
page_content="The stock market is down 500 points today due to fears of a recession.",
|
56 |
+
metadata={"source": "news"},
|
57 |
+
)
|
58 |
+
|
59 |
+
document_10 = Document(
|
60 |
+
page_content="I have a bad feeling I am going to get deleted :(",
|
61 |
+
metadata={"source": "tweet"},
|
62 |
+
)
|
63 |
+
|
64 |
+
documents = [
|
65 |
+
document_1,
|
66 |
+
document_2,
|
67 |
+
document_3,
|
68 |
+
document_4,
|
69 |
+
document_5,
|
70 |
+
document_6,
|
71 |
+
document_7,
|
72 |
+
document_8,
|
73 |
+
document_9,
|
74 |
+
document_10,
|
75 |
+
]
|
76 |
+
uuids = [str(uuid4()) for _ in range(len(documents))]
|
77 |
+
|
78 |
+
docs = documents
|
79 |
+
|
80 |
+
sparse_embeddings = FastEmbedSparse(model_name="Qdrant/bm25")
|
81 |
+
|
82 |
+
client = QdrantClient(path="tmp/langchain_qdrant")
|
83 |
+
|
84 |
+
# Create a collection with sparse vectors
|
85 |
+
client.create_collection(
|
86 |
+
collection_name="my_documents",
|
87 |
+
vectors_config={"dense": VectorParams(size=3072, distance=Distance.COSINE)},
|
88 |
+
sparse_vectors_config={
|
89 |
+
"sparse": SparseVectorParams(index=models.SparseIndexParams(on_disk=False))
|
90 |
+
},
|
91 |
+
)
|
92 |
+
|
93 |
+
qdrant = QdrantVectorStore(
|
94 |
+
client=client,
|
95 |
+
collection_name="my_documents",
|
96 |
+
sparse_embedding=sparse_embeddings,
|
97 |
+
retrieval_mode=RetrievalMode.SPARSE,
|
98 |
+
sparse_vector_name="sparse",
|
99 |
+
|
100 |
+
)
|
101 |
+
|
102 |
+
qdrant.add_documents(documents=documents, ids=uuids)
|
103 |
|
104 |
app = FastAPI()
|
105 |
|
106 |
+
@app.get("/get_data")
|
107 |
+
def get_data(query: str):
|
108 |
+
# query = "How much money did the robbers steal?"
|
109 |
+
found_docs = [x.model_dump() for x qdrant.similarity_search(query)]
|
110 |
+
found_docs.pop("id", None)
|
111 |
+
for k,v in found_docs["metadata"].keys():
|
112 |
+
if k[0] == "_":
|
113 |
+
found_docs["metadata"].pop(k)
|
114 |
+
|
115 |
+
return {
|
116 |
+
"data": found_docs
|
117 |
+
}
|
118 |
+
|
119 |
+
|
120 |
@app.get("/")
|
121 |
def greet_json():
|
122 |
return {"Hello": "World!"}
|