(session: SessionDep, current_user: CurrentUser, request_question: ChatQuestion,
current_assistant: Optional[CurrentAssistant] = None, in_chat: bool = True, stream: bool = True,
finish_step: ChatFinishStep = ChatFinishStep.GENERATE_CHART, embedding: bool = False,
return_img: bool = True)
| 368 | |
| 369 | |
| 370 | async def stream_sql(session: SessionDep, current_user: CurrentUser, request_question: ChatQuestion, |
| 371 | current_assistant: Optional[CurrentAssistant] = None, in_chat: bool = True, stream: bool = True, |
| 372 | finish_step: ChatFinishStep = ChatFinishStep.GENERATE_CHART, embedding: bool = False, |
| 373 | return_img: bool = True): |
| 374 | try: |
| 375 | llm_service = await LLMService.create(session, current_user, request_question, current_assistant, |
| 376 | embedding=embedding) |
| 377 | llm_service.init_record(session=session) |
| 378 | llm_service.run_task_async(in_chat=in_chat, stream=stream, finish_step=finish_step, return_img=return_img) |
| 379 | except Exception as e: |
| 380 | traceback.print_exc() |
| 381 | |
| 382 | if stream: |
| 383 | def _err(_e: Exception): |
| 384 | yield 'data:' + orjson.dumps({'content': str(_e), 'type': 'error'}).decode() + '\n\n' |
| 385 | |
| 386 | return StreamingResponse(_err(e), media_type="text/event-stream") |
| 387 | else: |
| 388 | return JSONResponse( |
| 389 | content={'message': str(e)}, |
| 390 | status_code=500, |
| 391 | ) |
| 392 | if stream: |
| 393 | return StreamingResponse(llm_service.await_result(), media_type="text/event-stream") |
| 394 | else: |
| 395 | res = llm_service.await_result() |
| 396 | raw_data = {} |
| 397 | for chunk in res: |
| 398 | if chunk: |
| 399 | raw_data = chunk |
| 400 | status_code = 200 |
| 401 | if not raw_data.get('success'): |
| 402 | status_code = 500 |
| 403 | |
| 404 | return JSONResponse( |
| 405 | content=raw_data, |
| 406 | status_code=status_code, |
| 407 | ) |
| 408 | |
| 409 | |
| 410 | @router.post("/record/{chat_record_id}/{action_type}", summary=f"{PLACEHOLDER_PREFIX}analysis_or_predict") |
no test coverage detected