2024-02-15 01:01:35 +03:00
|
|
|
from typing import Annotated, List, Optional
|
2023-06-11 00:59:16 +03:00
|
|
|
from uuid import UUID
|
2023-06-23 18:59:53 +03:00
|
|
|
|
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
|
2024-01-28 12:03:36 +03:00
|
|
|
from langchain.embeddings.ollama import OllamaEmbeddings
|
|
|
|
from langchain.embeddings.openai import OpenAIEmbeddings
|
2024-01-28 05:58:57 +03:00
|
|
|
from logger import get_logger
|
2023-11-14 16:31:02 +03:00
|
|
|
from middlewares.auth import AuthBearer, get_current_user
|
2024-01-28 12:03:36 +03:00
|
|
|
from models.settings import BrainSettings, get_supabase_client
|
2023-12-04 20:38:54 +03:00
|
|
|
from models.user_usage import UserUsage
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.brain.service.brain_service import BrainService
|
2024-01-20 07:34:30 +03:00
|
|
|
from modules.chat.controller.chat.brainful_chat import BrainfulChat
|
2023-12-04 20:38:54 +03:00
|
|
|
from modules.chat.dto.chats import ChatItem, ChatQuestion
|
|
|
|
from modules.chat.dto.inputs import (
|
2024-03-21 10:11:06 +03:00
|
|
|
ChatMessageProperties,
|
2023-08-21 13:58:22 +03:00
|
|
|
ChatUpdatableProperties,
|
|
|
|
CreateChatProperties,
|
2023-12-04 20:38:54 +03:00
|
|
|
QuestionAndAnswer,
|
2023-10-30 12:18:23 +03:00
|
|
|
)
|
2023-12-04 20:38:54 +03:00
|
|
|
from modules.chat.entity.chat import Chat
|
|
|
|
from modules.chat.service.chat_service import ChatService
|
|
|
|
from modules.notification.service.notification_service import NotificationService
|
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2024-02-08 07:44:37 +03:00
|
|
|
from packages.utils.telemetry import send_telemetry
|
2024-01-28 12:03:36 +03:00
|
|
|
from vectorstore.supabase import CustomSupabaseVectorStore
|
2023-09-26 11:35:52 +03:00
|
|
|
|
2024-01-27 12:50:58 +03:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
chat_router = APIRouter()
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-11-28 16:27:39 +03:00
|
|
|
notification_service = NotificationService()
|
2023-12-01 00:29:28 +03:00
|
|
|
brain_service = BrainService()
|
2023-12-04 20:38:54 +03:00
|
|
|
chat_service = ChatService()
|
2023-11-28 16:27:39 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2024-01-28 12:03:36 +03:00
|
|
|
def init_vector_store(user_id: UUID) -> CustomSupabaseVectorStore:
|
|
|
|
"""
|
|
|
|
Initialize the vector store
|
|
|
|
"""
|
|
|
|
brain_settings = BrainSettings()
|
|
|
|
supabase_client = get_supabase_client()
|
|
|
|
embeddings = None
|
|
|
|
if brain_settings.ollama_api_base_url:
|
|
|
|
embeddings = OllamaEmbeddings(
|
|
|
|
base_url=brain_settings.ollama_api_base_url
|
|
|
|
) # pyright: ignore reportPrivateUsage=none
|
|
|
|
else:
|
|
|
|
embeddings = OpenAIEmbeddings()
|
|
|
|
vector_store = CustomSupabaseVectorStore(
|
|
|
|
supabase_client, embeddings, table_name="vectors", user_id=user_id
|
|
|
|
)
|
|
|
|
|
|
|
|
return vector_store
|
|
|
|
|
|
|
|
|
2024-01-28 05:58:57 +03:00
|
|
|
def get_answer_generator(
|
|
|
|
chat_id: UUID,
|
|
|
|
chat_question: ChatQuestion,
|
|
|
|
brain_id: UUID,
|
|
|
|
current_user: UserIdentity,
|
|
|
|
):
|
|
|
|
chat_instance = BrainfulChat()
|
|
|
|
chat_instance.validate_authorization(user_id=current_user.id, brain_id=brain_id)
|
|
|
|
|
|
|
|
user_usage = UserUsage(
|
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
)
|
|
|
|
|
2024-01-28 12:03:36 +03:00
|
|
|
vector_store = init_vector_store(user_id=current_user.id)
|
|
|
|
|
2024-01-28 05:58:57 +03:00
|
|
|
# Get History
|
|
|
|
history = chat_service.get_chat_history(chat_id)
|
|
|
|
|
|
|
|
# Generic
|
|
|
|
brain, metadata_brain = brain_service.find_brain_from_question(
|
2024-01-28 12:03:36 +03:00
|
|
|
brain_id, chat_question.question, current_user, chat_id, history, vector_store
|
2024-01-28 05:58:57 +03:00
|
|
|
)
|
|
|
|
|
2024-02-20 04:29:45 +03:00
|
|
|
send_telemetry("question_asked", {"model_name": brain.model})
|
2024-02-08 07:44:37 +03:00
|
|
|
|
2024-01-28 05:58:57 +03:00
|
|
|
gpt_answer_generator = chat_instance.get_answer_generator(
|
2024-02-20 04:29:45 +03:00
|
|
|
brain=brain,
|
2024-01-28 05:58:57 +03:00
|
|
|
chat_id=str(chat_id),
|
2024-02-20 04:29:45 +03:00
|
|
|
model=brain.model,
|
2024-01-28 05:58:57 +03:00
|
|
|
temperature=0.1,
|
|
|
|
streaming=True,
|
|
|
|
prompt_id=chat_question.prompt_id,
|
|
|
|
user_id=current_user.id,
|
2024-02-20 04:29:45 +03:00
|
|
|
user_email=current_user.email,
|
2024-01-28 05:58:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return gpt_answer_generator
|
|
|
|
|
|
|
|
|
2023-08-21 00:20:57 +03:00
|
|
|
@chat_router.get("/chat/healthz", tags=["Health"])
|
|
|
|
async def healthz():
|
|
|
|
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
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-08-21 15:05:13 +03:00
|
|
|
async def get_chats(current_user: UserIdentity = 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-12-04 20:38:54 +03:00
|
|
|
chats = chat_service.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-11-28 16:27:39 +03:00
|
|
|
notification_service.remove_chat_notifications(chat_id)
|
2023-09-07 18:23:31 +03:00
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
chat_service.delete_chat_from_db(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,
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-12-04 20:38:54 +03:00
|
|
|
):
|
2023-06-22 18:50:06 +03:00
|
|
|
"""
|
|
|
|
Update chat attributes
|
|
|
|
"""
|
|
|
|
|
2023-12-04 20:38:54 +03:00
|
|
|
chat = chat_service.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-12-04 20:38:54 +03:00
|
|
|
return chat_service.update_chat(chat_id=chat_id, chat_data=chat_data)
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
|
2024-03-21 10:11:06 +03:00
|
|
|
# update existing message
|
|
|
|
@chat_router.put(
|
|
|
|
"/chat/{chat_id}/{message_id}", dependencies=[Depends(AuthBearer())], tags=["Chat"]
|
|
|
|
)
|
|
|
|
async def update_chat_message(
|
|
|
|
chat_message_properties: ChatMessageProperties,
|
|
|
|
chat_id: UUID,
|
|
|
|
message_id: UUID,
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
) :
|
|
|
|
|
|
|
|
chat = chat_service.get_chat_by_id(
|
|
|
|
chat_id # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
|
|
|
if str(current_user.id) != chat.user_id:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=403, # pyright: ignore reportPrivateUsage=none
|
|
|
|
detail="You should be the owner of the chat to update it.", # pyright: ignore reportPrivateUsage=none
|
|
|
|
)
|
|
|
|
return chat_service.update_chat_message(
|
|
|
|
chat_id=chat_id,
|
|
|
|
message_id=message_id,
|
|
|
|
chat_message_properties=chat_message_properties.dict(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-06-20 10:54:23 +03:00
|
|
|
):
|
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-12-04 20:38:54 +03:00
|
|
|
return chat_service.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,
|
2024-02-15 01:01:35 +03:00
|
|
|
brain_id: Annotated[UUID | None, Query()] = None,
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-12-15 13:43:41 +03:00
|
|
|
):
|
2023-06-22 18:50:06 +03:00
|
|
|
try:
|
2024-02-15 01:01:35 +03:00
|
|
|
logger.info(
|
|
|
|
f"Creating question for chat {chat_id} with brain {brain_id} of type {type(brain_id)}"
|
|
|
|
)
|
2024-01-28 05:58:57 +03:00
|
|
|
gpt_answer_generator = get_answer_generator(
|
|
|
|
chat_id, chat_question, brain_id, current_user
|
2023-10-30 12:18:23 +03:00
|
|
|
)
|
2023-06-30 11:10:59 +03:00
|
|
|
|
2023-12-15 13:43:41 +03:00
|
|
|
chat_answer = gpt_answer_generator.generate_answer(
|
|
|
|
chat_id, chat_question, save_answer=True
|
|
|
|
)
|
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,
|
2024-02-15 01:01:35 +03:00
|
|
|
brain_id: Annotated[UUID | None, Query()] = None,
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-06-30 11:10:59 +03:00
|
|
|
) -> StreamingResponse:
|
2024-02-15 01:01:35 +03:00
|
|
|
|
2024-01-28 12:03:36 +03:00
|
|
|
chat_instance = BrainfulChat()
|
|
|
|
chat_instance.validate_authorization(user_id=current_user.id, brain_id=brain_id)
|
|
|
|
|
|
|
|
user_usage = UserUsage(
|
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
)
|
2024-02-15 01:01:35 +03:00
|
|
|
|
|
|
|
logger.info(
|
|
|
|
f"Creating question for chat {chat_id} with brain {brain_id} of type {type(brain_id)}"
|
|
|
|
)
|
|
|
|
|
2024-01-28 05:58:57 +03:00
|
|
|
gpt_answer_generator = get_answer_generator(
|
|
|
|
chat_id, chat_question, brain_id, current_user
|
2024-01-27 12:50:58 +03:00
|
|
|
)
|
|
|
|
|
2023-06-30 11:10:59 +03:00
|
|
|
try:
|
|
|
|
return StreamingResponse(
|
2023-12-15 13:43:41 +03:00
|
|
|
gpt_answer_generator.generate_stream(
|
|
|
|
chat_id, chat_question, save_answer=True
|
|
|
|
),
|
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-09-07 18:23:31 +03:00
|
|
|
) -> List[ChatItem]:
|
2023-06-22 18:50:06 +03:00
|
|
|
# TODO: RBAC with current_user
|
2023-12-04 20:38:54 +03:00
|
|
|
return chat_service.get_chat_history_with_notifications(chat_id)
|
2023-10-12 10:39:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
@chat_router.post(
|
|
|
|
"/chat/{chat_id}/question/answer",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["Chat"],
|
|
|
|
)
|
|
|
|
async def add_question_and_answer_handler(
|
|
|
|
chat_id: UUID,
|
|
|
|
question_and_answer: QuestionAndAnswer,
|
|
|
|
) -> Optional[Chat]:
|
|
|
|
"""
|
|
|
|
Add a new question and anwser to the chat.
|
|
|
|
"""
|
2023-12-04 20:38:54 +03:00
|
|
|
return chat_service.add_question_and_answer(chat_id, question_and_answer)
|