Skip to content

Commit

Permalink
add Refresh Cache button,
Browse files Browse the repository at this point in the history
 - 'unsaved_model_state' for single run non-persistent 'session_state'
 - changed 'save_on_step' to save the original 'sr_session_state'
  • Loading branch information
milovate committed Jan 3, 2025
1 parent a48cbd7 commit caf91c0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
19 changes: 13 additions & 6 deletions celeryapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ def runner_task(
error_msg = None

@db_middleware
def save_on_step(yield_val: str | tuple[str, dict] = None, *, done: bool = False):
def save_on_step(
yield_val: str | tuple[str, dict] = None,
*,
done: bool = False,
sr_session_state: dict[str, typing.Any] = None,
):
if isinstance(yield_val, tuple):
run_status, extra_output = yield_val
else:
Expand Down Expand Up @@ -93,7 +98,8 @@ def save_on_step(yield_val: str | tuple[str, dict] = None, *, done: bool = False
# send outputs to ui
gui.realtime_push(channel, output)
# save to db
page.dump_state_to_sr(gui.session_state | output, sr)
# dont save unsaved_state
page.dump_state_to_sr(sr_session_state | output, sr)

page = page_cls(
user=AppUser.objects.get(id=user_id),
Expand All @@ -103,12 +109,13 @@ def save_on_step(yield_val: str | tuple[str, dict] = None, *, done: bool = False
sr = page.current_sr
set_current_workspace(page.request.session, int(sr.workspace_id))
threadlocal.saved_run = sr
gui.set_session_state(sr.to_dict() | (unsaved_state or {}))
original_state = sr.to_dict()
gui.set_session_state(original_state | (unsaved_state or {}))

try:
save_on_step()
save_on_step(sr_session_state=original_state)
for val in page.main(sr, gui.session_state):
save_on_step(val)
save_on_step(val, sr_session_state=original_state)

# render errors nicely
except Exception as e:
Expand All @@ -130,7 +137,7 @@ def save_on_step(yield_val: str | tuple[str, dict] = None, *, done: bool = False

# save everything, mark run as completed
finally:
save_on_step(done=True)
save_on_step(done=True, sr_session_state=original_state)
threadlocal.saved_run = None

post_runner_tasks.delay(sr.id)
Expand Down
19 changes: 13 additions & 6 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,8 +1909,10 @@ def render_extra_waiting_output(self):
def estimate_run_duration(self) -> int | None:
pass

def submit_and_redirect(self) -> typing.NoReturn | None:
sr = self.on_submit()
def submit_and_redirect(
self, unsaved_model_state: dict[str, typing.Any] = None
) -> typing.NoReturn | None:
sr = self.on_submit(unsaved_model_state=unsaved_model_state)
if not sr:
return
raise gui.RedirectException(self.app_url(run_id=sr.run_id, uid=sr.uid))
Expand Down Expand Up @@ -1940,11 +1942,11 @@ def publish_and_redirect(self) -> typing.NoReturn | None:
)
raise gui.RedirectException(pr.get_app_url())

def on_submit(self):
def on_submit(self, unsaved_model_state: dict[str, typing.Any] = None):
sr = self.create_and_validate_new_run(enable_rate_limits=True)
if not sr:
return
self.call_runner_task(sr)
self.call_runner_task(sr, unsaved_model_state=unsaved_model_state)
return sr

def should_submit_after_login(self) -> bool:
Expand Down Expand Up @@ -2056,7 +2058,12 @@ def dump_state_to_sr(self, state: dict, sr: SavedRun):
}
)

def call_runner_task(self, sr: SavedRun, deduct_credits: bool = True):
def call_runner_task(
self,
sr: SavedRun,
deduct_credits: bool = True,
unsaved_model_state: dict[str, typing.Any] = None,
):
from celeryapp.tasks import runner_task

return runner_task.delay(
Expand All @@ -2065,7 +2072,7 @@ def call_runner_task(self, sr: SavedRun, deduct_credits: bool = True):
run_id=sr.run_id,
uid=sr.uid,
channel=self.realtime_channel_name(sr.run_id, sr.uid),
unsaved_state=self._unsaved_state(),
unsaved_state=self._unsaved_state() | (unsaved_model_state or {}),
deduct_credits=deduct_credits,
)

Expand Down
28 changes: 20 additions & 8 deletions daras_ai_v2/doc_search_settings_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,32 @@ def keyword_instructions_widget():
)


def cache_knowledge_widget():
def cache_knowledge_widget(self):
gui.write("###### Cache")
gui.caption(
f"""
By default we embed your knowledge files & links and cache their contents for fast responses.
"""
)

gui.checkbox(
"Always Check for Updates",
help="With each incoming message, documents and links will be checked for changes and re-indexed. Slower but useful for dynamic webpages, Google Sheets, Docs, etc that change often.",
tooltip_placement="right",
key="check_document_updates",
)
col1, col2 = gui.columns(2, style={"alignItems": "center"})
with col1:
gui.checkbox(
"Always Check for Updates",
help="With each incoming message, documents and links will be checked for changes and re-indexed. Slower but useful for dynamic webpages, Google Sheets, Docs, etc that change often.",
tooltip_placement="bottom",
key="check_document_updates",
)
with col2:
with gui.tooltip(
"Clear the knowledge cache and re-index all knowledge base files and links."
):
if gui.button(
"♻️ Refresh Cache",
type="tertiary",
):
# temporary model filed
unsaved_model_state = {"check_document_updates": True}
self.submit_and_redirect(unsaved_model_state=unsaved_model_state)


def doc_extract_selector(current_user: AppUser | None):
Expand Down
2 changes: 1 addition & 1 deletion recipes/DocSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def render_settings(self):
citation_style_selector()
doc_extract_selector(self.request.user)
query_instructions_widget()
cache_knowledge_widget()
cache_knowledge_widget(self)
gui.write("---")
doc_search_advanced_settings()

Expand Down
2 changes: 1 addition & 1 deletion recipes/GoogleGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def render_settings(self):
gui.write("---")
gui.write("##### 🔎 Document Search Settings")
query_instructions_widget()
cache_knowledge_widget()
cache_knowledge_widget(self)
gui.write("---")
doc_search_advanced_settings()

Expand Down
2 changes: 1 addition & 1 deletion recipes/VideoBots.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ def render_settings(self):

citation_style_selector()
gui.checkbox("🔗 Shorten citation links", key="use_url_shortener")
cache_knowledge_widget()
cache_knowledge_widget(self)
doc_extract_selector(self.request.user)

gui.write("---")
Expand Down

0 comments on commit caf91c0

Please sign in to comment.