-
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.
* chore: change internet client * feat: add score threshold * feat: async client * fix: internet and exceptions --------- Co-authored-by: leoguillaume <[email protected]>
- Loading branch information
1 parent
a1cc34f
commit e85f03e
Showing
24 changed files
with
508 additions
and
1,625 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,72 @@ | ||
import uuid | ||
|
||
from fastapi import APIRouter, Request, Security | ||
|
||
from app.schemas.search import Searches, SearchRequest | ||
from app.schemas.security import User | ||
from app.utils.settings import settings | ||
from app.utils.lifespan import clients, limiter | ||
from app.utils.security import check_api_key, check_rate_limit | ||
from app.utils.settings import settings | ||
from app.utils.variables import INTERNET_COLLECTION_DISPLAY_ID | ||
|
||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/search") | ||
@limiter.limit(settings.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: | ||
@router.post(path="/search") | ||
@limiter.limit(limit_value=settings.default_rate_limit, key_func=lambda request: check_rate_limit(request=request)) | ||
async def search(request: Request, body: SearchRequest, user: User = Security(dependency=check_api_key)) -> Searches: | ||
""" | ||
Endpoint to search on the internet or with our engine client | ||
""" | ||
|
||
# TODO: to be handled by a service top to InternetExplorer | ||
body = await request.json() | ||
body = SearchRequest(**body) | ||
|
||
# Internet search | ||
need_internet_search = not body.collections or INTERNET_COLLECTION_DISPLAY_ID in body.collections | ||
internet_chunks = [] | ||
if need_internet_search: | ||
internet_chunks = clients.internet.get_chunks(prompt=body.prompt) | ||
# get internet results chunks | ||
internet_collection_id = str(uuid.uuid4()) | ||
internet_chunks = clients.internet.get_chunks(prompt=body.prompt, collection_id=internet_collection_id) | ||
|
||
if internet_chunks: | ||
internet_collection = clients.internet.create_temporary_internet_collection(internet_chunks, body.collections, user) | ||
internet_embeddings_model_id = ( | ||
clients.internet.default_embeddings_model_id | ||
if body.collections == [INTERNET_COLLECTION_DISPLAY_ID] | ||
else clients.search.get_collections(collection_ids=body.collections, user=user)[0].model | ||
) | ||
|
||
clients.search.create_collection( | ||
collection_id=internet_collection_id, | ||
collection_name=internet_collection_id, | ||
collection_model=internet_embeddings_model_id, | ||
user=user, | ||
) | ||
clients.search.upsert(chunks=internet_chunks, collection_id=internet_collection_id, user=user) | ||
|
||
# case: no other collections, only internet, and no internet results | ||
elif body.collections == [INTERNET_COLLECTION_DISPLAY_ID]: | ||
return Searches(data=[]) | ||
|
||
# case: other collections or only internet and internet results | ||
if INTERNET_COLLECTION_DISPLAY_ID in body.collections: | ||
body.collections.remove(INTERNET_COLLECTION_DISPLAY_ID) | ||
if not body.collections and not internet_chunks: | ||
return Searches(data=[]) | ||
if internet_chunks: | ||
body.collections.append(internet_collection.id) | ||
body.collections.append(internet_collection_id) | ||
|
||
searches = clients.search.query( | ||
prompt=body.prompt, | ||
collection_ids=body.collections, | ||
method=body.method, | ||
k=body.k, | ||
rff_k=body.rff_k, | ||
score_threshold=body.score_threshold, | ||
user=user, | ||
) | ||
|
||
if internet_chunks: | ||
clients.search.delete_collection(internet_collection.id, user=user) | ||
clients.search.delete_collection(collection_id=internet_collection_id, user=user) | ||
|
||
if body.score_threshold: | ||
searches = [search for search in searches if search.score >= body.score_threshold] | ||
|
||
return Searches(data=searches) |
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
Oops, something went wrong.