Skip to content

Commit

Permalink
feat: implement usage counter for openAI access endpoint /ask
Browse files Browse the repository at this point in the history
  • Loading branch information
ydennisy committed Apr 1, 2024
1 parent 97abf79 commit 8d585ef
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env
.venv
__pycache__
supabase/.temp
supabase/.branches
2 changes: 2 additions & 0 deletions backend/app/artefacts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
4 changes: 4 additions & 0 deletions backend/app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def __init__(self) -> None:
os.getenv("SUPABASE_URL"), os.getenv("SUPABASE_KEY")
)

def increment_usage_counter(self) -> int:
result = self._client.rpc("update_usage_counter").execute()
return result.data

def create_urls(self, urls: list[URL], user_id: str):
data = [{**url.to_persistence(), "user_id": user_id} for url in urls]
try:
Expand Down
3 changes: 3 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ async def get_search_route(q: str):

@app.get("/api/ask")
def get_ask_route(q: str):
usage_count = db.increment_usage_counter()
if usage_count > 1000:
return "Sorry, we have had to stop usage of the /ask endpoint due to API cost restrictions."
query_emb = NodeEmbedder.embed(q, return_type="list")
chunks = supabase.rpc(
"search_chunks", {"query_embedding": query_emb, "top_n": 10}
Expand Down
4 changes: 1 addition & 3 deletions backend/supabase/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# A string used to distinguish different Supabase projects on the same host. Defaults to the
# working directory name when running `supabase init`.
project_id = "pse"
project_id = "kg1"

[api]
enabled = true
Expand Down
11 changes: 6 additions & 5 deletions backend/supabase/migrations/20240114120525_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ create index
);

create table
public.counter(
public.usage_counter(
id text primary key,
count integer not null default 0
);

insert into counter(count) values (0);
insert into usage_counter(id, count) values ('openai-api', 0);

create or replace function update_counter()
create or replace function update_usage_counter()
returns integer
language plpgsql
as $$
begin
update counter set count = count + 1;
return (select count from counter);
update usage_counter set count = count + 1 where id = 'openai-api';
return (select count from usage_counter where id = 'openai-api');
end;
$$;

Expand Down

0 comments on commit 8d585ef

Please sign in to comment.