mirror of
https://github.com/StanGirard/quivr.git
synced 2024-11-11 08:24:38 +03:00
feat: add api_brain_definition table (#1601)
Issue: https://github.com/StanGirard/quivr/issues/1549
This commit is contained in:
parent
c7ed9523f3
commit
9382762d01
12
backend/models/ApiBrainDefinition.py
Normal file
12
backend/models/ApiBrainDefinition.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class ApiBrainDefinition(BaseModel):
|
||||||
|
brain_id: UUID
|
||||||
|
method: str
|
||||||
|
url: str
|
||||||
|
params: dict
|
||||||
|
search_params: dict
|
||||||
|
secrets: dict
|
@ -279,3 +279,15 @@ class Repository(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def remove_onboarding_more_than_x_days(self, days: int):
|
def remove_onboarding_more_than_x_days(self, days: int):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_api_brain_definition(self, brain_id: UUID):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_api_brain_definition(self, brain_id: UUID, config):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def delete_api_brain_definition(self, brain_id: UUID):
|
||||||
|
pass
|
||||||
|
@ -9,3 +9,5 @@ from models.databases.supabase.onboarding import Onboarding
|
|||||||
from models.databases.supabase.prompts import Prompts
|
from models.databases.supabase.prompts import Prompts
|
||||||
from models.databases.supabase.user_usage import UserUsage
|
from models.databases.supabase.user_usage import UserUsage
|
||||||
from models.databases.supabase.vectors import Vector
|
from models.databases.supabase.vectors import Vector
|
||||||
|
|
||||||
|
from models.databases.supabase.api_brain_definition import ApiBrainDefinitions
|
||||||
|
55
backend/models/databases/supabase/api_brain_definition.py
Normal file
55
backend/models/databases/supabase/api_brain_definition.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
from enum import Enum
|
||||||
|
from typing import Optional
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from models.ApiBrainDefinition import ApiBrainDefinition
|
||||||
|
from models.databases.repository import Repository
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class ApiMethod(str, Enum):
|
||||||
|
GET = "GET"
|
||||||
|
POST = "POST"
|
||||||
|
PUT = "PUT"
|
||||||
|
DELETE = "DELETE"
|
||||||
|
|
||||||
|
|
||||||
|
class CreateApiBrainDefinition(BaseModel):
|
||||||
|
brain_id: UUID
|
||||||
|
method: ApiMethod
|
||||||
|
url: str
|
||||||
|
params: dict
|
||||||
|
search_params: dict
|
||||||
|
secrets: dict
|
||||||
|
|
||||||
|
|
||||||
|
class ApiBrainDefinitions(Repository):
|
||||||
|
def __init__(self, supabase_client):
|
||||||
|
self.db = supabase_client
|
||||||
|
|
||||||
|
def get_api_brain_definition(self, brain_id: UUID) -> Optional[ApiBrainDefinition]:
|
||||||
|
response = (
|
||||||
|
self.db.table("api_brain_definition")
|
||||||
|
.select("*")
|
||||||
|
.filter("brain_id", "eq", brain_id)
|
||||||
|
.execute()
|
||||||
|
)
|
||||||
|
if len(response.data) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return ApiBrainDefinition(**response.data[0])
|
||||||
|
|
||||||
|
def add_api_brain_definition(
|
||||||
|
self, brain_id: UUID, config: CreateApiBrainDefinition
|
||||||
|
) -> Optional[ApiBrainDefinition]:
|
||||||
|
response = self.db.table("api_brain_definition").insert(
|
||||||
|
[{"brain_id": str(brain_id), **config.dict()}]
|
||||||
|
)
|
||||||
|
if len(response.data) == 0:
|
||||||
|
return None
|
||||||
|
return ApiBrainDefinition(**response.data[0])
|
||||||
|
|
||||||
|
def delete_api_brain_definition(self, brain_id: UUID) -> None:
|
||||||
|
self.db.table("api_brain_definition").delete().filter(
|
||||||
|
"brain_id", "eq", str(brain_id)
|
||||||
|
).execute()
|
@ -3,9 +3,7 @@ from typing import Optional
|
|||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from models.databases.repository import (
|
from models.databases.repository import Repository
|
||||||
Repository, # Assuming you have a repository class
|
|
||||||
)
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from logger import get_logger
|
from logger import get_logger
|
||||||
from models.databases.supabase import (
|
from models.databases.supabase import (
|
||||||
|
ApiBrainDefinitions,
|
||||||
ApiKeyHandler,
|
ApiKeyHandler,
|
||||||
Brain,
|
Brain,
|
||||||
BrainSubscription,
|
BrainSubscription,
|
||||||
@ -28,6 +29,7 @@ class SupabaseDB(
|
|||||||
Prompts,
|
Prompts,
|
||||||
Notifications,
|
Notifications,
|
||||||
Knowledges,
|
Knowledges,
|
||||||
|
ApiBrainDefinitions,
|
||||||
):
|
):
|
||||||
def __init__(self, supabase_client):
|
def __init__(self, supabase_client):
|
||||||
self.db = supabase_client
|
self.db = supabase_client
|
||||||
@ -42,3 +44,4 @@ class SupabaseDB(
|
|||||||
Notifications.__init__(self, supabase_client)
|
Notifications.__init__(self, supabase_client)
|
||||||
Knowledges.__init__(self, supabase_client)
|
Knowledges.__init__(self, supabase_client)
|
||||||
Onboarding.__init__(self, supabase_client)
|
Onboarding.__init__(self, supabase_client)
|
||||||
|
ApiBrainDefinitions.__init__(self, supabase_client)
|
||||||
|
0
backend/repository/api_brain_definition/__init__.py
Normal file
0
backend/repository/api_brain_definition/__init__.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from models.databases.supabase.api_brain_definition import (
|
||||||
|
CreateApiBrainDefinition,
|
||||||
|
)
|
||||||
|
from models.settings import get_supabase_db
|
||||||
|
|
||||||
|
|
||||||
|
def add_api_brain_definition(
|
||||||
|
brain_id: UUID, api_brain_configs: CreateApiBrainDefinition
|
||||||
|
) -> None:
|
||||||
|
supabase_db = get_supabase_db()
|
||||||
|
|
||||||
|
supabase_db.add_api_brain_definition(brain_id, api_brain_configs)
|
@ -0,0 +1,9 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from models.settings import get_supabase_db
|
||||||
|
|
||||||
|
|
||||||
|
def delete_api_brain_definition(brain_id: UUID) -> None:
|
||||||
|
supabase_db = get_supabase_db()
|
||||||
|
|
||||||
|
supabase_db.delete_api_brain_definition(brain_id)
|
@ -0,0 +1,11 @@
|
|||||||
|
from typing import Optional
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from models import get_supabase_db
|
||||||
|
from models.ApiBrainDefinition import ApiBrainDefinition
|
||||||
|
|
||||||
|
|
||||||
|
def get_api_brain_definition(brain_id: UUID) -> Optional[ApiBrainDefinition]:
|
||||||
|
supabase_db = get_supabase_db()
|
||||||
|
|
||||||
|
return supabase_db.get_api_brain_definition(brain_id)
|
18
scripts/2023110607100000_add_api_brain_definition_table.sql
Normal file
18
scripts/2023110607100000_add_api_brain_definition_table.sql
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
-- Create the new table with 6 columns
|
||||||
|
CREATE TABLE IF NOT EXISTS api_brain_definition (
|
||||||
|
brain_id UUID REFERENCES brains(brain_id),
|
||||||
|
method VARCHAR(255) CHECK (method IN ('GET', 'POST', 'PUT', 'DELETE')),
|
||||||
|
url VARCHAR(255),
|
||||||
|
params JSON,
|
||||||
|
search_params JSON,
|
||||||
|
secrets JSON
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Insert migration record if it doesn't exist
|
||||||
|
INSERT INTO migrations (name)
|
||||||
|
SELECT '2023110607100000_add_api_brain_definition_table'
|
||||||
|
WHERE NOT EXISTS (
|
||||||
|
SELECT 1 FROM migrations WHERE name = '2023110607100000_add_api_brain_definition_table'
|
||||||
|
);
|
||||||
|
|
||||||
|
COMMIT;
|
@ -201,6 +201,15 @@ CREATE TABLE IF NOT EXISTS user_identity (
|
|||||||
openai_api_key VARCHAR(255)
|
openai_api_key VARCHAR(255)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- Create the new table with 6 columns
|
||||||
|
CREATE TABLE IF NOT EXISTS api_brain_definition (
|
||||||
|
brain_id UUID REFERENCES brains(brain_id),
|
||||||
|
method VARCHAR(255) CHECK (method IN ('GET', 'POST', 'PUT', 'DELETE')),
|
||||||
|
url VARCHAR(255),
|
||||||
|
params JSON,
|
||||||
|
search_params JSON,
|
||||||
|
secrets JSON
|
||||||
|
);
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION public.get_user_email_by_user_id(user_id uuid)
|
CREATE OR REPLACE FUNCTION public.get_user_email_by_user_id(user_id uuid)
|
||||||
RETURNS TABLE (email text)
|
RETURNS TABLE (email text)
|
||||||
@ -381,9 +390,9 @@ CREATE POLICY "Access Quivr Storage 1jccrwz_2" ON storage.objects FOR UPDATE TO
|
|||||||
CREATE POLICY "Access Quivr Storage 1jccrwz_3" ON storage.objects FOR DELETE TO anon USING (bucket_id = 'quivr');
|
CREATE POLICY "Access Quivr Storage 1jccrwz_3" ON storage.objects FOR DELETE TO anon USING (bucket_id = 'quivr');
|
||||||
|
|
||||||
INSERT INTO migrations (name)
|
INSERT INTO migrations (name)
|
||||||
SELECT '20231023160000_copy_auth_users_to_public_users'
|
SELECT '2023110607100000_add_api_brain_definition_table'
|
||||||
WHERE NOT EXISTS (
|
WHERE NOT EXISTS (
|
||||||
SELECT 1 FROM migrations WHERE name = '20231023160000_copy_auth_users_to_public_users'
|
SELECT 1 FROM migrations WHERE name = '2023110607100000_add_api_brain_definition_table'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user