-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add rate limiting * feat: add redoc * feat: extends rate limiting --------- Co-authored-by: leoguillaume <[email protected]>
- Loading branch information
1 parent
420ca6a
commit 4d1211e
Showing
32 changed files
with
365 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
from fastapi import APIRouter, Security | ||
from fastapi import APIRouter, Request, Security | ||
|
||
from app.helpers import SearchOnInternet | ||
from app.schemas.search import Searches, SearchRequest | ||
from app.schemas.security import User | ||
from app.utils.lifespan import clients | ||
from app.utils.security import check_api_key | ||
from app.utils.config import DEFAULT_RATE_LIMIT | ||
from app.utils.lifespan import clients, limiter | ||
from app.utils.security import check_api_key, check_rate_limit | ||
from app.utils.variables import INTERNET_COLLECTION_ID | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/search") | ||
async def search(request: SearchRequest, user: User = Security(check_api_key)) -> Searches: | ||
@limiter.limit(DEFAULT_RATE_LIMIT, key_func=lambda request: check_rate_limit(request=request)) | ||
async def search(request: Request, body: SearchRequest, user: User = Security(check_api_key)) -> Searches: | ||
""" | ||
Similarity search for chunks in the vector store or on the internet. | ||
""" | ||
|
||
data = [] | ||
if INTERNET_COLLECTION_ID in request.collections: | ||
request.collections.remove(INTERNET_COLLECTION_ID) | ||
if INTERNET_COLLECTION_ID in body.collections: | ||
body.collections.remove(INTERNET_COLLECTION_ID) | ||
internet = SearchOnInternet(models=clients.models) | ||
if len(request.collections) > 0: | ||
collection_model = clients.vectors.get_collections(collection_ids=request.collections, user=user)[0].model | ||
if len(body.collections) > 0: | ||
collection_model = clients.vectors.get_collections(collection_ids=body.collections, user=user)[0].model | ||
else: | ||
collection_model = None | ||
data.extend(internet.search(prompt=request.prompt, n=4, model_id=collection_model, score_threshold=request.score_threshold)) | ||
data.extend(internet.search(prompt=body.prompt, n=4, model_id=collection_model, score_threshold=body.score_threshold)) | ||
|
||
if len(request.collections) > 0: | ||
if len(body.collections) > 0: | ||
data.extend( | ||
clients.vectors.search( | ||
prompt=request.prompt, collection_ids=request.collections, k=request.k, score_threshold=request.score_threshold, user=user | ||
) | ||
clients.vectors.search(prompt=body.prompt, collection_ids=body.collections, k=body.k, score_threshold=body.score_threshold, user=user) | ||
) | ||
|
||
data = sorted(data, key=lambda x: x.score, reverse=False)[: request.k] | ||
data = sorted(data, key=lambda x: x.score, reverse=False)[: body.k] | ||
|
||
return Searches(data=data) |
Oops, something went wrong.