| 67 | |
| 68 | |
| 69 | class LLMService: |
| 70 | ds: CoreDatasource |
| 71 | chat_question: ChatQuestion |
| 72 | record: ChatRecord |
| 73 | config: LLMConfig |
| 74 | llm: BaseChatModel |
| 75 | sql_message: List[Union[BaseMessage, dict[str, Any]]] |
| 76 | chart_message: List[Union[BaseMessage, dict[str, Any]]] |
| 77 | |
| 78 | # session: Session = db_session |
| 79 | current_user: CurrentUser |
| 80 | current_assistant: Optional[CurrentAssistant] = None |
| 81 | out_ds_instance: Optional[AssistantOutDs] = None |
| 82 | change_title: bool = False |
| 83 | |
| 84 | generate_sql_logs: List[ChatLog] |
| 85 | generate_chart_logs: List[ChatLog] |
| 86 | current_logs: dict[OperationEnum, ChatLog] |
| 87 | chunk_list: List[str] |
| 88 | future: Future |
| 89 | |
| 90 | trans: I18nHelper = None |
| 91 | |
| 92 | last_execute_sql_error: str = None |
| 93 | articles_number: int = 4 |
| 94 | |
| 95 | enable_sql_row_limit: bool = settings.GENERATE_SQL_QUERY_LIMIT_ENABLED |
| 96 | base_message_round_count_limit: int = settings.GENERATE_SQL_QUERY_HISTORY_ROUND_COUNT |
| 97 | |
| 98 | def __init__(self, session: Session, current_user: CurrentUser, chat_question: ChatQuestion, |
| 99 | current_assistant: Optional[CurrentAssistant] = None, no_reasoning: bool = False, |
| 100 | embedding: bool = False, config: LLMConfig = None): |
| 101 | self.sql_message = [] |
| 102 | self.chart_message = [] |
| 103 | self.generate_sql_logs = [] |
| 104 | self.generate_chart_logs = [] |
| 105 | self.current_logs = {} |
| 106 | self.chunk_list = [] |
| 107 | self.current_user = current_user |
| 108 | self.current_assistant = current_assistant |
| 109 | chat_id = chat_question.chat_id |
| 110 | chat: Chat | None = session.get(Chat, chat_id) |
| 111 | if not chat: |
| 112 | raise SingleMessageError(f"Chat with id {chat_id} not found") |
| 113 | ds: CoreDatasource | AssistantOutDsSchema | None = None |
| 114 | if not chat.datasource and chat_question.datasource_id: |
| 115 | _ds = session.get(CoreDatasource, chat_question.datasource_id) |
| 116 | if _ds: |
| 117 | if _ds.oid != current_user.oid: |
| 118 | raise SingleMessageError( |
| 119 | f"Datasource with id {chat_question.datasource_id} does not belong to current workspace") |
| 120 | chat.datasource = _ds.id |
| 121 | chat.engine_type = _ds.type_name |
| 122 | # save chat |
| 123 | session.add(chat) |
| 124 | session.flush() |
| 125 | session.refresh(chat) |
| 126 | session.commit() |
nothing calls this directly
no outgoing calls
no test coverage detected