Revert "no cache"
Browse filesThis reverts commit 3bb3721807552a6f254ff0722e680201be0129d2.
- App/Embedding/EmbeddingRoutes.py +14 -7
- App/Embedding/utils/Initialize.py +1 -6
- App/Embedding/utils/__init__.py +5 -6
- requirements.txt +1 -0
App/Embedding/EmbeddingRoutes.py
CHANGED
@@ -2,28 +2,35 @@ from fastapi import APIRouter, BackgroundTasks
|
|
2 |
|
3 |
from .utils.Initialize import TextSearch, IdSearch
|
4 |
from .Schemas import SearchRequest, AddDocumentRequest
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
embeddigs_router = APIRouter(tags=["embeddings"])
|
8 |
|
9 |
|
10 |
# create
|
11 |
@embeddigs_router.post("/add_document")
|
12 |
-
# @cache(namespace="cache1")
|
13 |
async def create_embeddings(req: AddDocumentRequest):
|
14 |
pass
|
15 |
|
16 |
|
17 |
@embeddigs_router.post("/search_id")
|
18 |
-
# @cache(namespace="cache2")
|
19 |
async def search_id(
|
20 |
req: SearchRequest,
|
21 |
background_tasks: BackgroundTasks,
|
22 |
):
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
|
26 |
@embeddigs_router.post("/search_text")
|
27 |
-
|
28 |
-
|
29 |
-
return TextSearch(query=req.query)
|
|
|
2 |
|
3 |
from .utils.Initialize import TextSearch, IdSearch
|
4 |
from .Schemas import SearchRequest, AddDocumentRequest
|
5 |
+
import redis, os, json
|
6 |
+
|
7 |
+
REDIS = os.environ.get("REDIS")
|
8 |
+
cache = redis.from_url(REDIS)
|
9 |
+
|
10 |
|
11 |
embeddigs_router = APIRouter(tags=["embeddings"])
|
12 |
|
13 |
|
14 |
# create
|
15 |
@embeddigs_router.post("/add_document")
|
|
|
16 |
async def create_embeddings(req: AddDocumentRequest):
|
17 |
pass
|
18 |
|
19 |
|
20 |
@embeddigs_router.post("/search_id")
|
|
|
21 |
async def search_id(
|
22 |
req: SearchRequest,
|
23 |
background_tasks: BackgroundTasks,
|
24 |
):
|
25 |
+
data = cache.get(f"recommendations:{req.query}")
|
26 |
+
if data is not None:
|
27 |
+
return json.loads(data)
|
28 |
+
|
29 |
+
data = IdSearch(query=req.query, background_task=background_tasks)
|
30 |
+
cache.set(f"recommendations:{req.query}", json.dumps(data), ex=72000)
|
31 |
+
return data
|
32 |
|
33 |
|
34 |
@embeddigs_router.post("/search_text")
|
35 |
+
async def search_text(reqx: SearchRequest):
|
36 |
+
return TextSearch(query=reqx.query)
|
|
App/Embedding/utils/Initialize.py
CHANGED
@@ -11,7 +11,7 @@ from .Elastic import FetchDocuments
|
|
11 |
index_name = "movie-recommender-fast"
|
12 |
model_name = "thenlper/gte-base"
|
13 |
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
14 |
-
|
15 |
|
16 |
TMDB_API = os.environ.get("TMDB_API")
|
17 |
|
@@ -27,7 +27,6 @@ vector_index = pinecone.Index(index_name=index_name)
|
|
27 |
docsearch = Pinecone.from_existing_index(index_name, embeddings)
|
28 |
|
29 |
|
30 |
-
# @cache(namespace="test")
|
31 |
def check_if_exists(text, imdb_id):
|
32 |
results = docsearch.similarity_search(text, filter={"key": {"$eq": imdb_id}}, k=1)
|
33 |
if results:
|
@@ -36,7 +35,6 @@ def check_if_exists(text, imdb_id):
|
|
36 |
return False
|
37 |
|
38 |
|
39 |
-
# @cache(namespace="test")
|
40 |
def add_document(imdb_id, doc):
|
41 |
text, temp_doc = doc
|
42 |
response = check_if_exists(text=text, imdb_id=imdb_id)
|
@@ -54,14 +52,12 @@ def add_document(imdb_id, doc):
|
|
54 |
docsearch.add_documents([temp])
|
55 |
|
56 |
|
57 |
-
# @cache(namespace="test")
|
58 |
def generate_text(doc):
|
59 |
if doc["tv_results"]:
|
60 |
return pprint.pformat(doc["tv_results"][0]), doc["tv_results"][0]
|
61 |
return pprint.pformat(doc["movie_results"][0]), doc["movie_results"][0]
|
62 |
|
63 |
|
64 |
-
# @cache(namespace="test")
|
65 |
def IdSearch(query: str, background_task: BackgroundTasks):
|
66 |
doc = requests.get(
|
67 |
f"https://api.themoviedb.org/3/find/{query}?external_source=imdb_id&language=en&api_key={TMDB_API}"
|
@@ -75,7 +71,6 @@ def IdSearch(query: str, background_task: BackgroundTasks):
|
|
75 |
return TextSearch(text, filter={"key": {"$ne": query}})
|
76 |
|
77 |
|
78 |
-
# @cache(namespace="test")
|
79 |
def TextSearch(query: str, filter=None):
|
80 |
docs = docsearch.similarity_search(query, k=10, filter=filter)
|
81 |
keys = [doc.metadata["key"] for doc in docs]
|
|
|
11 |
index_name = "movie-recommender-fast"
|
12 |
model_name = "thenlper/gte-base"
|
13 |
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
14 |
+
|
15 |
|
16 |
TMDB_API = os.environ.get("TMDB_API")
|
17 |
|
|
|
27 |
docsearch = Pinecone.from_existing_index(index_name, embeddings)
|
28 |
|
29 |
|
|
|
30 |
def check_if_exists(text, imdb_id):
|
31 |
results = docsearch.similarity_search(text, filter={"key": {"$eq": imdb_id}}, k=1)
|
32 |
if results:
|
|
|
35 |
return False
|
36 |
|
37 |
|
|
|
38 |
def add_document(imdb_id, doc):
|
39 |
text, temp_doc = doc
|
40 |
response = check_if_exists(text=text, imdb_id=imdb_id)
|
|
|
52 |
docsearch.add_documents([temp])
|
53 |
|
54 |
|
|
|
55 |
def generate_text(doc):
|
56 |
if doc["tv_results"]:
|
57 |
return pprint.pformat(doc["tv_results"][0]), doc["tv_results"][0]
|
58 |
return pprint.pformat(doc["movie_results"][0]), doc["movie_results"][0]
|
59 |
|
60 |
|
|
|
61 |
def IdSearch(query: str, background_task: BackgroundTasks):
|
62 |
doc = requests.get(
|
63 |
f"https://api.themoviedb.org/3/find/{query}?external_source=imdb_id&language=en&api_key={TMDB_API}"
|
|
|
71 |
return TextSearch(text, filter={"key": {"$ne": query}})
|
72 |
|
73 |
|
|
|
74 |
def TextSearch(query: str, filter=None):
|
75 |
docs = docsearch.similarity_search(query, k=10, filter=filter)
|
76 |
keys = [doc.metadata["key"] for doc in docs]
|
App/Embedding/utils/__init__.py
CHANGED
@@ -1,14 +1,13 @@
|
|
1 |
from elasticsearch import Elasticsearch
|
2 |
import os
|
3 |
|
4 |
-
elastic_host=os.environ.get(
|
|
|
|
|
|
|
5 |
|
6 |
# initialize elasticSearch
|
7 |
-
es = Elasticsearch(
|
8 |
-
[
|
9 |
-
elastic_host
|
10 |
-
]
|
11 |
-
)
|
12 |
|
13 |
|
14 |
def FetchDocuments(ids):
|
|
|
1 |
from elasticsearch import Elasticsearch
|
2 |
import os
|
3 |
|
4 |
+
elastic_host = os.environ.get(
|
5 |
+
"ELASTIC_HOST",
|
6 |
+
"https://u46hxt12c:[email protected]:443",
|
7 |
+
)
|
8 |
|
9 |
# initialize elasticSearch
|
10 |
+
es = Elasticsearch([elastic_host])
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
def FetchDocuments(ids):
|
requirements.txt
CHANGED
@@ -6,5 +6,6 @@ langchain
|
|
6 |
uvicorn[standard]
|
7 |
pydantic
|
8 |
requests
|
|
|
9 |
fastapi-cache2[memcache]
|
10 |
|
|
|
6 |
uvicorn[standard]
|
7 |
pydantic
|
8 |
requests
|
9 |
+
redis
|
10 |
fastapi-cache2[memcache]
|
11 |
|