File size: 4,433 Bytes
d57efd6
 
 
b39c0ba
 
 
 
d57efd6
b39c0ba
d57efd6
b39c0ba
 
 
 
d57efd6
 
 
b39c0ba
d57efd6
 
b39c0ba
 
 
 
 
 
d57efd6
 
 
 
 
 
 
 
 
 
 
b39c0ba
 
 
 
d57efd6
 
 
b39c0ba
 
 
d57efd6
 
b39c0ba
 
d57efd6
 
 
 
 
 
 
 
 
 
b39c0ba
d57efd6
 
 
 
 
 
b39c0ba
 
 
d57efd6
 
 
 
 
 
 
 
b39c0ba
 
 
d57efd6
 
 
b39c0ba
d57efd6
 
 
 
 
 
 
 
 
b39c0ba
 
 
 
 
d57efd6
 
 
 
b39c0ba
 
d57efd6
 
 
 
 
 
 
 
 
b39c0ba
d57efd6
 
 
b39c0ba
 
 
d57efd6
 
 
b39c0ba
 
d57efd6
b39c0ba
d57efd6
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from typing import Annotated
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
import pytz

from service.dto import UserPromptRequest, BotResponse, BotCreateRequest
from datetime import datetime
from core.chat.chatstore import ChatStore
from core.chat.bot_service import ChatCompletionService
from db.database import get_db
from db.models import Session_Publisher
from db.query.query_book import BookQuery
from db.query.query_bot import BotQuery

from sqlalchemy.orm import Session
from sse_starlette.sse import EventSourceResponse
from utils.utils import generate_uuid
from utils.error_handlers import handle_exception
from langfuse.llama_index import LlamaIndexCallbackHandler

from api.auth import check_user_authentication
from api.router.user import user_dependency
from api.function import (
    generate_streaming_completion
)


router = APIRouter(tags=["Bot_One"])

db_dependency = Annotated[Session, Depends(get_db)]


def get_chat_store():
    return ChatStore()


@router.post("/bot_one/{metadata_id}")
async def create_bot_one(user: user_dependency, db: db_dependency, metadata_id: int, bot_name:BotCreateRequest):
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response
    # Generate a new session ID (UUID)
    try:
        session_id = generate_uuid()
        
        bot_query = BotQuery(user)
        bot_query.add_bot(db, session_id, bot_name.name, metadata_id)

        return {
            "status": "session id created successfully",
            "bot_name": bot_name.name,
            "session_id": session_id,
        }

    except Exception as e:
        return JSONResponse(
            status_code=500, content=f"An unexpected  in retrieving session id {str(e)}"
        )


@router.post("/bot_one/{metadata_id}/{session_id}")
async def generator_bot(
    user: user_dependency,
    db: db_dependency,
    metadata_id: int,
    session_id: str,
    user_prompt_request: UserPromptRequest,
):
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    langfuse_callback_handler = LlamaIndexCallbackHandler()
    langfuse_callback_handler.set_trace_params(
        user_id=user.get("username"), session_id=session_id
    )

    # Query to retrieve the titles
    try:
        book_query = BookQuery(user)
        output_titles = book_query.get_title_from_session(db, metadata_id, session_id)
        titles = [item[0] for item in output_titles]
        print(titles)

    except Exception as e:
        return handle_exception(e)

    if user_prompt_request.streaming:
        return EventSourceResponse(
            generate_streaming_completion(
                user_prompt_request.prompt,
                session_id,
            )
        )
    else:
        bot_service = ChatCompletionService(session_id, user_prompt_request.prompt, titles, type_bot="specific")
        response, metadata, scores = bot_service.generate_completion()

        # Set Jakarta timezone
        jakarta_tz = pytz.timezone('Asia/Jakarta')

        existing_session = (
            db.query(Session_Publisher).filter(Session_Publisher.id == session_id).first()
        )
        
        existing_session.updated_at = datetime.now(jakarta_tz)
        db.commit()
        
        return BotResponse(
            content=response,
            metadata=metadata,
            scores=scores,
        )


@router.get("/bot_one/{metadata_id}")
async def get_all_session_bot_one(
    user: user_dependency, db: db_dependency, metadata_id: int
):
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    try:
        # Query the session IDs based on the user ID
        bot_query = BotQuery(user)
        sessions = bot_query.get_session_ids_bot(db, metadata_id)
        
        session_data = [{"id": session.id, "bot_name":session.bot_name, "updated_at": str(session.updated_at)} for session in sessions]

        # Convert list of tuples to a simple list
        session_sorted_data = sorted(session_data, key=lambda x: datetime.fromisoformat(x['updated_at']), reverse=True)

        return session_sorted_data

    except Exception as e:
        # Log the error and return JSONResponse for FastAPI
        print(f"An error occurred while fetching session IDs: {e}")
        return JSONResponse(status_code=400, content="Error retrieving session IDs")