File size: 9,942 Bytes
0743bb0
9002555
 
 
 
0743bb0
d57efd6
9002555
 
d57efd6
0743bb0
d57efd6
9002555
0743bb0
d57efd6
 
9002555
 
d57efd6
 
 
 
 
 
9002555
 
0743bb0
d57efd6
9002555
d57efd6
9002555
 
 
 
 
d57efd6
9002555
d57efd6
0743bb0
 
 
 
 
 
d57efd6
0743bb0
d57efd6
0743bb0
d57efd6
 
 
9002555
d57efd6
 
 
9002555
 
 
d57efd6
 
9002555
d57efd6
9002555
 
0743bb0
d57efd6
9002555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d57efd6
 
 
9002555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d57efd6
 
 
9002555
 
 
 
 
 
 
 
 
 
 
 
d57efd6
 
 
9002555
 
 
0743bb0
d57efd6
0743bb0
d57efd6
 
 
 
 
 
 
 
 
 
 
 
9002555
 
 
 
 
d57efd6
0743bb0
 
 
 
d57efd6
9002555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0743bb0
9002555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0743bb0
9002555
 
 
 
 
 
 
 
0743bb0
 
 
 
 
 
 
9002555
 
d57efd6
 
9002555
d57efd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9002555
d57efd6
 
 
 
 
 
 
 
9002555
d57efd6
9002555
 
 
 
 
 
 
 
d57efd6
9002555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from script.vector_db import IndexManager
from script.document_uploader import Uploader
from db.get_data import GetDatabase
from db.delete_data import DeleteDatabase
from db.update_data import UpdateDatabase

from typing import Any
from fastapi import UploadFile
from fastapi import HTTPException
from fastapi.responses import JSONResponse

from llama_index.core.llms import MessageRole
from core.chat.engine import Engine
from core.chat.chatstore import ChatStore
from core.parser import clean_text, update_response, renumber_sources
from service.dto import BotResponseStreaming, ChatMessage
from service.aws_loader import Loader

from pymongo.mongo_client import MongoClient
from dotenv import load_dotenv

from typing import List
from datetime import datetime
import redis
import logging
import re
import json
import os

load_dotenv()

# Configure logging
logging.basicConfig(level=logging.INFO)


async def data_ingestion(category_id, reference, file: UploadFile) -> Any:
    try:
        # Upload to AWS
        file_name = f"{reference['title']}"
        aws_loader = Loader()

        file_obj = file
        aws_loader.upload_to_s3(file_obj, file_name)

        uploader = Uploader(reference, file)

        nodes_with_metadata = await uploader.process_documents()

        # Build indexes using IndexManager
        index = IndexManager()
        index.build_indexes(nodes_with_metadata)

        return json.dumps(
            {"status": "success", "message": "Vector Index loaded successfully."}
        )

    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error("An error occurred in data ingestion: %s", e)
        return JSONResponse(
            status_code=500,
            content="An internal server error occurred in data ingestion.",
        )


async def get_data(db_conn, title=None, fetch_all_data=True):
    get_database = GetDatabase(db_conn)
    print(get_database)
    try:
        if fetch_all_data:
            results = await get_database.get_all_data()
            print(results)
            logging.info("Database fetched all data")
            return results
        else:
            results = await get_database.get_data(title)
            logging.info("Database fetched one data")
            return results

    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error("An error occurred in get data: %s", e)
        return JSONResponse(
            status_code=500, content="An internal server error occurred in get data."
        )


async def update_data(id: int, reference, db_conn):
    update_database = UpdateDatabase(db_conn)
    try:
        reference = reference.model_dump()
        print(reference)
        reference.update({"id": id})
        print(reference)
        await update_database.update_record(reference)
        response = {"status": "Update Success"}
        return response
    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error("An error occurred in update data: %s", e)
        return JSONResponse(
            status_code=500, content="An internal server error occurred in update data."
        )


async def delete_data(id: int, db_conn):
    delete_database = DeleteDatabase(db_conn)
    try:
        params = {"id": id}
        await delete_database.delete_record(params)
        response = {"status": "Delete Success"}
        return response
    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error("An error occurred in get data: %s", e)
        return JSONResponse(
            status_code=500, content="An internal server error occurred in delete data."
        )


