-
Notifications
You must be signed in to change notification settings - Fork 930
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Cache generated responses (#336)
- Loading branch information
1 parent
98c95fe
commit c908e3a
Showing
7 changed files
with
209 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
from typing import Callable, Literal, Optional, Protocol | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
|
11 changes: 11 additions & 0 deletions
11
agents-api/agents_api/models/session/get_cached_response.py
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from ..utils import cozo_query | ||
|
||
|
||
@cozo_query | ||
def get_cached_response(key: str) -> tuple[str, dict]: | ||
query = """ | ||
input[key] <- [[$key]] | ||
?[key, value] := input[key], *session_cache{key, value} | ||
""" | ||
|
||
return (query, {"key": key}) |
16 changes: 16 additions & 0 deletions
16
agents-api/agents_api/models/session/set_cached_response.py
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from ..utils import cozo_query | ||
|
||
|
||
@cozo_query | ||
def set_cached_response(key: str, value: dict) -> tuple[str, dict]: | ||
query = """ | ||
?[key, value] <- [[$key, $value]] | ||
:insert session_cache { | ||
key => value | ||
} | ||
:returning | ||
""" | ||
|
||
return (query, {"key": key, "value": value}) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# /usr/bin/env python3 | ||
|
||
MIGRATION_ID = "session_cache" | ||
CREATED_AT = 1716013793.746602 | ||
|
||
|
||
session_cache = dict( | ||
up=""" | ||
:create session_cache { | ||
key: String, | ||
=> | ||
value: Json, | ||
} | ||
""", | ||
down=""" | ||
::remove session_cache | ||
""", | ||
) | ||
|
||
|
||
queries_to_run = [ | ||
session_cache, | ||
] | ||
|
||
|
||
def up(client): | ||
for q in queries_to_run: | ||
client.run(q["up"]) | ||
|
||
|
||
def down(client): | ||
for q in reversed(queries_to_run): | ||
client.run(q["down"]) |
Oops, something went wrong.