Skip to content

Commit

Permalink
refactor: Prompt module (#1688)
Browse files Browse the repository at this point in the history
# Description

Prompt module with Service

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
  • Loading branch information
gozineb authored Nov 23, 2023
1 parent 18421ca commit 1bf67e3
Show file tree
Hide file tree
Showing 27 changed files with 209 additions and 184 deletions.
2 changes: 1 addition & 1 deletion backend/llm/qa_headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from logger import get_logger
from models.chats import ChatQuestion
from models.databases.supabase.chats import CreateChatHistory
from models.prompt import Prompt
from modules.prompt.entity.prompt import Prompt
from pydantic import BaseModel
from repository.chat import (
GetChatHistoryOutput,
Expand Down
8 changes: 5 additions & 3 deletions backend/llm/utils/get_prompt_to_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from uuid import UUID

from llm.utils.get_prompt_to_use_id import get_prompt_to_use_id
from models.prompt import Prompt
from repository.prompt import get_prompt_by_id
from modules.prompt.entity.prompt import Prompt
from modules.prompt.service import PromptService

promptService = PromptService()


def get_prompt_to_use(
Expand All @@ -13,4 +15,4 @@ def get_prompt_to_use(
if prompt_to_use_id is None:
return None

return get_prompt_by_id(prompt_to_use_id)
return promptService.get_prompt_by_id(prompt_to_use_id)
2 changes: 1 addition & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from fastapi.responses import JSONResponse
from logger import get_logger
from middlewares.cors import add_cors_middleware
from modules.prompt.controller.prompt_routes import prompt_router
from modules.user.controller.user_controller import user_router
from routes.api_key_routes import api_key_router
from routes.brain_routes import brain_router
Expand All @@ -24,7 +25,6 @@
from routes.misc_routes import misc_router
from routes.notification_routes import notification_router
from routes.onboarding_routes import onboarding_router
from routes.prompt_routes import prompt_router
from routes.subscription_routes import subscription_router
from routes.upload_routes import upload_router

Expand Down
6 changes: 2 additions & 4 deletions backend/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from .chat import Chat, ChatHistory
from .chats import ChatMessage, ChatQuestion
from .files import File
from .prompt import Prompt, PromptStatusEnum
from .settings import (BrainRateLimiting, BrainSettings, ContactsSettings,
ResendSettings, get_embeddings,
get_documents_vector_store, get_embeddings,
get_supabase_client, get_supabase_db)
ResendSettings, get_documents_vector_store,
get_embeddings, get_supabase_client, get_supabase_db)
from .user_usage import UserUsage

# TODO uncomment the below import when start using SQLalchemy
Expand Down
20 changes: 0 additions & 20 deletions backend/models/databases/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,6 @@ def get_vectors_in_batch(self, batch_ids):
def get_vectors_by_file_sha1(self, file_sha1):
pass

@abstractmethod
def create_prompt(self, new_prompt):
pass

@abstractmethod
def get_prompt_by_id(self, prompt_id: UUID):
pass

@abstractmethod
def delete_prompt_by_id(self, prompt_id: UUID):
pass

@abstractmethod
def update_prompt_by_id(self, prompt_id: UUID, updates):
pass

@abstractmethod
def get_public_prompts(self):
pass

@abstractmethod
def add_notification(self, notification):
pass
Expand Down
7 changes: 3 additions & 4 deletions backend/models/databases/supabase/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from models.databases.supabase.api_brain_definition import ApiBrainDefinitions
from models.databases.supabase.api_key_handler import ApiKeyHandler
from models.databases.supabase.brains import Brain
from models.databases.supabase.brains_subscription_invitations import BrainSubscription
from models.databases.supabase.brains_subscription_invitations import \
BrainSubscription
from models.databases.supabase.chats import Chats
from models.databases.supabase.files import File
from models.databases.supabase.knowledge import Knowledges
from models.databases.supabase.notifications import Notifications
from models.databases.supabase.onboarding import Onboarding
from models.databases.supabase.prompts import Prompts
from models.databases.supabase.user_usage import UserUsage
from models.databases.supabase.vectors import Vector

from models.databases.supabase.api_brain_definition import ApiBrainDefinitions
3 changes: 0 additions & 3 deletions backend/models/databases/supabase/supabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Knowledges,
Notifications,
Onboarding,
Prompts,
UserUsage,
Vector,
)
Expand All @@ -26,7 +25,6 @@ class SupabaseDB(
Chats,
Vector,
Onboarding,
Prompts,
Notifications,
Knowledges,
ApiBrainDefinitions,
Expand All @@ -40,7 +38,6 @@ def __init__(self, supabase_client):
ApiKeyHandler.__init__(self, supabase_client)
Chats.__init__(self, supabase_client)
Vector.__init__(self, supabase_client)
Prompts.__init__(self, supabase_client)
Notifications.__init__(self, supabase_client)
Knowledges.__init__(self, supabase_client)
Onboarding.__init__(self, supabase_client)
Expand Down
16 changes: 0 additions & 16 deletions backend/models/prompt.py

This file was deleted.

Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

from fastapi import APIRouter, Depends
from middlewares.auth import AuthBearer
from models import Prompt
from models.databases.supabase.prompts import (
from modules.prompt.entity.prompt import (
CreatePromptProperties,
Prompt,
PromptUpdatableProperties,
)
from repository.prompt import (
create_prompt,
get_prompt_by_id,
get_public_prompts,
update_prompt_by_id,
)
from modules.prompt.service import PromptService

prompt_router = APIRouter()

promptService = PromptService()


@prompt_router.get("/prompts", dependencies=[Depends(AuthBearer())], tags=["Prompt"])
async def get_prompts() -> list[Prompt]:
"""
Retrieve all public prompt
"""

return get_public_prompts()
return promptService.get_public_prompts()


@prompt_router.get(
Expand All @@ -34,7 +30,7 @@ async def get_prompt(prompt_id: UUID) -> Prompt | None:
Retrieve a prompt by its id
"""

return get_prompt_by_id(prompt_id)
return promptService.get_prompt_by_id(prompt_id)


@prompt_router.put(
Expand All @@ -47,7 +43,7 @@ async def update_prompt(
Update a prompt by its id
"""

return update_prompt_by_id(prompt_id, prompt)
return promptService.update_prompt_by_id(prompt_id, prompt)


@prompt_router.post("/prompts", dependencies=[Depends(AuthBearer())], tags=["Prompt"])
Expand All @@ -56,4 +52,4 @@ async def create_prompt_route(prompt: CreatePromptProperties) -> Prompt | None:
Create a prompt by its id
"""

return create_prompt(prompt)
return promptService.create_prompt(prompt)
1 change: 1 addition & 0 deletions backend/modules/prompt/entity/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .prompt import Prompt, PromptStatusEnum, CreatePromptProperties, PromptUpdatableProperties, DeletePromptResponse
40 changes: 40 additions & 0 deletions backend/modules/prompt/entity/prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from enum import Enum
from typing import Optional
from uuid import UUID

from pydantic import BaseModel


class PromptStatusEnum(str, Enum):
private = "private"
public = "public"


class Prompt(BaseModel):
title: str
content: str
status: PromptStatusEnum = PromptStatusEnum.private
id: UUID


class CreatePromptProperties(BaseModel):
"""Properties that can be received on prompt creation"""

title: str
content: str
status: PromptStatusEnum = PromptStatusEnum.private


class PromptUpdatableProperties(BaseModel):
"""Properties that can be received on prompt update"""

title: Optional[str]
content: Optional[str]
status: Optional[PromptStatusEnum]


class DeletePromptResponse(BaseModel):
"""Response when deleting a prompt"""

status: str = "delete"
prompt_id: UUID
Empty file.
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
from typing import Optional
from uuid import UUID

from fastapi import HTTPException
from models.databases.repository import Repository
from models.prompt import Prompt, PromptStatusEnum
from pydantic import BaseModel


class CreatePromptProperties(BaseModel):
"""Properties that can be received on prompt creation"""

title: str
content: str
status: PromptStatusEnum = PromptStatusEnum.private


class PromptUpdatableProperties(BaseModel):
"""Properties that can be received on prompt update"""

title: Optional[str]
content: Optional[str]
status: Optional[PromptStatusEnum]


class DeletePromptResponse(BaseModel):
"""Response when deleting a prompt"""

status: str = "delete"
prompt_id: UUID
from modules.prompt.entity.prompt import Prompt
from modules.prompt.repository.prompts_interface import (
DeletePromptResponse,
PromptsInterface,
)


class Prompts(Repository):
class Prompts(PromptsInterface):
def __init__(self, supabase_client):
self.db = supabase_client

def create_prompt(self, prompt: CreatePromptProperties) -> Prompt:
def create_prompt(self, prompt):
"""
Create a prompt
"""
Expand All @@ -43,7 +19,7 @@ def create_prompt(self, prompt: CreatePromptProperties) -> Prompt:

return Prompt(**response[0])

def delete_prompt_by_id(self, prompt_id: UUID) -> DeletePromptResponse:
def delete_prompt_by_id(self, prompt_id):
"""
Delete a prompt by id
Args:
Expand All @@ -65,7 +41,7 @@ def delete_prompt_by_id(self, prompt_id: UUID) -> DeletePromptResponse:

return DeletePromptResponse(status="deleted", prompt_id=prompt_id)

def get_prompt_by_id(self, prompt_id: UUID) -> Prompt | None:
def get_prompt_by_id(self, prompt_id):
"""
Get a prompt by its id
Expand All @@ -84,7 +60,7 @@ def get_prompt_by_id(self, prompt_id: UUID) -> Prompt | None:
return None
return Prompt(**response[0])

def get_public_prompts(self) -> list[Prompt]:
def get_public_prompts(self):
"""
List all public prompts
"""
Expand All @@ -96,9 +72,7 @@ def get_public_prompts(self) -> list[Prompt]:
.execute()
).data

def update_prompt_by_id(
self, prompt_id: UUID, prompt: PromptUpdatableProperties
) -> Prompt:
def update_prompt_by_id(self, prompt_id, prompt):
"""Update a prompt by id"""

response = (
Expand Down
57 changes: 57 additions & 0 deletions backend/modules/prompt/repository/prompts_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from abc import ABC, abstractmethod
from uuid import UUID

from modules.prompt.entity import (
CreatePromptProperties,
DeletePromptResponse,
Prompt,
PromptUpdatableProperties,
)


class PromptsInterface(ABC):
@abstractmethod
def create_prompt(self, prompt: CreatePromptProperties) -> Prompt:
"""
Create a prompt
"""
pass

@abstractmethod
def delete_prompt_by_id(self, prompt_id: UUID) -> DeletePromptResponse:
"""
Delete a prompt by id
Args:
prompt_id (UUID): The id of the prompt
Returns:
A dictionary containing the status of the delete and prompt_id of the deleted prompt
"""
pass

@abstractmethod
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
"""
pass

@abstractmethod
def get_public_prompts(self) -> list[Prompt]:
"""
List all public prompts
"""
pass

@abstractmethod
def update_prompt_by_id(
self, prompt_id: UUID, prompt: PromptUpdatableProperties
) -> Prompt:
"""Update a prompt by id"""
pass
1 change: 1 addition & 0 deletions backend/modules/prompt/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .prompt_service import PromptService
Loading

0 comments on commit 1bf67e3

Please sign in to comment.