-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user level open ai key management (#805)
* feat: add user user identity table * feat: add user openai api key input * feat: add encryption missing message * chore: log more details about 422 errors * docs(API): update api creation path * feat: use user openai key if defined
- Loading branch information
1 parent
b72139a
commit 7532b55
Showing
20 changed files
with
452 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class UserIdentity(BaseModel): | ||
user_id: UUID | ||
openai_api_key: Optional[str] = None |
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
13 changes: 13 additions & 0 deletions
13
backend/core/repository/user_identity/create_user_identity.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,13 @@ | ||
from models.settings import common_dependencies | ||
from models.user_identity import UserIdentity | ||
|
||
|
||
def create_user_identity(user_identity: UserIdentity) -> UserIdentity: | ||
commons = common_dependencies() | ||
user_identity_dict = user_identity.dict() | ||
user_identity_dict["user_id"] = str(user_identity.user_id) | ||
response = ( | ||
commons["supabase"].from_("user_identity").insert(user_identity_dict).execute() | ||
) | ||
|
||
return UserIdentity(**response.data[0]) |
21 changes: 21 additions & 0 deletions
21
backend/core/repository/user_identity/get_user_identity.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,21 @@ | ||
from uuid import UUID | ||
|
||
from models.settings import common_dependencies | ||
from models.user_identity import UserIdentity | ||
from repository.user_identity.create_user_identity import create_user_identity | ||
|
||
|
||
def get_user_identity(user_id: UUID) -> UserIdentity: | ||
commons = common_dependencies() | ||
response = ( | ||
commons["supabase"] | ||
.from_("user_identity") | ||
.select("*") | ||
.filter("user_id", "eq", user_id) | ||
.execute() | ||
) | ||
|
||
if len(response.data) == 0: | ||
return create_user_identity(UserIdentity(user_id=user_id)) | ||
|
||
return UserIdentity(**response.data[0]) |
36 changes: 36 additions & 0 deletions
36
backend/core/repository/user_identity/update_user_identity.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,36 @@ | ||
from typing import Optional | ||
from uuid import UUID | ||
|
||
from models.settings import common_dependencies | ||
from models.user_identity import UserIdentity | ||
from pydantic import BaseModel | ||
from repository.user_identity.create_user_identity import ( | ||
create_user_identity, | ||
) | ||
|
||
|
||
class UserIdentityUpdatableProperties(BaseModel): | ||
openai_api_key: Optional[str] | ||
|
||
|
||
def update_user_identity( | ||
user_id: UUID, | ||
user_identity_updatable_properties: UserIdentityUpdatableProperties, | ||
) -> UserIdentity: | ||
commons = common_dependencies() | ||
response = ( | ||
commons["supabase"] | ||
.from_("user_identity") | ||
.update(user_identity_updatable_properties.__dict__) | ||
.filter("user_id", "eq", user_id) | ||
.execute() | ||
) | ||
|
||
if len(response.data) == 0: | ||
user_identity = UserIdentity( | ||
user_id=user_id, | ||
openai_api_key=user_identity_updatable_properties.openai_api_key, | ||
) | ||
return create_user_identity(user_identity) | ||
|
||
return UserIdentity(**response.data[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
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
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
Oops, something went wrong.