-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (114 loc) · 4.7 KB
/
app.py
File metadata and controls
149 lines (114 loc) · 4.7 KB
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
import streamlit as st
from src.core.models import AgentState
from src.analysis.graph import create_graph
from src.core.config import Config
import pandas as pd
import logging
logging.basicConfig(level=logging.INFO)
if 'query_result' not in st.session_state:
st.session_state.query_result = None
if 'chart_type' not in st.session_state:
st.session_state.chart_type = "Bar"
st.set_page_config(
page_title="SQL Sorgulama Asistanı",
page_icon="🤖",
layout="wide"
)
st.title("🤖 SQL Sorgulama Asistanı")
st.markdown("""
Bu uygulama, doğal dil ile yazılan sorguları SQL'e çevirir ve veritabanında çalıştırır.
Sorgunuzu yazın ve sonuçları görüntüleyin.
""")
with st.sidebar:
st.header("⚙️ Ayarlar")
st.text_input("Veritabanı URL", value=Config.DATABASE_URL)
provider = st.selectbox(
"LLM Sağlayıcı",
["Ollama", "OpenAI"],
index=0 if Config.USE_OLLAMA else 1
)
if provider == "OpenAI":
Config.USE_OPENAI = True
Config.USE_OLLAMA = False
model = st.text_input("OpenAI Model", value="gpt-3.5-turbo")
api_key = st.text_input("OpenAI API Key", type="password")
else:
Config.USE_OLLAMA = True
Config.USE_OPENAI = False
model = st.text_input("Ollama Model", value=Config.LLM_MODEL)
base_url = st.text_input("Ollama Base URL", value=Config.OLLAMA_BASE_URL)
temperature = st.slider(
"Sıcaklık",
min_value=0.0,
max_value=1.0,
value=float(Config.LLM_TEMPERATURE),
step=0.1
)
query = st.text_area(
"Sorgunuzu yazın:",
placeholder="Örnek: Show me the total number of employees for each level",
height=100
)
if st.button("🚀 Sorguyu Çalıştır", type="primary"):
if not query:
st.error("Lütfen bir sorgu yazın!")
else:
with st.spinner("Sorgu işleniyor..."):
try:
initial_state = AgentState(
user_query=query,
attempts=0,
correct_attempts=0,
)
graph = create_graph()
final_state = graph.invoke(initial_state)
st.session_state.query_result = final_state
st.success("Sorgu başarıyla çalıştırıldı!")
with st.expander("🤔 Mantıksal Adımlar"):
st.markdown(final_state["sql_reasoning"])
with st.expander("📝 Oluşturulan SQL Sorgusu"):
st.code(final_state["generated_sql"], language="sql")
except Exception as e:
st.error(f"Bir hata oluştu: {str(e)}")
st.session_state.query_result = None
if st.session_state.query_result:
final_state = st.session_state.query_result
st.header("📊 Sonuçlar")
if final_state["execution_result"].success:
df = pd.DataFrame(final_state["execution_result"].data)
st.dataframe(
df,
use_container_width=True,
hide_index=True
)
if not df.empty and len(df.columns) == 2:
col1, col2 = st.columns([1, 3])
with col1:
st.session_state.chart_type = st.selectbox(
"Grafik türü seçin:",
["Bar", "Line", "Scatter"],
index=["Bar", "Line", "Scatter"].index(st.session_state.chart_type)
)
with col2:
if st.session_state.chart_type == "Bar":
st.bar_chart(df, x=df.columns[0],y=df.columns[1],use_container_width=True)
elif st.session_state.chart_type == "Line":
st.line_chart(df, x=df.columns[0],y=df.columns[1],use_container_width=True)
else:
st.scatter_chart(df, x=df.columns[0],y=df.columns[1], use_container_width=True)
else:
st.error(f"Sorgu hatası: {final_state['execution_result'].error}")
if final_state["report_summary"]:
with st.expander("📝 Özet"):
st.markdown(final_state["report_summary"])
with st.expander("❓ Nasıl Kullanılır?"):
st.markdown("""
1. Yan panelden LLM sağlayıcı ve model seçin
2. Sorgunuzu doğal dil ile yazın
3. 'Sorguyu Çalıştır' butonuna tıklayın
4. Sonuçları ve grafikleri inceleyin
**Örnek Sorgular:**
- Show me the total number of employees for each level
- What is the average salary by department?
- Show me the highest paid employees in each department
""")