2023-06-11 00:59:16 +03:00
|
|
|
import os
|
|
|
|
import time
|
2023-06-28 20:39:27 +03:00
|
|
|
from typing import List
|
2023-06-11 00:59:16 +03:00
|
|
|
from uuid import UUID
|
2023-07-31 22:34:34 +03:00
|
|
|
from venv import logger
|
2023-06-23 18:59:53 +03:00
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
from auth import AuthBearer, get_current_user
|
2023-08-03 13:11:25 +03:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
2023-06-30 11:10:59 +03:00
|
|
|
from fastapi.responses import StreamingResponse
|
2023-07-04 18:56:54 +03:00
|
|
|
from llm.openai import OpenAIBrainPicking
|
2023-08-03 11:37:13 +03:00
|
|
|
from models.brains import Brain
|
2023-08-11 21:44:48 +03:00
|
|
|
from models.brain_entity import BrainEntity
|
2023-08-18 11:18:29 +03:00
|
|
|
from models.chat import Chat
|
2023-06-22 18:50:06 +03:00
|
|
|
from models.chats import ChatQuestion
|
2023-08-03 21:24:42 +03:00
|
|
|
from models.databases.supabase.supabase import SupabaseDB
|
|
|
|
from models.settings import LLMSettings, get_supabase_db
|
2023-06-11 00:59:16 +03:00
|
|
|
from models.users import User
|
2023-08-03 11:37:13 +03:00
|
|
|
from repository.brain.get_brain_details import get_brain_details
|
|
|
|
from repository.brain.get_default_user_brain_or_create_new import (
|
|
|
|
get_default_user_brain_or_create_new,
|
|
|
|
)
|
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-08-10 11:25:08 +03:00
|
|
|
from repository.chat.get_chat_history import GetChatHistoryOutput, 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-08-01 17:25:02 +03:00
|
|
|
from repository.user_identity.get_user_identity import get_user_identity
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
chat_router = APIRouter()
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-07-23 12:14:42 +03:00
|
|
|
class NullableUUID(UUID):
|
2023-07-14 22:02:26 +03:00
|
|
|
@classmethod
|
|
|
|
def __get_validators__(cls):
|
|
|
|
yield cls.validate
|
|
|
|
|
|
|
|
@classmethod
|
2023-07-23 12:14:42 +03:00
|
|
|
def validate(cls, v) -> UUID | None:
|
2023-07-14 22:02:26 +03:00
|
|
|
if v == "":
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return UUID(v)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-08-03 21:24:42 +03:00
|
|
|
def delete_chat_from_db(supabase_db: SupabaseDB, chat_id):
|
2023-07-01 18:22:28 +03:00
|
|
|
try:
|
2023-08-03 21:24:42 +03:00
|
|
|
supabase_db.delete_chat_history(chat_id)
|
2023-07-01 18:22:28 +03:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
pass
|
|
|
|
try:
|
2023-08-03 21:24:42 +03:00
|
|
|
supabase_db.delete_chat(chat_id)
|
2023-07-01 18:22:28 +03:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
pass
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
def check_user_limit(
|
|
|
|
user: User,
|
|
|
|
):
|
|
|
|
if user.user_openai_api_key is None:
|
|
|
|
date = time.strftime("%Y%m%d")
|
2023-08-18 11:18:29 +03:00
|
|
|
max_requests_number = int(os.getenv("MAX_REQUESTS_NUMBER", 1))
|
2023-06-30 11:10:59 +03:00
|
|
|
|
|
|
|
user.increment_user_request_count(date)
|
2023-07-02 11:54:36 +03:00
|
|
|
if int(user.requests_count) >= int(max_requests_number):
|
2023-06-30 11:10:59 +03:00
|
|
|
raise HTTPException(
|
2023-07-10 15:27:49 +03:00
|
|
|
status_code=429, # pyright: ignore reportPrivateUsage=none
|
|
|
|
detail="You have reached the maximum number of requests for today.", # pyright: ignore reportPrivateUsage=none
|
2023-06-30 11:10:59 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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-08-10 11:25:08 +03:00
|
|
|
chats = get_user_chats(str(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-08-03 21:24:42 +03:00
|
|
|
supabase_db = get_supabase_db()
|
|
|
|
delete_chat_from_db(supabase_db=supabase_db, chat_id=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
|
|
|
|
"""
|
|
|
|
|
2023-07-10 15:27:49 +03:00
|
|
|
chat = get_chat_by_id(chat_id) # pyright: ignore reportPrivateUsage=none
|
2023-08-03 13:11:25 +03:00
|
|
|
if str(current_user.id) != chat.user_id:
|
2023-06-23 11:36:55 +03:00
|
|
|
raise HTTPException(
|
2023-07-10 15:27:49 +03:00
|
|
|
status_code=403, # pyright: ignore reportPrivateUsage=none
|
|
|
|
detail="You should be the owner of the chat to update it.", # pyright: ignore reportPrivateUsage=none
|
2023-06-23 11:36:55 +03:00
|
|
|
)
|
2023-06-22 18:50:06 +03:00
|
|
|
return update_chat(chat_id=chat_id, chat_data=chat_data)
|
|
|
|
|
|
|
|
|
|
|
|
# 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-29 09:00:34 +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(
|
2023-07-14 22:02:26 +03:00
|
|
|
"/chat/{chat_id}/question",
|
|
|
|
dependencies=[
|
|
|
|
Depends(
|
|
|
|
AuthBearer(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
tags=["Chat"],
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
|
|
|
async def create_question_handler(
|
|
|
|
request: Request,
|
|
|
|
chat_question: ChatQuestion,
|
2023-06-20 10:54:23 +03:00
|
|
|
chat_id: UUID,
|
2023-07-14 22:02:26 +03:00
|
|
|
brain_id: NullableUUID
|
|
|
|
| UUID
|
|
|
|
| None = Query(..., description="The ID of the brain"),
|
2023-06-20 10:54:23 +03:00
|
|
|
current_user: User = Depends(get_current_user),
|
2023-08-10 11:25:08 +03:00
|
|
|
) -> GetChatHistoryOutput:
|
2023-08-01 17:25:02 +03:00
|
|
|
"""
|
|
|
|
Add a new question to the chat.
|
|
|
|
"""
|
|
|
|
# Retrieve user's OpenAI API key
|
2023-06-28 20:39:27 +03:00
|
|
|
current_user.user_openai_api_key = request.headers.get("Openai-Api-Key")
|
2023-08-01 17:25:02 +03:00
|
|
|
brain = Brain(id=brain_id)
|
|
|
|
|
2023-08-10 11:25:08 +03:00
|
|
|
if not current_user.user_openai_api_key and brain_id:
|
|
|
|
brain_details = get_brain_details(brain_id)
|
|
|
|
if brain_details:
|
|
|
|
current_user.user_openai_api_key = brain_details.openai_api_key
|
2023-08-01 17:25:02 +03:00
|
|
|
|
|
|
|
if not current_user.user_openai_api_key:
|
|
|
|
user_identity = get_user_identity(current_user.id)
|
|
|
|
|
|
|
|
if user_identity is not None:
|
|
|
|
current_user.user_openai_api_key = user_identity.openai_api_key
|
|
|
|
|
|
|
|
# Retrieve chat model (temperature, max_tokens, model)
|
|
|
|
if (
|
|
|
|
not chat_question.model
|
|
|
|
or not chat_question.temperature
|
|
|
|
or not chat_question.max_tokens
|
|
|
|
):
|
|
|
|
# TODO: create ChatConfig class (pick config from brain or user or chat) and use it here
|
2023-08-07 17:35:23 +03:00
|
|
|
chat_question.model = chat_question.model or brain.model or "gpt-3.5-turbo"
|
2023-08-01 17:25:02 +03:00
|
|
|
chat_question.temperature = chat_question.temperature or brain.temperature or 0
|
|
|
|
chat_question.max_tokens = chat_question.max_tokens or brain.max_tokens or 256
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
try:
|
2023-06-28 20:39:27 +03:00
|
|
|
check_user_limit(current_user)
|
2023-08-01 17:25:02 +03:00
|
|
|
LLMSettings()
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-07-14 22:02:26 +03:00
|
|
|
if not brain_id:
|
2023-08-03 11:37:13 +03:00
|
|
|
brain_id = get_default_user_brain_or_create_new(current_user).brain_id
|
2023-07-04 18:56:54 +03:00
|
|
|
|
2023-07-31 17:01:34 +03:00
|
|
|
gpt_answer_generator = OpenAIBrainPicking(
|
|
|
|
chat_id=str(chat_id),
|
|
|
|
model=chat_question.model,
|
|
|
|
max_tokens=chat_question.max_tokens,
|
|
|
|
temperature=chat_question.temperature,
|
|
|
|
brain_id=str(brain_id),
|
|
|
|
user_openai_api_key=current_user.user_openai_api_key, # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-08-10 11:25:08 +03:00
|
|
|
chat_answer = gpt_answer_generator.generate_answer(chat_id, chat_question)
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
return chat_answer
|
|
|
|
except HTTPException as e:
|
|
|
|
raise e
|
2023-06-20 10:54:23 +03:00
|
|
|
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
# stream new question response from chat
|
|
|
|
@chat_router.post(
|
|
|
|
"/chat/{chat_id}/question/stream",
|
2023-07-14 22:02:26 +03:00
|
|
|
dependencies=[
|
|
|
|
Depends(
|
|
|
|
AuthBearer(),
|
|
|
|
),
|
|
|
|
],
|
2023-06-30 11:10:59 +03:00
|
|
|
tags=["Chat"],
|
|
|
|
)
|
|
|
|
async def create_stream_question_handler(
|
|
|
|
request: Request,
|
|
|
|
chat_question: ChatQuestion,
|
|
|
|
chat_id: UUID,
|
2023-07-14 22:02:26 +03:00
|
|
|
brain_id: NullableUUID
|
|
|
|
| UUID
|
|
|
|
| None = Query(..., description="The ID of the brain"),
|
2023-06-30 11:10:59 +03:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
) -> StreamingResponse:
|
2023-07-14 22:02:26 +03:00
|
|
|
# TODO: check if the user has access to the brain
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-08-01 17:25:02 +03:00
|
|
|
# Retrieve user's OpenAI API key
|
|
|
|
current_user.user_openai_api_key = request.headers.get("Openai-Api-Key")
|
|
|
|
brain = Brain(id=brain_id)
|
2023-08-11 21:44:48 +03:00
|
|
|
brain_details: BrainEntity = None
|
2023-08-03 11:37:13 +03:00
|
|
|
if not current_user.user_openai_api_key and brain_id:
|
|
|
|
brain_details = get_brain_details(brain_id)
|
2023-08-01 17:25:02 +03:00
|
|
|
if brain_details:
|
2023-08-03 11:37:13 +03:00
|
|
|
current_user.user_openai_api_key = brain_details.openai_api_key
|
2023-08-01 17:25:02 +03:00
|
|
|
|
|
|
|
if not current_user.user_openai_api_key:
|
|
|
|
user_identity = get_user_identity(current_user.id)
|
|
|
|
|
|
|
|
if user_identity is not None:
|
|
|
|
current_user.user_openai_api_key = user_identity.openai_api_key
|
|
|
|
|
|
|
|
# Retrieve chat model (temperature, max_tokens, model)
|
|
|
|
if (
|
|
|
|
not chat_question.model
|
2023-08-11 21:44:48 +03:00
|
|
|
or chat_question.temperature is None
|
2023-08-01 17:25:02 +03:00
|
|
|
or not chat_question.max_tokens
|
|
|
|
):
|
|
|
|
# TODO: create ChatConfig class (pick config from brain or user or chat) and use it here
|
2023-08-07 17:35:23 +03:00
|
|
|
chat_question.model = chat_question.model or brain.model or "gpt-3.5-turbo"
|
2023-08-01 17:25:02 +03:00
|
|
|
chat_question.temperature = chat_question.temperature or brain.temperature or 0
|
|
|
|
chat_question.max_tokens = chat_question.max_tokens or brain.max_tokens or 256
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
try:
|
2023-07-31 22:34:34 +03:00
|
|
|
logger.info(f"Streaming request for {chat_question.model}")
|
2023-06-30 11:10:59 +03:00
|
|
|
check_user_limit(current_user)
|
2023-07-31 22:34:34 +03:00
|
|
|
if not brain_id:
|
2023-08-03 11:37:13 +03:00
|
|
|
brain_id = get_default_user_brain_or_create_new(current_user).brain_id
|
2023-07-31 22:34:34 +03:00
|
|
|
|
2023-07-31 17:01:34 +03:00
|
|
|
gpt_answer_generator = OpenAIBrainPicking(
|
|
|
|
chat_id=str(chat_id),
|
2023-08-11 21:44:48 +03:00
|
|
|
model=(brain_details or chat_question).model if current_user.user_openai_api_key else "gpt-3.5-turbo",
|
|
|
|
max_tokens=(brain_details or chat_question).max_tokens if current_user.user_openai_api_key else 0,
|
|
|
|
temperature=(brain_details or chat_question).temperature if current_user.user_openai_api_key else 256,
|
2023-07-31 17:01:34 +03:00
|
|
|
brain_id=str(brain_id),
|
2023-07-31 22:34:34 +03:00
|
|
|
user_openai_api_key=current_user.user_openai_api_key, # pyright: ignore reportPrivateUsage=none
|
|
|
|
streaming=True,
|
2023-07-31 17:01:34 +03:00
|
|
|
)
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-07-31 22:34:34 +03:00
|
|
|
print("streaming")
|
2023-06-30 11:10:59 +03:00
|
|
|
return StreamingResponse(
|
2023-08-10 11:25:08 +03:00
|
|
|
gpt_answer_generator.generate_stream(chat_id, chat_question),
|
2023-06-30 11:10:59 +03:00
|
|
|
media_type="text/event-stream",
|
|
|
|
)
|
|
|
|
|
|
|
|
except HTTPException as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
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-08-10 11:25:08 +03:00
|
|
|
) -> List[GetChatHistoryOutput]:
|
2023-06-22 18:50:06 +03:00
|
|
|
# TODO: RBAC with current_user
|
2023-08-10 11:25:08 +03:00
|
|
|
return get_chat_history(str(chat_id))
|