11import asyncio
22import io
33import traceback
4- from typing import Optional
4+ from typing import Optional , List
55
66import orjson
77import pandas as pd
8- from fastapi import APIRouter , HTTPException
8+ from fastapi import APIRouter , HTTPException , Path
99from fastapi .responses import StreamingResponse
1010from sqlalchemy import and_ , select
1111
1212from apps .chat .curd .chat import list_chats , get_chat_with_records , create_chat , rename_chat , \
1313 delete_chat , get_chat_chart_data , get_chat_predict_data , get_chat_with_records_with_data , get_chat_record_by_id , \
1414 format_json_data , format_json_list_data , get_chart_config , list_recent_questions
15- from apps .chat .models .chat_model import CreateChat , ChatRecord , RenameChat , ChatQuestion , AxisObj , QuickCommand
15+ from apps .chat .models .chat_model import CreateChat , ChatRecord , RenameChat , ChatQuestion , AxisObj , QuickCommand , \
16+ ChatInfo , Chat
1617from apps .chat .task .llm import LLMService
18+ from apps .swagger .i18n import PLACEHOLDER_PREFIX
1719from apps .system .schemas .permission import SqlbotPermission , require_permissions
1820from common .core .deps import CurrentAssistant , SessionDep , CurrentUser , Trans
1921from common .utils .command_utils import parse_quick_command
2224router = APIRouter (tags = ["Data Q&A" ], prefix = "/chat" )
2325
2426
25- @router .get ("/list" )
27+ @router .get ("/list" , response_model = List [ Chat ], summary = f" { PLACEHOLDER_PREFIX } get_chat_list" )
2628async def chats (session : SessionDep , current_user : CurrentUser ):
2729 return list_chats (session , current_user )
2830
2931
30- @router .get ("/{chart_id}" )
32+ @router .get ("/{chart_id}" , response_model = ChatInfo , summary = f" { PLACEHOLDER_PREFIX } get_chat" )
3133async def get_chat (session : SessionDep , current_user : CurrentUser , chart_id : int , current_assistant : CurrentAssistant ,
3234 trans : Trans ):
3335 def inner ():
@@ -37,7 +39,7 @@ def inner():
3739 return await asyncio .to_thread (inner )
3840
3941
40- @router .get ("/{chart_id}/with_data" )
42+ @router .get ("/{chart_id}/with_data" , response_model = ChatInfo , summary = f" { PLACEHOLDER_PREFIX } get_chat_with_data" )
4143async def get_chat_with_data (session : SessionDep , current_user : CurrentUser , chart_id : int ,
4244 current_assistant : CurrentAssistant ):
4345 def inner ():
@@ -47,7 +49,7 @@ def inner():
4749 return await asyncio .to_thread (inner )
4850
4951
50- @router .get ("/record/{chat_record_id}/data" )
52+ @router .get ("/record/{chat_record_id}/data" , summary = f" { PLACEHOLDER_PREFIX } get_chart_data" )
5153async def chat_record_data (session : SessionDep , chat_record_id : int ):
5254 def inner ():
5355 data = get_chat_chart_data (chat_record_id = chat_record_id , session = session )
@@ -56,7 +58,7 @@ def inner():
5658 return await asyncio .to_thread (inner )
5759
5860
59- @router .get ("/record/{chat_record_id}/predict_data" )
61+ @router .get ("/record/{chat_record_id}/predict_data" , summary = f" { PLACEHOLDER_PREFIX } get_chart_predict_data" )
6062async def chat_predict_data (session : SessionDep , chat_record_id : int ):
6163 def inner ():
6264 data = get_chat_predict_data (chat_record_id = chat_record_id , session = session )
@@ -65,7 +67,7 @@ def inner():
6567 return await asyncio .to_thread (inner )
6668
6769
68- @router .post ("/rename" )
70+ @router .post ("/rename" , response_model = str , summary = f" { PLACEHOLDER_PREFIX } rename_chat" )
6971async def rename (session : SessionDep , chat : RenameChat ):
7072 try :
7173 return rename_chat (session = session , rename_object = chat )
@@ -76,7 +78,7 @@ async def rename(session: SessionDep, chat: RenameChat):
7678 )
7779
7880
79- @router .delete ("/{chart_id}" )
81+ @router .delete ("/{chart_id}" , response_model = str , summary = f" { PLACEHOLDER_PREFIX } delete_chat" )
8082async def delete (session : SessionDep , chart_id : int ):
8183 try :
8284 return delete_chat (session = session , chart_id = chart_id )
@@ -87,7 +89,7 @@ async def delete(session: SessionDep, chart_id: int):
8789 )
8890
8991
90- @router .post ("/start" )
92+ @router .post ("/start" , response_model = ChatInfo , summary = f" { PLACEHOLDER_PREFIX } start_chat" )
9193@require_permissions (permission = SqlbotPermission (type = 'ds' , keyExpression = "create_chat_obj.datasource" ))
9294async def start_chat (session : SessionDep , current_user : CurrentUser , create_chat_obj : CreateChat ):
9395 try :
@@ -99,7 +101,7 @@ async def start_chat(session: SessionDep, current_user: CurrentUser, create_chat
99101 )
100102
101103
102- @router .post ("/assistant/start" )
104+ @router .post ("/assistant/start" , response_model = ChatInfo , summary = f" { PLACEHOLDER_PREFIX } assistant_start_chat" )
103105async def start_chat (session : SessionDep , current_user : CurrentUser ):
104106 try :
105107 return create_chat (session , current_user , CreateChat (origin = 2 ), False )
@@ -110,9 +112,9 @@ async def start_chat(session: SessionDep, current_user: CurrentUser):
110112 )
111113
112114
113- @router .post ("/recommend_questions/{chat_record_id}" )
114- async def recommend_questions (session : SessionDep , current_user : CurrentUser , chat_record_id : int ,
115- current_assistant : CurrentAssistant , articles_number : Optional [int ] = 4 ):
115+ @router .post ("/recommend_questions/{chat_record_id}" , summary = f" { PLACEHOLDER_PREFIX } ask_recommend_questions" )
116+ async def ask_recommend_questions (session : SessionDep , current_user : CurrentUser , chat_record_id : int ,
117+ current_assistant : CurrentAssistant , articles_number : Optional [int ] = 4 ):
116118 def _return_empty ():
117119 yield 'data:' + orjson .dumps ({'content' : '[]' , 'type' : 'recommended_question' }).decode () + '\n \n '
118120
@@ -139,9 +141,11 @@ def _err(_e: Exception):
139141 return StreamingResponse (llm_service .await_result (), media_type = "text/event-stream" )
140142
141143
142- @router .get ("/recent_questions/{datasource_id}" )
144+ @router .get ("/recent_questions/{datasource_id}" , response_model = List [str ],
145+ summary = f"{ PLACEHOLDER_PREFIX } get_recommend_questions" )
143146@require_permissions (permission = SqlbotPermission (type = 'ds' , keyExpression = "datasource_id" ))
144- async def recommend_questions (session : SessionDep , current_user : CurrentUser , datasource_id : int ):
147+ async def recommend_questions (session : SessionDep , current_user : CurrentUser ,
148+ datasource_id : int = Path (..., description = f"{ PLACEHOLDER_PREFIX } ds_id" )):
145149 return list_recent_questions (session = session , current_user = current_user , datasource_id = datasource_id )
146150
147151
@@ -158,7 +162,7 @@ def find_base_question(record_id: int, session: SessionDep):
158162 return rec_question
159163
160164
161- @router .post ("/question" )
165+ @router .post ("/question" , summary = f" { PLACEHOLDER_PREFIX } ask_question" )
162166@require_permissions (permission = SqlbotPermission (type = 'chat' , keyExpression = "request_question.chat_id" ))
163167async def question_answer (session : SessionDep , current_user : CurrentUser , request_question : ChatQuestion ,
164168 current_assistant : CurrentAssistant ):
@@ -255,10 +259,10 @@ def _err(_e: Exception):
255259 return StreamingResponse (llm_service .await_result (), media_type = "text/event-stream" )
256260
257261
258- @router .post ("/record/{chat_record_id}/{action_type}" )
259- async def analysis_or_predict_question (session : SessionDep , current_user : CurrentUser , chat_record_id : int ,
260- action_type : str ,
261- current_assistant : CurrentAssistant ):
262+ @router .post ("/record/{chat_record_id}/{action_type}" , summary = f" { PLACEHOLDER_PREFIX } analysis_or_predict" )
263+ async def analysis_or_predict_question (session : SessionDep , current_user : CurrentUser ,
264+ current_assistant : CurrentAssistant , chat_record_id : int ,
265+ action_type : str = Path (..., description = f" { PLACEHOLDER_PREFIX } analysis_or_predict_action_type" ) ):
262266 return await analysis_or_predict (session , current_user , chat_record_id , action_type , current_assistant )
263267
264268
@@ -302,7 +306,7 @@ def _err(_e: Exception):
302306 return StreamingResponse (llm_service .await_result (), media_type = "text/event-stream" )
303307
304308
305- @router .get ("/record/{chat_record_id}/excel/export" )
309+ @router .get ("/record/{chat_record_id}/excel/export" , summary = f" { PLACEHOLDER_PREFIX } export_chart_data" )
306310async def export_excel (session : SessionDep , chat_record_id : int , trans : Trans ):
307311 chat_record = session .get (ChatRecord , chat_record_id )
308312 if not chat_record :
0 commit comments