File size: 7,731 Bytes
77320e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
from io import BytesIO
from typing import Any, Dict, List, Union

from fastapi import UploadFile
from starlette.datastructures import UploadFile as StarletteUploadFile
from werkzeug.datastructures import FileStorage

from ..conversation_sessions import CodeInterpreterSession
from ..exceptions.exceptions import (
    DependencyException,
    InputErrorException,
    InternalErrorException,
    ModelMaxIterationsException,
)
from ..schemas import Message, RoleType
from ..utils import get_logger
from ..tools import AsyncPythonSandBoxTool

logger = get_logger()


async def predict(
        prompt: str,
        model_name: str,
        config_path: str,
        uploaded_files: Any,
        **kwargs: Dict[str, Any]):
    start_time = time.time()

    # create new session
    session = await CodeInterpreterSession.create(
        model_name=model_name,
        config_path=config_path,
        **kwargs
    )

    files = upload_files(uploaded_files, session.session_id)
    logger.info(f"Session Creation Latency: {time.time() - start_time}")

    # upload file
    if isinstance(files, str):
        logger.info(f"Upload {files} as file path")
        await session.upload_to_sandbox(files)
    # upload list of file
    elif isinstance(files, list):
        for file in files:
            if isinstance(file, str):
                await session.upload_to_sandbox(file)
            elif isinstance(file, UploadFile) or isinstance(file, StarletteUploadFile):
                file_content = file.file.read()  # get file content
                file_like_object = BytesIO(file_content)
                file_storage = FileStorage(
                    stream=file_like_object,
                    filename=file.filename,
                    content_type=file.content_type
                )
                await session.upload_to_sandbox(file_storage)
            else:
                raise InputErrorException("The file type {} not supported, can't be uploaded".format(type(file)))

    # chat
    try:
        logger.info(f"Instruction message: {prompt}")
        content = None
        output_files = []
        user_messages = [Message(RoleType.User, prompt)]
        async for response in session.chat(user_messages):
            logger.info(f'Session Chat Response: {response}')
            if content is None:
                content = response.output_text
            else:
                content += response.output_text

            output_files.extend([output_file.__dict__() for output_file in response.output_files])

        session.messages.append(Message(RoleType.Agent, content))
        AsyncPythonSandBoxTool.kill_kernels(session.session_id)
        logger.info(f"Release python sandbox {session.session_id}")
        logger.info(f"Total Latency: {time.time() - start_time}")

        return content

    except (ModelMaxIterationsException, DependencyException, InputErrorException, InternalErrorException, Exception) \
            as e:
        exception_messages = {
            ModelMaxIterationsException: "Sorry. The agent didn't find the correct answer after multiple trials, "
                                         "Please try another question.",
            DependencyException: "Agent failed to process message due to dependency issue. You can try it later. "
                                 "If it still happens, please contact oncall.",
            InputErrorException: "Agent failed to process message due to value issue. If you believe all input are "
                                 "correct, please contact oncall.",
            InternalErrorException: "Agent failed to process message due to internal error, please contact oncall.",
            Exception: "Agent failed to process message due to unknown error, please contact oncall."
        }
        err_msg = exception_messages.get(type(e), f"Unknown error occurred: {str(e)}")
        logger.error(err_msg, exc_info=True)
        
        raise Exception(err_msg)

import time
from typing import Union, List, Any, Dict
from io import BytesIO

from fastapi import UploadFile
from starlette.datastructures import UploadFile as StarletteUploadFile

from ..conversation_sessions import CodeInterpreterSession
from ..schemas import (
    Message,
    RoleType
)
from werkzeug.datastructures import FileStorage

from ..exceptions.exceptions import InputErrorException, DependencyException, InternalErrorException, \
    ModelMaxIterationsException

from ..utils import get_logger, upload_files

logger = get_logger()


async def predict(
        prompt: str,
        model_name: str,
        uploaded_files: Any,
        **kwargs: Dict[str, Any]):
    start_time = time.time()

    # create new session
    session = await CodeInterpreterSession.create(
        model_name=model_name,
        **kwargs
    )

    files = upload_files(uploaded_files, session.session_id)
    logger.info(f"Session Creation Latency: {time.time() - start_time}")

    # upload file
    if isinstance(files, str):
        logger.info(f"Upload {files} as file path")
        await session.upload_to_sandbox(files)
    # upload list of file
    elif isinstance(files, list):
        for file in files:
            if isinstance(file, str):
                await session.upload_to_sandbox(file)
            elif isinstance(file, UploadFile) or isinstance(file, StarletteUploadFile):
                file_content = file.file.read()  # get file content
                file_like_object = BytesIO(file_content)
                file_storage = FileStorage(
                    stream=file_like_object,
                    filename=file.filename,
                    content_type=file.content_type
                )
                await session.upload_to_sandbox(file_storage)
            else:
                raise InputErrorException("The file type {} not supported, can't be uploaded".format(type(file)))

    # chat
    try:
        logger.info(f"Instruction message: {prompt}")
        content = None
        output_files = []
        user_messages = [Message(RoleType.User, prompt)]

        async for response in session.chat(user_messages):
            logger.info(f'Session Chat Response: {response}')
            if content is None:
                content = response.output_text
            else:
                content += response.output_text

            output_files.extend([output_file.__dict__() for output_file in response.output_files])

        session.messages.append(Message(RoleType.Agent, content))

        logger.info(f"Total Latency: {time.time() - start_time}")

        return content
    except (ModelMaxIterationsException, DependencyException, InputErrorException, InternalErrorException, Exception) \
            as e:
        exception_messages = {
            ModelMaxIterationsException: "Sorry. The agent didn't find the correct answer after multiple trials, "
                                         "Please try another question.",
            DependencyException: "Agent failed to process message due to dependency issue. You can try it later. "
                                 "If it still happens, please contact oncall.",
            InputErrorException: "Agent failed to process message due to value issue. If you believe all input are "
                                 "correct, please contact oncall.",
            InternalErrorException: "Agent failed to process message due to internal error, please contact oncall.",
            Exception: "Agent failed to process message due to unknown error, please contact oncall."
        }
        err_msg = exception_messages.get(type(e), f"Unknown error occurred: {str(e)}")
        logger.error(err_msg, exc_info=True)
        
        raise Exception(err_msg)