Skip to content

Commit

Permalink
feat: 🎸 openai (#1658)
Browse files Browse the repository at this point in the history
cleaning old code to introduce better patern

# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## 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
StanGirard authored Nov 20, 2023
1 parent d955e31 commit 6a041b6
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 56 deletions.
4 changes: 1 addition & 3 deletions .backend_env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ GOOGLE_CLOUD_PROJECT=<change-me>
CELERY_BROKER_URL=redis://redis:6379/0
CELEBRY_BROKER_QUEUE_NAME=quivr-preview.fifo

#Private LLM Variables
PRIVATE=False
MODEL_PATH=./local_models/ggml-gpt4all-j-v1.3-groovy.bin


#RESEND
RESEND_API_KEY=<change-me>
Expand Down
6 changes: 1 addition & 5 deletions backend/llm/qa_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class Config:
max_tokens: int = 256
streaming: bool = False

openai_api_key: str = None # pyright: ignore reportPrivateUsage=none
callbacks: List[
AsyncIteratorCallbackHandler
] = None # pyright: ignore reportPrivateUsage=none
Expand All @@ -85,9 +84,7 @@ def _determine_callback_array(

@property
def embeddings(self) -> OpenAIEmbeddings:
return OpenAIEmbeddings(
openai_api_key=self.openai_api_key
) # pyright: ignore reportPrivateUsage=none
return OpenAIEmbeddings() # pyright: ignore reportPrivateUsage=none

supabase_client: Optional[Client] = None
vector_store: Optional[CustomSupabaseVectorStore] = None
Expand Down Expand Up @@ -152,7 +149,6 @@ def _create_llm(
streaming=streaming,
verbose=False,
callbacks=callbacks,
openai_api_key=self.openai_api_key,
) # pyright: ignore reportPrivateUsage=none

def _create_prompt_template(self):
Expand Down
2 changes: 0 additions & 2 deletions backend/llm/qa_headless.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class HeadlessQA(BaseModel):
model: str
temperature: float = 0.0
max_tokens: int = 2000
openai_api_key: Optional[str] = None
streaming: bool = False
chat_id: str
callbacks: Optional[List[AsyncIteratorCallbackHandler]] = None
Expand Down Expand Up @@ -80,7 +79,6 @@ def _create_llm(
streaming=streaming,
verbose=True,
callbacks=callbacks,
openai_api_key=self.openai_api_key,
)

def _create_prompt_template(self):
Expand Down
2 changes: 1 addition & 1 deletion backend/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .files import File
from .prompt import Prompt, PromptStatusEnum
from .settings import (BrainRateLimiting, BrainSettings, ContactsSettings,
LLMSettings, ResendSettings, get_embeddings,
ResendSettings, get_embeddings,
get_documents_vector_store, get_embeddings,
get_supabase_client, get_supabase_db)
from .user_usage import UserUsage
Expand Down
6 changes: 0 additions & 6 deletions backend/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class BrainSettings(BaseSettings):
openai_api_key: str
supabase_url: str
supabase_service_key: str
pg_database_url: str = "not implemented"
resend_api_key: str = "null"
resend_email_address: str = "brain@mail.quivr.app"

Expand All @@ -23,11 +22,6 @@ class ContactsSettings(BaseSettings):
resend_contact_sales_to: str = "null"


class LLMSettings(BaseSettings):
private: bool = False
model_path: str = "./local_models/ggml-gpt4all-j-v1.3-groovy.bin"


class ResendSettings(BaseSettings):
resend_api_key: str = "null"

Expand Down
6 changes: 2 additions & 4 deletions backend/modules/user/repository/create_user_identity.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
from typing import Optional
from uuid import UUID

from models import get_supabase_client
from modules.user.entity.user_identity import UserIdentity


def create_user_identity(id: UUID, openai_api_key: Optional[str]) -> UserIdentity:
def create_user_identity(id: UUID) -> UserIdentity:
supabase_client = get_supabase_client()

response = (
supabase_client.from_("user_identity")
.insert(
{
"user_id": str(id),
"openai_api_key": openai_api_key,
}
)
.execute()
)
user_identity = response.data[0]
return UserIdentity(
id=user_identity.get("user_id"), openai_api_key=user_identity.get("openai_api_key") # type: ignore
id=user_identity.get("user_id")
)
5 changes: 2 additions & 3 deletions backend/modules/user/repository/get_user_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def get_user_identity(user_id: UUID) -> UserIdentity:
)

if len(response.data) == 0:
return create_user_identity(user_id, openai_api_key=None)
return create_user_identity(user_id)

user_identity = response.data[0]
openai_api_key = user_identity["openai_api_key"]

return UserIdentity(id=user_id, openai_api_key=openai_api_key)
return UserIdentity(id=user_id)
10 changes: 5 additions & 5 deletions backend/modules/user/repository/update_user_properties.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Optional
from uuid import UUID

from models.settings import get_supabase_client
Expand All @@ -8,7 +7,8 @@


class UserUpdatableProperties(BaseModel):
openai_api_key: Optional[str]
# Nothing for now
empty: bool = True


def update_user_properties(
Expand All @@ -25,10 +25,10 @@ def update_user_properties(

if len(response.data) == 0:
return create_user_identity(
user_id, openai_api_key=user_identity_updatable_properties.openai_api_key
user_id
)

user_identity = response.data[0]
openai_api_key = user_identity["openai_api_key"]

return UserIdentity(id=user_id, openai_api_key=openai_api_key)

return UserIdentity(id=user_id)
6 changes: 0 additions & 6 deletions backend/routes/chat/brainful_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from llm.api_brain_qa import APIBrainQA
from llm.qa_base import QABaseBrainPicking
from models.brain_entity import BrainType
from repository.brain import get_brain_details
from repository.brain.get_brain_by_id import get_brain_by_id
from routes.authorizations.brain_authorization import validate_brain_authorization
from routes.authorizations.types import RoleEnum
Expand All @@ -27,11 +26,6 @@ def validate_authorization(self, user_id, brain_id):
required_roles=[RoleEnum.Viewer, RoleEnum.Editor, RoleEnum.Owner],
)

def get_openai_api_key(self, brain_id, user_id):
brain_details = get_brain_details(brain_id)
if brain_details:
return brain_details.openai_api_key

def get_answer_generator(
self,
brain_id,
Expand Down
7 changes: 0 additions & 7 deletions backend/routes/chat/brainless_chat.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
from llm.qa_headless import HeadlessQA
from modules.user.repository import get_user_identity
from routes.chat.interface import ChatInterface


class BrainlessChat(ChatInterface):
def validate_authorization(self, user_id, brain_id):
pass

def get_openai_api_key(self, brain_id, user_id):
user_identity = get_user_identity(user_id)

if user_identity is not None:
return user_identity.openai_api_key

def get_answer_generator(
self,
brain_id,
Expand Down
4 changes: 0 additions & 4 deletions backend/routes/chat/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class ChatInterface(ABC):
def validate_authorization(self, user_id, required_roles):
pass

@abstractmethod
def get_openai_api_key(self, brain_id, user_id):
pass

@abstractmethod
def get_answer_generator(
self,
Expand Down
1 change: 0 additions & 1 deletion backend/routes/chat_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ async def create_stream_question_handler(
chat_instance = get_chat_strategy(brain_id)
chat_instance.validate_authorization(user_id=current_user.id, brain_id=brain_id)

# Retrieve user's OpenAI API key
brain = Brain(id=brain_id)
brain_details: BrainEntity | None = None
userDailyUsage = UserUsage(
Expand Down
4 changes: 0 additions & 4 deletions backend/routes/crawl_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ async def crawl_endpoint(
)
userSettings = userDailyUsage.get_user_settings()

# [TODO] rate limiting of user for crawl
if request.headers.get("Openai-Api-Key"):
brain.max_brain_size = userSettings.get("max_brain_size", 1000000000)

file_size = 1000000
remaining_free_space = userSettings.get("max_brain_size", 1000000000)

Expand Down
3 changes: 0 additions & 3 deletions backend/routes/upload_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ async def upload_file(
)
userSettings = userDailyUsage.get_user_settings()

if request.headers.get("Openai-Api-Key"):
brain.max_brain_size = userSettings.get("max_brain_size", 1000000000)

remaining_free_space = userSettings.get("max_brain_size", 1000000000)

file_size = get_file_size(uploadFile)
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/api/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UUID } from "crypto";
import { UserStats } from "@/lib/types/User";

export type UserIdentityUpdatableProperties = {
openai_api_key?: string | null;
empty?: string | null;
};

export type UserIdentity = {
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/locales/en/home.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"intro": {
"title": "Get a Second Brain with",
"subtitle": "Upload all your files and start talking with them.",
"subtitle": "Your productivity assistant connected to your files & favorite applications",
"try_demo": "Try free demo",
"contact_sales": "Contact sales team"
},
Expand Down
19 changes: 19 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"config:base"
],
"semanticCommits": true,
"stabilityDays": 3,
"prCreation": "not-pending",
"labels": [
"type: dependencies"
],
"packageRules": [
{
"packageNames": [
"node"
],
"enabled": false
}
]
}

0 comments on commit 6a041b6

Please sign in to comment.