2023-06-11 00:59:16 +03:00
|
|
|
import os
|
|
|
|
import time
|
2023-06-28 20:39:27 +03:00
|
|
|
from http.client import HTTPException
|
|
|
|
from typing import List
|
2023-06-11 00:59:16 +03:00
|
|
|
from uuid import UUID
|
2023-06-23 18:59:53 +03:00
|
|
|
|
2023-06-14 22:21:13 +03:00
|
|
|
from auth.auth_bearer import AuthBearer, get_current_user
|
2023-06-28 20:39:27 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
2023-06-20 01:14:40 +03:00
|
|
|
from llm.brainpicking import BrainPicking
|
2023-06-28 20:39:27 +03:00
|
|
|
from llm.BrainPickingOpenAIFunctions.BrainPickingOpenAIFunctions import \
|
|
|
|
BrainPickingOpenAIFunctions
|
2023-06-26 11:34:03 +03:00
|
|
|
from llm.PrivateBrainPicking import PrivateBrainPicking
|
2023-06-23 18:59:53 +03:00
|
|
|
from models.chat import Chat, ChatHistory
|
2023-06-22 18:50:06 +03:00
|
|
|
from models.chats import ChatQuestion
|
2023-06-26 11:34:03 +03:00
|
|
|
from models.settings import LLMSettings, common_dependencies
|
2023-06-11 00:59:16 +03:00
|
|
|
from models.users import User
|
2023-06-23 18:59:53 +03:00
|
|
|
from repository.chat.create_chat import CreateChatProperties, create_chat
|
2023-06-22 18:50:06 +03:00
|
|
|
from repository.chat.get_chat_by_id import get_chat_by_id
|
2023-06-23 18:59:53 +03:00
|
|
|
from repository.chat.get_chat_history import get_chat_history
|
2023-06-22 18:50:06 +03:00
|
|
|
from repository.chat.get_user_chats import get_user_chats
|
2023-06-23 18:59:53 +03:00
|
|
|
from repository.chat.update_chat import ChatUpdatableProperties, update_chat
|
2023-06-22 18:50:06 +03:00
|
|
|
from repository.chat.update_chat_history import update_chat_history
|
|
|
|
|
|
|
|
chat_router = APIRouter()
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def get_chat_details(commons, chat_id):
|
2023-06-20 10:54:23 +03:00
|
|
|
response = (
|
|
|
|
commons["supabase"]
|
|
|
|
.from_("chats")
|
|
|
|
.select("*")
|
|
|
|
.filter("chat_id", "eq", chat_id)
|
|
|
|
.execute()
|
|
|
|
)
|
2023-06-12 18:58:05 +03:00
|
|
|
return response.data
|
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def delete_chat_from_db(commons, chat_id):
|
2023-06-20 10:54:23 +03:00
|
|
|
commons["supabase"].table("chats").delete().match({"chat_id": chat_id}).execute()
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
# get all chats
|
2023-06-15 15:43:40 +03:00
|
|
|
@chat_router.get("/chat", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
2023-06-19 23:46:25 +03:00
|
|
|
async def get_chats(current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve all chats for the current user.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns a list of all chats for the user.
|
|
|
|
|
|
|
|
This endpoint retrieves all the chats associated with the current authenticated user. It returns a list of chat objects
|
|
|
|
containing the chat ID and chat name for each chat.
|
|
|
|
"""
|
2023-06-19 23:46:25 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-28 20:39:27 +03:00
|
|
|
chats = get_user_chats( current_user.id)
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"chats": chats}
|
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
# delete one chat
|
2023-06-22 18:50:06 +03:00
|
|
|
@chat_router.delete(
|
|
|
|
"/chat/{chat_id}", dependencies=[Depends(AuthBearer())], tags=["Chat"]
|
|
|
|
)
|
2023-06-20 10:54:23 +03:00
|
|
|
async def delete_chat(chat_id: UUID):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Delete a specific chat by chat ID.
|
|
|
|
"""
|
2023-06-19 23:46:25 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-12 18:58:05 +03:00
|
|
|
delete_chat_from_db(commons, chat_id)
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"message": f"{chat_id} has been deleted."}
|
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
# update existing chat metadata
|
|
|
|
@chat_router.put(
|
|
|
|
"/chat/{chat_id}/metadata", dependencies=[Depends(AuthBearer())], tags=["Chat"]
|
|
|
|
)
|
|
|
|
async def update_chat_metadata_handler(
|
|
|
|
chat_data: ChatUpdatableProperties,
|
|
|
|
chat_id: UUID,
|
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
) -> Chat:
|
|
|
|
"""
|
|
|
|
Update chat attributes
|
|
|
|
"""
|
|
|
|
commons = common_dependencies()
|
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
chat = get_chat_by_id(chat_id)
|
2023-06-28 20:39:27 +03:00
|
|
|
if current_user.id != chat.user_id:
|
2023-06-23 11:36:55 +03:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=403, detail="You should be the owner of the chat to update it."
|
|
|
|
)
|
2023-06-22 18:50:06 +03:00
|
|
|
return update_chat(chat_id=chat_id, chat_data=chat_data)
|
|
|
|
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
# helper method for update and create chat
|
2023-06-22 18:50:06 +03:00
|
|
|
def check_user_limit(
|
2023-06-28 20:39:27 +03:00
|
|
|
user : User,
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
):
|
2023-06-28 20:39:27 +03:00
|
|
|
if user.user_openai_api_key is None:
|
2023-06-23 18:59:53 +03:00
|
|
|
date = time.strftime("%Y%m%d")
|
|
|
|
max_requests_number = os.getenv("MAX_REQUESTS_NUMBER")
|
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
user.increment_user_request_count( date )
|
|
|
|
if user.requests_count >= float(max_requests_number):
|
2023-06-23 18:59:53 +03:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=429,
|
|
|
|
detail="You have reached the maximum number of requests for today.",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
pass
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
# create new chat
|
|
|
|
@chat_router.post("/chat", dependencies=[Depends(AuthBearer())], tags=["Chat"])
|
|
|
|
async def create_chat_handler(
|
|
|
|
chat_data: CreateChatProperties,
|
2023-06-20 10:54:23 +03:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
2023-06-22 18:50:06 +03:00
|
|
|
Create a new chat with initial chat messages.
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
return create_chat(user_id=current_user.id,chat_data=chat_data)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
# add new question to chat
|
|
|
|
@chat_router.post(
|
|
|
|
"/chat/{chat_id}/question", dependencies=[Depends(AuthBearer())], tags=["Chat"]
|
|
|
|
)
|
|
|
|
async def create_question_handler(
|
|
|
|
request: Request,
|
|
|
|
chat_question: ChatQuestion,
|
2023-06-20 10:54:23 +03:00
|
|
|
chat_id: UUID,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id: UUID = Query(..., description="The ID of the brain"),
|
2023-06-20 10:54:23 +03:00
|
|
|
current_user: User = Depends(get_current_user),
|
2023-06-22 18:50:06 +03:00
|
|
|
) -> ChatHistory:
|
2023-06-28 20:39:27 +03:00
|
|
|
current_user.user_openai_api_key = request.headers.get("Openai-Api-Key")
|
|
|
|
print("current_user", current_user)
|
2023-06-22 18:50:06 +03:00
|
|
|
try:
|
2023-06-28 20:39:27 +03:00
|
|
|
check_user_limit(current_user)
|
2023-06-26 11:34:03 +03:00
|
|
|
llm_settings = LLMSettings()
|
2023-06-22 18:50:06 +03:00
|
|
|
openai_function_compatible_models = [
|
|
|
|
"gpt-3.5-turbo-0613",
|
|
|
|
"gpt-4-0613",
|
|
|
|
]
|
2023-06-26 11:34:03 +03:00
|
|
|
if llm_settings.private:
|
|
|
|
gpt_answer_generator = PrivateBrainPicking(
|
|
|
|
model=chat_question.model,
|
|
|
|
chat_id=str(chat_id),
|
|
|
|
temperature=chat_question.temperature,
|
|
|
|
max_tokens=chat_question.max_tokens,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id = brain_id,
|
|
|
|
user_openai_api_key=current_user.user_openai_api_key,
|
2023-06-26 11:34:03 +03:00
|
|
|
)
|
|
|
|
answer = gpt_answer_generator.generate_answer(chat_question.question)
|
|
|
|
elif chat_question.model in openai_function_compatible_models:
|
2023-06-22 18:50:06 +03:00
|
|
|
# TODO: RBAC with current_user
|
2023-06-26 11:34:03 +03:00
|
|
|
gpt_answer_generator = BrainPickingOpenAIFunctions(
|
2023-06-22 18:50:06 +03:00
|
|
|
model=chat_question.model,
|
2023-06-26 11:34:03 +03:00
|
|
|
chat_id=str(chat_id),
|
2023-06-22 18:50:06 +03:00
|
|
|
temperature=chat_question.temperature,
|
|
|
|
max_tokens=chat_question.max_tokens,
|
|
|
|
# TODO: use user_id in vectors table instead of email
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id = brain_id,
|
|
|
|
user_openai_api_key=current_user.user_openai_api_key,
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
2023-06-26 11:34:03 +03:00
|
|
|
answer = gpt_answer_generator.generate_answer(chat_question.question)
|
2023-06-22 18:50:06 +03:00
|
|
|
else:
|
|
|
|
brainPicking = BrainPicking(
|
|
|
|
chat_id=str(chat_id),
|
|
|
|
model=chat_question.model,
|
|
|
|
max_tokens=chat_question.max_tokens,
|
2023-06-26 11:34:03 +03:00
|
|
|
temperature=chat_question.temperature,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id = brain_id,
|
|
|
|
user_openai_api_key=current_user.user_openai_api_key,
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
|
|
|
answer = brainPicking.generate_answer(chat_question.question)
|
|
|
|
|
|
|
|
chat_answer = update_chat_history(
|
|
|
|
chat_id=chat_id,
|
|
|
|
user_message=chat_question.question,
|
|
|
|
assistant_answer=answer,
|
|
|
|
)
|
|
|
|
return chat_answer
|
|
|
|
except HTTPException as e:
|
|
|
|
raise e
|
2023-06-20 10:54:23 +03:00
|
|
|
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
# get chat history
|
|
|
|
@chat_router.get(
|
|
|
|
"/chat/{chat_id}/history", dependencies=[Depends(AuthBearer())], tags=["Chat"]
|
|
|
|
)
|
|
|
|
async def get_chat_history_handler(
|
|
|
|
chat_id: UUID,
|
2023-06-28 20:39:27 +03:00
|
|
|
) -> List[ChatHistory]:
|
2023-06-22 18:50:06 +03:00
|
|
|
# TODO: RBAC with current_user
|
|
|
|
return get_chat_history(chat_id)
|