Skip to content

Commit f405e07

Browse files
authored
limit query size (#4)
* limit query size * add env variable * fix typo * add more info on query length and limits
1 parent ea01d74 commit f405e07

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SCHOLARAG__KEYCLOAK__VALIDATE_TOKEN=
4040
# other
4141
SCHOLARAG__MISC__APPLICATION_PREFIX=
4242
SCHOLARAG__MISC__CORS_ORIGINS=
43+
SCHOLARAG__MISC__QUERY_MAX_SIZE=
4344

4445
SSL_CERT_FILE=(path to cert file)(etc/ssl/certs/ca-certificates.crt for ubuntu, for mac check Nicolas tutorial)
4546
REQUESTS_CA_BUNDLE=(path to cert file)(etc/ssl/certs/ca-certificates.crt for ubuntu, for mac check Nicolas tutorial)

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Fixed
11+
- Limit query size.
1112
- Add OBI copyright.
1213

1314
## [0.0.8] - 10.12.2024

src/scholarag/app/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class SettingsMisc(BaseModel):
138138
# comma separated entries, i.e. "value_1, value_2, ..."
139139
cors_origins: str = ""
140140

141+
# Query size limiter, in number of characters. (630 words ~= 5000 characters.)
142+
query_max_size: int = 10000
143+
141144
model_config = ConfigDict(frozen=True)
142145

143146

src/scholarag/app/routers/qa.py

+15
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ async def passthrough(
104104
-------
105105
A single answer.
106106
""" # noqa: D301, D400, D205
107+
if len(request.query) > settings.misc.query_max_size:
108+
raise HTTPException(
109+
status_code=413,
110+
detail=f"Query string has {len(request.query)} characters. Maximum allowed is {settings.misc.query_max_size}.",
111+
)
107112
start = time.time()
108113
logger.info("Finding answers ...")
109114
try:
@@ -240,6 +245,11 @@ async def generative_qa(
240245
-------
241246
A single answer with metadata of each relevant source.
242247
""" # noqa: D301, D400, D205
248+
if len(request.query) > settings.misc.query_max_size:
249+
raise HTTPException(
250+
status_code=413,
251+
detail=f"Query string has {len(request.query)} characters. Maximum allowed is {settings.misc.query_max_size}.",
252+
)
243253
start = time.time()
244254
logger.info("Finding answers ...")
245255

@@ -460,6 +470,11 @@ async def streamed_generative_qa(
460470
-------
461471
A single answer with metadata of each relevant source.
462472
""" # noqa: D301, D400, D205
473+
if len(request.query) > settings.misc.query_max_size:
474+
raise HTTPException(
475+
status_code=413,
476+
detail=f"Query string has {len(request.query)} characters. Maximum allowed is {settings.misc.query_max_size}.",
477+
)
463478
start = time.time()
464479
logger.info("Finding answers ...")
465480

src/scholarag/app/routers/retrieval.py

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ async def retrieval(
9090
-------
9191
A list of article titles and paragraphs.
9292
""" # noqa: D301, D400, D205
93+
if len(request.query) > settings.misc.query_max_size:
94+
raise HTTPException(
95+
status_code=413,
96+
detail=f"Query string has {len(request.query)} characters. Maximum allowed is {settings.misc.query_max_size}.",
97+
)
9398
start = time.time()
9499
logger.info("Finding documents ...")
95100

0 commit comments

Comments
 (0)