Spaces:
Runtime error
Runtime error
| from fastapi import APIRouter, HTTPException, Depends | |
| from service.dto import UserPromptRequest, BotResponse | |
| from core.chat.chatstore import ChatStore | |
| from api.function import ( | |
| generate_streaming_completion, | |
| generate_completion_non_streaming, | |
| ) | |
| from sse_starlette.sse import EventSourceResponse | |
| from utils.utils import generate_uuid | |
| router = APIRouter(tags=["Bot"]) | |
| def get_chat_store(): | |
| return ChatStore() | |
| async def create_new_session(): | |
| session_id = generate_uuid() | |
| return {"session_id" : session_id} | |
| async def get_session_id(session_id: str, chat_store: ChatStore = Depends(get_chat_store)): | |
| chat_history = chat_store.get_messages(session_id) | |
| if not chat_history: | |
| raise HTTPException(status_code=404, detail="Session not found or empty.") | |
| return chat_history | |
| async def get_all_session_ids(): | |
| try: | |
| chat_store = ChatStore() | |
| all_keys = chat_store.get_keys() | |
| print(all_keys) | |
| return all_keys | |
| except Exception as e: | |
| # Log the error and raise HTTPException for FastAPI | |
| print(f"An error occurred in update data.: {e}") | |
| raise HTTPException( | |
| status_code=400, detail="the error when get all session ids" | |
| ) | |
| async def bot_generator_general(user_prompt_request: UserPromptRequest): | |
| if user_prompt_request.streaming: | |
| return EventSourceResponse( | |
| generate_streaming_completion( | |
| user_prompt_request.prompt, user_prompt_request.streaming | |
| ) | |
| ) | |
| else: | |
| response, raw_references, references, metadata, scores = ( | |
| generate_completion_non_streaming( | |
| user_prompt_request.session_id, user_prompt_request.prompt, user_prompt_request.streaming | |
| ) | |
| ) | |
| return BotResponse( | |
| content=response, | |
| raw_references=raw_references, | |
| references=references, | |
| metadata=metadata, | |
| scores=scores, | |
| ) | |
| #Ganti router | |
| async def bot_generator_spesific( | |
| category_id: int, title: str, user_prompt_request: UserPromptRequest | |
| ): | |
| pass | |
| async def delete_bot(session_id: str, chat_store: ChatStore = Depends(get_chat_store)): | |
| try: | |
| chat_store.delete_messages(session_id) | |
| return {"info": f"Delete {session_id} successful"} | |
| except Exception as e: | |
| # Log the error and raise HTTPException for FastAPI | |
| print(f"An error occurred in update data.: {e}") | |
| raise HTTPException( | |
| status_code=400, detail="the error when deleting message" | |
| ) | |
| async def get_favourite_data(category_id: int, title: str, human_template): | |
| pass |