def generate_completion_non_streaming(
    session_id, user_request, titles: List = None, type_bot="general"
):
    uri = os.getenv("MONGO_URI")
    engine = Engine()
    index_manager = IndexManager()
    chatstore = ChatStore()
    client = MongoClient(uri)

    try:
        client.admin.command("ping")
        print("Pinged your deployment. You successfully connected to MongoDB!")
    except Exception as e:
        return JSONResponse(status_code=500, content=f"Database Error as {e}")

    try:

        # Load existing indexes
        index = index_manager.load_existing_indexes()

        if type_bot == "general":
            # Retrieve the chat engine with the loaded index
            chat_engine = engine.get_chat_engine(session_id, index)
        else:
            # Retrieve the chat engine with the loaded index
            chat_engine = engine.get_chat_engine(session_id, index, titles, type_bot)

        # Generate completion response
        response = chat_engine.chat(user_request)

        sources = response.sources

        number_reference = list(set(re.findall(r"\[(\d+)\]", str(response))))
        number_reference_sorted = sorted(number_reference)

        contents = []
        metadata_collection = []
        scores = []

        if number_reference_sorted:
            for number in number_reference_sorted:
                # Konversi number ke integer untuk digunakan sebagai indeks
                number = int(number)

                # Pastikan sources tidak kosong dan memiliki elemen yang diperlukan
                if sources and len(sources) > 0:
                    node = dict(sources[0])["raw_output"].source_nodes

                    # Pastikan number valid sebagai indeks
                    if 0 <= number - 1 < len(node):

                        content = clean_text(node[number - 1].node.get_text())
                        contents.append(content)

                        metadata = dict(node[number - 1].node.metadata)
                        metadata_collection.append(metadata)

                        score = node[number - 1].score
                        scores.append(score)
                    else:
                        print(f"Invalid reference number: {number}")
                else:
                    print("No sources available")
        else:
            print("There are no references")

        response = update_response(str(response))
        contents = renumber_sources(contents)

        # Check the lengths of content and metadata
        num_content = len(contents)
        num_metadata = len(metadata_collection)

        # Add content to metadata
        for i in range(min(num_content, num_metadata)):
            metadata_collection[i]["content"] = re.sub(r"source \d+\:", "", contents[i])

        message = ChatMessage(
            role=MessageRole.ASSISTANT, content=response, metadata=metadata_collection
        )

        chatstore.delete_last_message(session_id)
        chatstore.add_message(session_id, message)
        chatstore.clean_message(session_id)
    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error("An error occurred in generate text: %s", e)
        return JSONResponse(
            status_code=500,
            content=f"An internal server error occurred in generate text as {e}.")

    try :
        chat_history_db = [
                ChatMessage(role=MessageRole.SYSTEM, 
                            content=user_request, 
                            timestamp=datetime.now(),
                            payment = "free" if type_bot=="general" else None
                            ),
                ChatMessage(
                    role=MessageRole.ASSISTANT,
                    content=response,
                    metadata=metadata_collection,
                    timestamp=datetime.now(),
                    payment = "free" if type_bot=="general" else None 
                )
            ]
        
        chat_history_json = [message.model_dump() for message in chat_history_db]

        db = client["bot_database"]  # Replace with your database name
        collection = db[session_id]  # Replace with your collection name
        
        result = collection.insert_many(chat_history_json)
        print("Data inserted with record ids", result.inserted_ids)

        return str(response), metadata_collection, scores
    
    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error("An error occurred in generate text: %s", e)
        return JSONResponse(
            status_code=500,
            content=f"An internal server error occurred in generate text as {e}.")

async def generate_streaming_completion(user_request, session_id):
    try:
        engine = Engine()
        index_manager = IndexManager()

        # Load existing indexes
        index = index_manager.load_existing_indexes()

        # Retrieve the chat engine with the loaded index
        chat_engine = engine.get_chat_engine(index, session_id)
        # Generate completion response
        response = chat_engine.stream_chat(user_request)

        completed_response = ""

        for gen in response.response_gen:
            completed_response += gen  # Concatenate the new string
            yield BotResponseStreaming(
                content=gen, completed_content=completed_response
            )

        nodes = response.source_nodes
        for node in nodes:
            reference = str(clean_text(node.node.get_text()))
            metadata = dict(node.node.metadata)
            score = float(node.score)
            yield BotResponseStreaming(
                completed_content=completed_response,
                reference=reference,
                metadata=metadata,
                score=score,
            )
    except Exception as e:
        yield {"error": str(e)}

    except Exception as e:
        # Log the error and raise HTTPException for FastAPI
        logging.error(f"An error occurred in generate text: {e}")
        raise HTTPException(
            status_code=500,
            detail="An internal server error occurred in generate text.",
        ) from e