-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit.py
More file actions
229 lines (181 loc) · 8.64 KB
/
streamlit.py
File metadata and controls
229 lines (181 loc) · 8.64 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from langchain_community.utilities import SQLDatabase
from langchain.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.chat_history import InMemoryChatMessageHistory, BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.output_parsers import StrOutputParser
from sqlalchemy import create_engine, text
from dotenv import load_dotenv
import os
import re
import streamlit as st
load_dotenv()
# Model konfigurasyonu
model = ChatGoogleGenerativeAI(
model="gemini-1.5-flash",
api_key=os.getenv("GEMINI_API_KEY"),
temperature=0.3
)
# Veri tabani baglantisinin saglanmasi
db_uri = f"postgresql+psycopg2://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}:{os.getenv('DB_PORT')}/{os.getenv('DB_NAME')}"
db = SQLDatabase.from_uri(db_uri)
engine = create_engine(db_uri)
# SQL uretilmesi icin prompt
sql_prompt = PromptTemplate.from_template("""
Veritabanı şemasını kullanarak aşağıdaki soruya uygun BİR SQL sorgusu oluştur:
VERİTABANI ŞEMASI:
{schema}
KULLANICI SORUSU:
{question}
SADECE SQL KODU (açıklama yok, ``` yok):
""")
# LLM cevapları icin prompt
chat_prompt = ChatPromptTemplate.from_messages([
("system", "Kullanıcının sorularına dostça ve yardımcı olacak şekilde Türkçe yanıt ver."),
MessagesPlaceholder(variable_name="history"),
("human", "{question}")
])
# SQL sonucunu kullaniciya aciklayan prompt
explanation_prompt = ChatPromptTemplate.from_messages([
("system", "SQL sorgusu sonuçlarını kullanıcıya anlaşılır şekilde açıkla:"),
("human", """
SORU: {question}
SQL: {sql_query}
SONUÇLAR: {query_results}
AÇIKLAMA:
""")
])
# Chains
sql_chain = sql_prompt | model | StrOutputParser()
general_chain = chat_prompt | model | StrOutputParser()
explanation_chain = explanation_prompt | model | StrOutputParser()
# SQL normalizasyon fonksiyonu
def normalize_sql(sql_query):
sql_query = re.sub(r'(\b(FROM|JOIN|WHERE|GROUP BY|ORDER BY|HAVING)\b)(?![ ])', r'\1 ', sql_query, flags=re.IGNORECASE)
return sql_query.strip()
# Soru tipini belirleme fonksiyonu
def is_sql_question(question, schema):
# SQL anahtar kelimeleri
sql_keywords = ["select", "insert", "update", "delete", "from", "where",
"join", "group by", "order by", "having", "limit", "offset",
"count", "sum", "avg", "max", "min", "distinct"]
# Veritabani bilgilerini al
table_names = [table.lower() for table in db.get_usable_table_names()]
column_names = []
# Tum tablolardaki sutun isimlerini al
for table in db.get_usable_table_names():
table_info = db.get_table_info(table_names=[table])
columns = re.findall(r'"(\w+)"\s+\w+', table_info)
column_names.extend([col.lower() for col in columns])
question_lower = question.lower()
# SQL anahtar kelimesi kontrol
if any(keyword in question_lower for keyword in sql_keywords):
return True
# Tablo adi kontrol
if any(table in question_lower for table in table_names):
return True
# Sutun adi kontrol
if any(len(col) > 3 and col in question_lower for col in column_names):
return True
# Veriye dair spesifik ifadeler
data_related_phrases = [
"listele", "göster", "bul", "getir", "sayısı",
"miktarı", "tarihi", "filtrele", "sorgula", "rapor"
]
if any(phrase in question_lower for phrase in data_related_phrases):
return True
return False
# Chat history tutulmasi
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = {
'chat_history': InMemoryChatMessageHistory(),
'sql_history': InMemoryChatMessageHistory()
}
return store[session_id]
# SQL'den markdown ve kod bloklarını temizleme
def clean_sql_output(sql_query):
# ```sql ve ``` temizle
sql_query = re.sub(r'```sql|```', '', sql_query)
# Baştaki ve sondaki boşlukları temizle
return sql_query.strip()
# Streamlit arayüzü
if __name__ == "__main__":
st.set_page_config(page_title="SQL Sohbet Asistanı", page_icon="💬")
# Session state yönetimi
if "session_id" not in st.session_state:
st.session_state.session_id = "streamlit-session-1"
session_data = get_session_history(st.session_state.session_id)
st.session_state.chat_history = session_data['chat_history']
st.session_state.sql_history = session_data['sql_history']
# Sidebar
with st.sidebar:
st.title("Ayarlar")
st.session_state.temperature = st.slider("Model Yaratıcılık Seviyesi", 0.0, 1.0, 0.3)
st.session_state.show_sql = st.checkbox("SQL Sorgularını Göster", value=True)
if st.button("Sohbet Geçmişini Temizle"):
st.session_state.chat_history.clear()
st.session_state.sql_history.clear()
st.rerun()
# Ana sayfa
st.title("💬 SQL Sohbet Asistanı")
st.caption("Veritabanınızla konuşan akıllı asistan")
# Sohbet geçmişini göster
for msg in st.session_state.chat_history.messages:
with st.chat_message("user" if isinstance(msg, HumanMessage) else "assistant"):
st.write(msg.content)
# Kullanıcı girişi
if prompt := st.chat_input("Sorunuzu yazın"):
with st.chat_message("user"):
st.write(prompt)
try:
with st.chat_message("assistant"):
with st.spinner("Düşünüyorum..."):
if is_sql_question(prompt, db.get_table_info()):
# SQL sorgusu üret
sql_query = sql_chain.invoke({
"schema": db.get_table_info(),
"question": prompt
})
sql_query = clean_sql_output(sql_query)
sql_query = normalize_sql(sql_query)
if st.session_state.show_sql:
with st.expander("Oluşturulan SQL Sorgusu"):
st.code(sql_query, language="sql")
# Sorguyu çalıştır
with engine.connect() as conn:
result = conn.execute(text(sql_query))
rows = result.fetchall()
if rows:
result_str = "\n".join(str(row) for row in rows[:5])
if len(rows) > 5:
result_str += f"\n...({len(rows)-5} sonuç daha)"
# Sonuçları açıkla
explanation = explanation_chain.invoke({
"question": prompt,
"sql_query": sql_query,
"query_results": result_str
})
st.write(explanation)
# Geçmişe ekle
st.session_state.chat_history.add_message(HumanMessage(content=prompt))
st.session_state.chat_history.add_message(AIMessage(content=explanation))
else:
st.warning("Sorgu sonucu bulunamadı")
st.session_state.chat_history.add_message(HumanMessage(content=prompt))
st.session_state.chat_history.add_message(AIMessage(content="Sorgu sonucu bulunamadı"))
else:
# Genel sohbet
response = general_chain.invoke(
{"question": prompt, "history": st.session_state.chat_history.messages},
config={"configurable": {"session_id": st.session_state.session_id}}
)
st.write(response)
st.session_state.chat_history.add_message(HumanMessage(content=prompt))
st.session_state.chat_history.add_message(AIMessage(content=response))
except Exception as e:
st.error(f"Bir hata oluştu: {str(e)}")
st.session_state.chat_history.add_message(HumanMessage(content=prompt))
st.session_state.chat_history.add_message(AIMessage(content=f"Hata: {str(e)}"))