-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prompt): add prompt table, entity and repository (#823)
* feat: add prompts table * feat: add Prompt entity * feat: add prompt router * refactor(promptRepository): use common reposority
- Loading branch information
1 parent
23f50ec
commit e3b6114
Showing
14 changed files
with
261 additions
and
11 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
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from models.databases.supabase.api_key_handler import ApiKeyHandler | ||
from models.databases.supabase.brains import Brain | ||
from models.databases.supabase.users import User | ||
from models.databases.supabase.files import File | ||
from models.databases.supabase.brains_subscription_invitations import BrainSubscription | ||
from models.databases.supabase.api_key_handler import ApiKeyHandler | ||
from models.databases.supabase.chats import Chats | ||
from models.databases.supabase.files import File | ||
from models.databases.supabase.prompts import Prompts | ||
from models.databases.supabase.users import User | ||
from models.databases.supabase.vectors import Vector |
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,103 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
from fastapi import HTTPException | ||
from models.databases.repository import Repository | ||
from models.prompt import Prompt | ||
from pydantic import BaseModel | ||
|
||
|
||
class CreatePromptProperties(BaseModel): | ||
"""Properties that can be received on prompt creation""" | ||
|
||
title: str | ||
content: str | ||
status: str = "private" | ||
|
||
|
||
class PromptUpdatableProperties(BaseModel): | ||
"""Properties that can be received on prompt update""" | ||
|
||
title: Optional[str] | ||
content: Optional[str] | ||
status: Optional[str] | ||
|
||
|
||
class Prompts(Repository): | ||
def __init__(self, supabase_client): | ||
self.db = supabase_client | ||
|
||
def create_prompt(self, prompt: CreatePromptProperties) -> Prompt: | ||
"""Create a prompt by id""" | ||
|
||
response = (self.db.from_("prompts").insert(prompt.dict()).execute()).data | ||
|
||
return Prompt(**response[0]) | ||
|
||
def delete_prompt_by_id(self, prompt_id: UUID) -> Prompt | None: | ||
""" | ||
Delete a prompt by id | ||
Args: | ||
prompt_id (UUID): The id of the prompt | ||
Returns: | ||
Prompt: The prompt | ||
""" | ||
response = ( | ||
self.db.from_("prompts") | ||
.delete() | ||
.filter("id", "eq", prompt_id) | ||
.execute() | ||
.data | ||
) | ||
if response == []: | ||
raise HTTPException(404, "Prompt not found") | ||
return Prompt(**response[0]) | ||
|
||
def get_prompt_by_id(self, prompt_id: UUID) -> Prompt | None: | ||
""" | ||
Get a prompt by its id | ||
Args: | ||
prompt_id (UUID): The id of the prompt | ||
Returns: | ||
Prompt: The prompt | ||
""" | ||
|
||
response = ( | ||
self.db.from_("prompts").select("*").filter("id", "eq", prompt_id).execute() | ||
).data | ||
|
||
if response == []: | ||
return None | ||
return Prompt(**response[0]) | ||
|
||
def get_public_prompts(self) -> list[Prompt]: | ||
""" | ||
List all public prompts | ||
""" | ||
|
||
return ( | ||
self.db.from_("prompts") | ||
.select("*") | ||
.filter("status", "eq", "public") | ||
.execute() | ||
).data | ||
|
||
def update_prompt_by_id( | ||
self, prompt_id: UUID, prompt: PromptUpdatableProperties | ||
) -> Prompt: | ||
"""Update a prompt by id""" | ||
|
||
response = ( | ||
self.db.from_("prompts") | ||
.update(prompt.dict(exclude_unset=True)) | ||
.filter("id", "eq", prompt_id) | ||
.execute() | ||
).data | ||
|
||
if response == []: | ||
raise HTTPException(404, "Prompt not found") | ||
|
||
return Prompt(**response[0]) |
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,10 @@ | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class Prompt(BaseModel): | ||
title: str | ||
content: str | ||
status: str = "private" | ||
id: UUID |
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,10 @@ | ||
from models.databases.supabase.prompts import CreatePromptProperties | ||
from models.prompt import Prompt | ||
from models.settings import common_dependencies | ||
|
||
|
||
def create_prompt(prompt: CreatePromptProperties) -> Prompt: | ||
"""Create a prompt by id""" | ||
commons = common_dependencies() | ||
|
||
return commons["db"].create_prompt(prompt) |
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,17 @@ | ||
from uuid import UUID | ||
|
||
from models.prompt import Prompt | ||
from models.settings import common_dependencies | ||
|
||
|
||
def delete_prompt_by_id(prompt_id: UUID) -> Prompt | None: | ||
""" | ||
Delete a prompt by id | ||
Args: | ||
prompt_id (UUID): The id of the prompt | ||
Returns: | ||
Prompt: The prompt | ||
""" | ||
commons = common_dependencies() | ||
return commons["db"].delete_prompt_by_id(prompt_id) |
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,18 @@ | ||
from uuid import UUID | ||
|
||
from models.prompt import Prompt | ||
from models.settings import common_dependencies | ||
|
||
|
||
def get_prompt_by_id(prompt_id: UUID) -> Prompt | None: | ||
""" | ||
Get a prompt by its id | ||
Args: | ||
prompt_id (UUID): The id of the prompt | ||
Returns: | ||
Prompt: The prompt | ||
""" | ||
commons = common_dependencies() | ||
return commons["db"].get_prompt_by_id(prompt_id) |
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,10 @@ | ||
from models.prompt import Prompt | ||
from models.settings import common_dependencies | ||
|
||
|
||
def get_public_prompts() -> list[Prompt]: | ||
""" | ||
List all public prompts | ||
""" | ||
commons = common_dependencies() | ||
return commons["db"].get_public_prompts() |
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,12 @@ | ||
from uuid import UUID | ||
|
||
from models.databases.supabase.prompts import PromptUpdatableProperties | ||
from models.prompt import Prompt | ||
from models.settings import common_dependencies | ||
|
||
|
||
def update_prompt_by_id(prompt_id: UUID, prompt: PromptUpdatableProperties) -> Prompt: | ||
"""Update a prompt by id""" | ||
commons = common_dependencies() | ||
|
||
return commons["db"].update_prompt_by_id(prompt_id, prompt) |
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,15 @@ | ||
from auth import AuthBearer | ||
from fastapi import APIRouter, Depends | ||
from models.prompt import Prompt | ||
from repository.prompt.get_public_prompts import get_public_prompts | ||
|
||
prompt_router = APIRouter() | ||
|
||
|
||
@prompt_router.get("/prompts", dependencies=[Depends(AuthBearer())], tags=["Prompt"]) | ||
async def get_prompts() -> list[Prompt]: | ||
""" | ||
Retrieve all public prompt | ||
""" | ||
|
||
return get_public_prompts() |
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,19 @@ | ||
BEGIN; | ||
|
||
-- Create user_identity table if it doesn't exist | ||
CREATE TABLE IF NOT EXISTS prompts ( | ||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, | ||
title VARCHAR(255), | ||
content TEXT, | ||
status VARCHAR(255) DEFAULT 'private' | ||
); | ||
|
||
|
||
-- Insert migration record if it doesn't exist | ||
INSERT INTO migrations (name) | ||
SELECT '20230701180101_add_prompts_table' | ||
WHERE NOT EXISTS ( | ||
SELECT 1 FROM migrations WHERE name = '20230701180101_add_prompts_table' | ||
); | ||
|
||
COMMIT; |
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