Spaces:
Runtime error
Runtime error
jiarongqiu commited on
Commit ·
733bea7
1
Parent(s): db15762
first commit
Browse files- main.py +11 -3
- requirements.txt +91 -7
- service/vector_store.py +25 -0
- test/vector_store.py +2 -2
main.py
CHANGED
|
@@ -1,11 +1,19 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
|
|
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
def read_root():
|
| 7 |
return {"Hello": "World!"}
|
| 8 |
|
| 9 |
-
@app.get("/")
|
| 10 |
-
def
|
| 11 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from service import VectorStore
|
| 3 |
+
|
| 4 |
+
PROJECT_NAME = "filecoin"
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
+
vector_store = VectorStore(PROJECT_NAME)
|
| 8 |
|
| 9 |
@app.get("/")
|
| 10 |
def read_root():
|
| 11 |
return {"Hello": "World!"}
|
| 12 |
|
| 13 |
+
@app.get("/vector/search")
|
| 14 |
+
def vector_search(inputs):
|
| 15 |
+
return vector_store.search(inputs)
|
| 16 |
+
|
| 17 |
+
@app.get("/vector/marginal_search")
|
| 18 |
+
def vector_marginal_search(inputs):
|
| 19 |
+
return vector_store.marginal_search(inputs)
|
requirements.txt
CHANGED
|
@@ -1,7 +1,91 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohttp==3.9.1
|
| 3 |
+
aiosignal==1.3.1
|
| 4 |
+
altair==5.2.0
|
| 5 |
+
annotated-types==0.6.0
|
| 6 |
+
anyio==3.7.1
|
| 7 |
+
attrs==23.1.0
|
| 8 |
+
beautifulsoup4==4.12.2
|
| 9 |
+
bs4==0.0.1
|
| 10 |
+
certifi==2023.11.17
|
| 11 |
+
charset-normalizer==3.3.2
|
| 12 |
+
click==8.1.7
|
| 13 |
+
colorama==0.4.6
|
| 14 |
+
contourpy==1.2.0
|
| 15 |
+
cycler==0.12.1
|
| 16 |
+
dataclasses-json==0.6.3
|
| 17 |
+
distro==1.8.0
|
| 18 |
+
dnspython==2.4.2
|
| 19 |
+
fastapi==0.105.0
|
| 20 |
+
ffmpy==0.3.1
|
| 21 |
+
filelock==3.13.1
|
| 22 |
+
fonttools==4.47.0
|
| 23 |
+
frozenlist==1.4.1
|
| 24 |
+
fsspec==2023.12.2
|
| 25 |
+
gradio==4.11.0
|
| 26 |
+
gradio_client==0.7.3
|
| 27 |
+
greenlet==3.0.2
|
| 28 |
+
h11==0.14.0
|
| 29 |
+
httpcore==1.0.2
|
| 30 |
+
httpx==0.25.2
|
| 31 |
+
huggingface-hub==0.19.4
|
| 32 |
+
idna==3.6
|
| 33 |
+
importlib-resources==6.1.1
|
| 34 |
+
Jinja2==3.1.2
|
| 35 |
+
jsonpatch==1.33
|
| 36 |
+
jsonpointer==2.4
|
| 37 |
+
jsonschema==4.20.0
|
| 38 |
+
jsonschema-specifications==2023.11.2
|
| 39 |
+
kiwisolver==1.4.5
|
| 40 |
+
langchain==0.0.332
|
| 41 |
+
langsmith==0.0.72
|
| 42 |
+
loguru==0.7.2
|
| 43 |
+
lxml==4.9.4
|
| 44 |
+
markdown-it-py==3.0.0
|
| 45 |
+
MarkupSafe==2.1.3
|
| 46 |
+
marshmallow==3.20.1
|
| 47 |
+
matplotlib==3.8.2
|
| 48 |
+
mdurl==0.1.2
|
| 49 |
+
multidict==6.0.4
|
| 50 |
+
mypy-extensions==1.0.0
|
| 51 |
+
numpy==1.26.2
|
| 52 |
+
openai==1.6.0
|
| 53 |
+
orjson==3.9.10
|
| 54 |
+
packaging==23.2
|
| 55 |
+
pandas==2.1.4
|
| 56 |
+
Pillow==10.1.0
|
| 57 |
+
pinecone-client==2.2.4
|
| 58 |
+
pydantic==2.5.2
|
| 59 |
+
pydantic_core==2.14.5
|
| 60 |
+
pydub==0.25.1
|
| 61 |
+
Pygments==2.17.2
|
| 62 |
+
pyparsing==3.1.1
|
| 63 |
+
python-dateutil==2.8.2
|
| 64 |
+
python-multipart==0.0.6
|
| 65 |
+
pytz==2023.3.post1
|
| 66 |
+
PyYAML==6.0.1
|
| 67 |
+
referencing==0.32.0
|
| 68 |
+
regex==2023.10.3
|
| 69 |
+
requests==2.31.0
|
| 70 |
+
rich==13.7.0
|
| 71 |
+
rpds-py==0.15.2
|
| 72 |
+
semantic-version==2.10.0
|
| 73 |
+
shellingham==1.5.4
|
| 74 |
+
six==1.16.0
|
| 75 |
+
sniffio==1.3.0
|
| 76 |
+
soupsieve==2.5
|
| 77 |
+
SQLAlchemy==2.0.23
|
| 78 |
+
starlette==0.27.0
|
| 79 |
+
tenacity==8.2.3
|
| 80 |
+
tiktoken==0.5.2
|
| 81 |
+
tomlkit==0.12.0
|
| 82 |
+
toolz==0.12.0
|
| 83 |
+
tqdm==4.66.1
|
| 84 |
+
typer==0.9.0
|
| 85 |
+
typing-inspect==0.9.0
|
| 86 |
+
typing_extensions==4.9.0
|
| 87 |
+
tzdata==2023.3
|
| 88 |
+
urllib3==2.1.0
|
| 89 |
+
uvicorn==0.24.0.post1
|
| 90 |
+
websockets==11.0.3
|
| 91 |
+
yarl==1.9.4
|
service/vector_store.py
CHANGED
|
@@ -7,6 +7,7 @@ from langchain.docstore.document import Document
|
|
| 7 |
from langchain.schema.embeddings import Embeddings
|
| 8 |
from langchain.vectorstores.utils import DistanceStrategy, maximal_marginal_relevance
|
| 9 |
import numpy as np
|
|
|
|
| 10 |
import logging
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
|
@@ -27,8 +28,32 @@ class VectorStore(Pinecone):
|
|
| 27 |
def add_docs(self,docs):
|
| 28 |
if self.index_name not in pinecone.list_indexes():
|
| 29 |
pinecone.create_index(name=self.index_name, metric="cosine", dimension=self.dims)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
Pinecone.from_documents(docs, self.embeddings, index_name=self.index_name)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def similarity_search_by_vector_with_score(
|
| 33 |
self,
|
| 34 |
embedding: List[float],
|
|
|
|
| 7 |
from langchain.schema.embeddings import Embeddings
|
| 8 |
from langchain.vectorstores.utils import DistanceStrategy, maximal_marginal_relevance
|
| 9 |
import numpy as np
|
| 10 |
+
import json
|
| 11 |
import logging
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
|
|
|
| 28 |
def add_docs(self,docs):
|
| 29 |
if self.index_name not in pinecone.list_indexes():
|
| 30 |
pinecone.create_index(name=self.index_name, metric="cosine", dimension=self.dims)
|
| 31 |
+
for doc in docs:
|
| 32 |
+
metadata = doc.metadata
|
| 33 |
+
for k in metadata:
|
| 34 |
+
if metadata.get(k) is None:
|
| 35 |
+
metadata[k] = 'unknown'
|
| 36 |
Pinecone.from_documents(docs, self.embeddings, index_name=self.index_name)
|
| 37 |
|
| 38 |
+
def search(self,query,ret_json=True):
|
| 39 |
+
docs = self.similarity_search(query)
|
| 40 |
+
if ret_json:
|
| 41 |
+
return self.jsonfy(docs)
|
| 42 |
+
else:
|
| 43 |
+
return docs
|
| 44 |
+
|
| 45 |
+
def marginal_search(self,query,ret_json=True):
|
| 46 |
+
docs = self.max_marginal_relevance_search(query)
|
| 47 |
+
if ret_json:
|
| 48 |
+
return self.jsonfy(docs)
|
| 49 |
+
else:
|
| 50 |
+
return docs
|
| 51 |
+
|
| 52 |
+
def jsonfy(self,docs):
|
| 53 |
+
docs = [doc.dict() for doc in docs]
|
| 54 |
+
docs = json.dumps(docs)
|
| 55 |
+
return docs
|
| 56 |
+
|
| 57 |
def similarity_search_by_vector_with_score(
|
| 58 |
self,
|
| 59 |
embedding: List[float],
|
test/vector_store.py
CHANGED
|
@@ -2,6 +2,6 @@ from service import VectorStore
|
|
| 2 |
|
| 3 |
index_name = 'filecoin'
|
| 4 |
vector_store = VectorStore(index_name)
|
| 5 |
-
print(vector_store.
|
| 6 |
|
| 7 |
-
print(vector_store.
|
|
|
|
| 2 |
|
| 3 |
index_name = 'filecoin'
|
| 4 |
vector_store = VectorStore(index_name)
|
| 5 |
+
print(vector_store.search("what is filecoin",ret_json=True))
|
| 6 |
|
| 7 |
+
print(vector_store.marginal_search("what is filecoin",ret_json=True))
|