Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions interface/lang2sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ def execute_query(
def display_result(
*,
res: dict,
database: BaseConnector,
) -> None:
"""
Lang2SQL 실행 결과를 Streamlit 화면에 출력합니다.

Args:
res (dict): Lang2SQL 실행 결과 딕셔너리.
database (ConnectDB): SQL 쿼리 실행을 위한 데이터베이스 연결 객체.

출력 항목:
- 총 토큰 사용량
Expand Down Expand Up @@ -240,8 +238,8 @@ def _as_float(value):
if not has_query:
st.info("QUERY_MAKER 없이 실행되었습니다. 검색된 테이블 정보만 표시합니다.")

if show_table_section:
st.markdown("---")
if show_table_section or show_chart_section:
database = get_db_connector()
try:
sql_raw = (
res["generated_query"].content
Expand All @@ -251,23 +249,24 @@ def _as_float(value):
if isinstance(sql_raw, str):
sql = LLMResponseParser.extract_sql(sql_raw)
df = database.run_sql(sql)
st.dataframe(df.head(10) if len(df) > 10 else df)
else:
st.error("SQL 원본이 문자열이 아닙니다.")
except Exception as e:
st.markdown("---")
st.error(f"쿼리 실행 중 오류 발생: {e}")
df = None

if show_chart_section:
st.markdown("---")
try:
sql_raw = (
res["generated_query"].content
if isinstance(res["generated_query"], AIMessage)
else str(res["generated_query"])
)
if isinstance(sql_raw, str):
sql = LLMResponseParser.extract_sql(sql_raw)
df = database.run_sql(sql)
if df is not None and show_table_section:
st.markdown("---")
st.markdown("**쿼리 실행 결과:**")
try:
st.dataframe(df.head(10) if len(df) > 10 else df)
except Exception as e:
st.error(f"결과 테이블 생성 중 오류 발생: {e}")

if df is not None and show_chart_section:
st.markdown("---")
try:
st.markdown("**쿼리 결과 시각화:**")
try:
if len(res["messages"]) > 1:
Expand All @@ -292,13 +291,9 @@ def _as_float(value):
plotly_code=display_code.generate_plotly_code(), df=df
)
st.plotly_chart(fig)
else:
st.error("SQL 원본이 문자열이 아닙니다.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💊 제 기억이 확실하지 않지만..... 리마인드 차원으로 기록만 해두겠습니다~
이 else가 필요했던 이유가 우리는 크게보면 "한국어로 질문" -> "LLM에서 쿼리를 리턴" -> "리턴받은 쿼리를 실행" 인데,
간헐적으로 질문에 따라 LLM에게 리턴받은 쿼리가.. 쿼리 형태가 아닌 경우가 있어서 이런식으로 예외처리를 했던 기억이 있습니다.
추후에 다양한 질문으로 테스트해볼텐데, 요 부분에서 자꾸 걸리는 현상이 있으면 다시 살펴보면 좋을 것 같습니다~

except Exception as e:
st.error(f"차트 생성 중 오류 발생: {e}")

except Exception as e:
st.error(f"차트 생성 중 오류 발생: {e}")

db = get_db_connector()

st.title(TITLE)

Expand Down Expand Up @@ -401,4 +396,4 @@ def _as_float(value):
top_n=user_top_n,
device=device,
)
display_result(res=result, database=db)
display_result(res=result)
Loading