Spaces:
Runtime error
Runtime error
jiarongqiu commited on
Commit ·
a5081b6
1
Parent(s): 733bea7
update
Browse files- main.py +16 -9
- run.sh +1 -0
- service/vector_store.py +31 -30
main.py
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from service import
|
| 3 |
-
|
| 4 |
-
|
| 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("/
|
| 14 |
-
def vector_search(inputs):
|
| 15 |
return vector_store.search(inputs)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
from service.vector_store import vector_store
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from fastapi.responses import StreamingResponse
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 8 |
|
| 9 |
@app.get("/")
|
| 10 |
def read_root():
|
| 11 |
return {"Hello": "World!"}
|
| 12 |
|
| 13 |
+
@app.get("/api/search")
|
| 14 |
+
def vector_search(inputs: str):
|
| 15 |
return vector_store.search(inputs)
|
| 16 |
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
async def fake_video_streamer():
|
| 20 |
+
for i in range(10):
|
| 21 |
+
yield "some fake video bytes"
|
| 22 |
+
time.sleep(0.5)
|
| 23 |
+
|
| 24 |
+
@app.get("/api/answer")
|
| 25 |
+
async def answer(inputs: str):
|
| 26 |
+
return StreamingResponse(fake_video_streamer())
|
run.sh
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
uvicorn main:app
|
service/vector_store.py
CHANGED
|
@@ -9,45 +9,42 @@ from langchain.vectorstores.utils import DistanceStrategy, maximal_marginal_rele
|
|
| 9 |
import numpy as np
|
| 10 |
import json
|
| 11 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
-
|
| 16 |
class VectorStore(Pinecone):
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def __init__(self
|
| 19 |
pinecone.init(
|
| 20 |
-
api_key=os.getenv("PINECONE_API_KEY"),
|
| 21 |
-
environment=os.getenv("PINECONE_ENV"),
|
| 22 |
)
|
| 23 |
-
|
| 24 |
self.dims = 1536
|
| 25 |
-
index = pinecone.Index(self.
|
| 26 |
-
super().__init__(index, OpenAIEmbeddings(),
|
| 27 |
|
| 28 |
def add_docs(self,docs):
|
| 29 |
-
if self.
|
| 30 |
-
pinecone.create_index(name=self.
|
| 31 |
-
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return self.jsonfy(docs)
|
| 42 |
-
else:
|
| 43 |
-
return docs
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
return self.jsonfy(docs)
|
| 49 |
-
else:
|
| 50 |
-
return docs
|
| 51 |
|
| 52 |
def jsonfy(self,docs):
|
| 53 |
docs = [doc.dict() for doc in docs]
|
|
@@ -73,6 +70,7 @@ class VectorStore(Pinecone):
|
|
| 73 |
include_metadata=True,
|
| 74 |
namespace=namespace,
|
| 75 |
filter=filter,
|
|
|
|
| 76 |
)
|
| 77 |
for res in results["matches"]:
|
| 78 |
metadata = res["metadata"]
|
|
@@ -80,7 +78,7 @@ class VectorStore(Pinecone):
|
|
| 80 |
text = metadata.pop(self._text_key)
|
| 81 |
score = res["score"]
|
| 82 |
metadata['score'] = score
|
| 83 |
-
print(f"metadata {metadata}")
|
| 84 |
docs.append((Document(page_content=text, metadata=metadata), score))
|
| 85 |
else:
|
| 86 |
logger.warning(
|
|
@@ -91,7 +89,7 @@ class VectorStore(Pinecone):
|
|
| 91 |
def max_marginal_relevance_search_by_vector(
|
| 92 |
self,
|
| 93 |
embedding: List[float],
|
| 94 |
-
k: int =
|
| 95 |
fetch_k: int = 20,
|
| 96 |
lambda_mult: float = 0.5,
|
| 97 |
filter: Optional[dict] = None,
|
|
@@ -123,6 +121,7 @@ class VectorStore(Pinecone):
|
|
| 123 |
include_metadata=True,
|
| 124 |
namespace=namespace,
|
| 125 |
filter=filter,
|
|
|
|
| 126 |
)
|
| 127 |
mmr_selected = maximal_marginal_relevance(
|
| 128 |
np.array([embedding], dtype=np.float32),
|
|
@@ -140,4 +139,6 @@ class VectorStore(Pinecone):
|
|
| 140 |
return [
|
| 141 |
Document(page_content=metadata.pop((self._text_key)), metadata=metadata)
|
| 142 |
for metadata in selected
|
| 143 |
-
]
|
|
|
|
|
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import json
|
| 11 |
import logging
|
| 12 |
+
import uuid
|
| 13 |
+
from langchain.utils.iter import batch_iterate
|
| 14 |
+
try:
|
| 15 |
+
from script import export
|
| 16 |
+
except:
|
| 17 |
+
pass
|
| 18 |
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
|
|
|
| 21 |
class VectorStore(Pinecone):
|
| 22 |
+
REQUEST_TIMEOUT=10
|
| 23 |
+
INDEX_NAME = "jarvis"
|
| 24 |
+
NAMESPACE = "filecoin"
|
| 25 |
|
| 26 |
+
def __init__(self) -> None:
|
| 27 |
pinecone.init(
|
| 28 |
+
api_key=os.getenv("PINECONE_API_KEY"),
|
| 29 |
+
environment=os.getenv("PINECONE_ENV"),
|
| 30 |
)
|
| 31 |
+
|
| 32 |
self.dims = 1536
|
| 33 |
+
index = pinecone.Index(self.INDEX_NAME)
|
| 34 |
+
super().__init__(index, OpenAIEmbeddings(),"text")
|
| 35 |
|
| 36 |
def add_docs(self,docs):
|
| 37 |
+
if self.INDEX_NAME not in pinecone.list_indexes():
|
| 38 |
+
pinecone.create_index(name=self.INDEX_NAME, metric="cosine", dimension=self.dims)
|
| 39 |
+
Pinecone.from_documents(docs, self.embeddings, index_name=self.INDEX_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# @timing
|
| 42 |
+
def search(self,query):
|
| 43 |
+
return self.similarity_search(query)
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# @timing
|
| 46 |
+
def marginal_search(self,query,k=5):
|
| 47 |
+
return self.max_marginal_relevance_search(query,k=k)
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def jsonfy(self,docs):
|
| 50 |
docs = [doc.dict() for doc in docs]
|
|
|
|
| 70 |
include_metadata=True,
|
| 71 |
namespace=namespace,
|
| 72 |
filter=filter,
|
| 73 |
+
_request_timeout=self.REQUEST_TIMEOUT
|
| 74 |
)
|
| 75 |
for res in results["matches"]:
|
| 76 |
metadata = res["metadata"]
|
|
|
|
| 78 |
text = metadata.pop(self._text_key)
|
| 79 |
score = res["score"]
|
| 80 |
metadata['score'] = score
|
| 81 |
+
# print(f"metadata {metadata}")
|
| 82 |
docs.append((Document(page_content=text, metadata=metadata), score))
|
| 83 |
else:
|
| 84 |
logger.warning(
|
|
|
|
| 89 |
def max_marginal_relevance_search_by_vector(
|
| 90 |
self,
|
| 91 |
embedding: List[float],
|
| 92 |
+
k: int = 5,
|
| 93 |
fetch_k: int = 20,
|
| 94 |
lambda_mult: float = 0.5,
|
| 95 |
filter: Optional[dict] = None,
|
|
|
|
| 121 |
include_metadata=True,
|
| 122 |
namespace=namespace,
|
| 123 |
filter=filter,
|
| 124 |
+
_request_timeout=self.REQUEST_TIMEOUT
|
| 125 |
)
|
| 126 |
mmr_selected = maximal_marginal_relevance(
|
| 127 |
np.array([embedding], dtype=np.float32),
|
|
|
|
| 139 |
return [
|
| 140 |
Document(page_content=metadata.pop((self._text_key)), metadata=metadata)
|
| 141 |
for metadata in selected
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
vector_store = VectorStore()
|