Skip to content

Commit

Permalink
update query api to ignore quries which has been invalidated
Browse files Browse the repository at this point in the history
  • Loading branch information
nattvara committed Mar 8, 2023
1 parent c7ec5d2 commit 94ca7f3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/routers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def new_query(input_data: InputModel) -> OutputModel:
elif input_data.override_cache:
should_create_new_query = True

elif query.cache_is_valid is False:
should_create_new_query = True

if should_create_new_query:
cached = False
query = create_query(lecture, input_data.query_string)
Expand Down
10 changes: 9 additions & 1 deletion db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ def delete_all_except_last_message_in_analysis(analysis_id: int):
# Query
def get_most_recent_query_by_sha(lecture, sha: str):
from db.models.query import Query
return Query.filter(Query.lecture_id == lecture.id).filter(Query.query_hash == sha).order_by(Query.modified_at.desc()).first() # noqa: E501
return Query.filter(
Query.lecture_id == lecture.id
).filter(
Query.query_hash == sha
).filter(
Query.cache_is_valid == True
).order_by(
Query.modified_at.desc()
).first()


def create_query(lecture, query_string: str):
Expand Down
30 changes: 30 additions & 0 deletions tests/feature/api/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,33 @@ def request():

assert response.json()['response'] == 'gpt-3 response'
assert gpt3.call_count == 2


def test_query_response_cache_can_be_invalidated(mocker, api_client, analysed_lecture):
gpt3 = mocker.patch('tools.text.ai.gpt3', return_value='gpt-3 response')

def make_query(query_string: str):
return api_client.post('/query', json={
'lecture_id': analysed_lecture.public_id,
'language': analysed_lecture.language,
'query_string': query_string,
})

def make_requests():
make_query('some query')
make_query('some query')
make_query('some other query')
make_query('some third query')

make_requests()
assert gpt3.call_count == 3

# invalidate the cache
queries = analysed_lecture.queries()

for query in queries:
query.cache_is_valid = False
query.save()

make_requests()
assert gpt3.call_count == 6

0 comments on commit 94ca7f3

Please sign in to comment.