Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import os
|
2 |
import time
|
|
|
3 |
import pandas as pd
|
|
|
4 |
from sqlalchemy import create_engine
|
5 |
from langchain_openai import ChatOpenAI
|
6 |
from langchain_community.agent_toolkits import create_sql_agent
|
@@ -8,26 +10,26 @@ from langchain_community.utilities import SQLDatabase
|
|
8 |
from huggingface_hub import InferenceClient
|
9 |
import gradio as gr
|
10 |
from dotenv import load_dotenv
|
11 |
-
import logging
|
12 |
|
13 |
load_dotenv()
|
14 |
|
15 |
CSV_FILE_PATH = "tabela_tiago_formated.csv"
|
16 |
SQL_DB_PATH = "data.db"
|
17 |
-
|
18 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
19 |
LLAMA_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
hf_client = InferenceClient(api_key=HUGGINGFACE_API_KEY)
|
22 |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
23 |
|
24 |
query_cache = {}
|
25 |
-
history_log = []
|
26 |
-
recent_history = []
|
27 |
-
show_history_flag = False
|
28 |
-
|
29 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
30 |
|
|
|
31 |
def create_or_load_sql_database(csv_path, sql_db_path):
|
32 |
if os.path.exists(sql_db_path):
|
33 |
print("Banco de dados SQL já existe. Carregando...")
|
@@ -41,11 +43,11 @@ def create_or_load_sql_database(csv_path, sql_db_path):
|
|
41 |
print("Banco de dados SQL criado com sucesso!")
|
42 |
return engine
|
43 |
|
44 |
-
engine = create_or_load_sql_database(
|
45 |
db = SQLDatabase(engine=engine)
|
46 |
|
47 |
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
48 |
-
sql_agent = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True, max_iterations=
|
49 |
|
50 |
def generate_initial_context(db_sample):
|
51 |
return (
|
@@ -63,97 +65,117 @@ def is_greeting(user_query):
|
|
63 |
greetings = ["olá", "oi", "bom dia", "boa tarde", "boa noite", "oi, tudo bem?"]
|
64 |
return user_query.lower().strip() in greetings
|
65 |
|
66 |
-
def query_with_llama(user_query, db_sample):
|
67 |
initial_context = generate_initial_context(db_sample)
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
72 |
full_prompt = f"{initial_context}\n\nHistórico recente:\n{formatted_history}\n\nPergunta do usuário:\n{user_query}"
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
start_time = time.time()
|
|
|
77 |
try:
|
78 |
response = hf_client.chat.completions.create(
|
79 |
model=LLAMA_MODEL,
|
80 |
messages=[{"role": "system", "content": full_prompt}],
|
81 |
-
max_tokens=
|
82 |
stream=False
|
83 |
)
|
84 |
llama_response = response["choices"][0]["message"]["content"]
|
85 |
end_time = time.time()
|
86 |
-
|
87 |
-
|
|
|
88 |
except Exception as e:
|
89 |
-
|
90 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
try:
|
94 |
if user_query in query_cache:
|
95 |
-
print(
|
96 |
-
return query_cache[user_query]
|
97 |
|
98 |
if is_greeting(user_query):
|
99 |
greeting_response = "Olá! Estou aqui para ajudar com suas consultas. Pergunte algo relacionado aos dados carregados no agente!"
|
100 |
-
|
101 |
-
return greeting_response
|
102 |
|
103 |
column_data = pd.read_sql_query("SELECT * FROM tabela_tiago_formated LIMIT 10", engine)
|
104 |
-
|
|
|
105 |
if not llama_instruction:
|
106 |
-
return "Erro: O modelo Llama não conseguiu gerar uma instrução válida."
|
107 |
|
108 |
print("------- Agent SQL: Executando query -------")
|
|
|
|
|
109 |
response = sql_agent.invoke({"input": llama_instruction})
|
110 |
-
sql_response = response.get("output", "Erro ao obter a resposta do agente
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
query_cache[user_query] = sql_response
|
113 |
-
return sql_response
|
114 |
-
|
115 |
except Exception as e:
|
116 |
-
return f"Erro ao consultar o agente SQL: {e}"
|
117 |
|
118 |
-
def
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
return
|
137 |
-
|
138 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
139 |
-
gr.Markdown("# Anomalia Agent")
|
140 |
-
chatbot = gr.Chatbot(height=600)
|
141 |
-
msg = gr.Textbox(placeholder="Digite sua pergunta aqui...", label=" ", lines=1)
|
142 |
-
|
143 |
-
def respond(message, chat_history):
|
144 |
-
response = chatbot_response(message)
|
145 |
-
chat_history.append((message, response))
|
146 |
-
return "", chat_history
|
147 |
-
|
148 |
-
with gr.Row():
|
149 |
-
btn = gr.Button("Enviar", variant="primary")
|
150 |
-
history_btn = gr.Button("Histórico", variant="secondary")
|
151 |
-
|
152 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
153 |
-
btn.click(respond, [msg, chatbot], [msg, chatbot])
|
154 |
-
|
155 |
-
history_output = gr.JSON()
|
156 |
-
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
157 |
|
158 |
if __name__ == "__main__":
|
|
|
159 |
demo.launch(share=False)
|
|
|
1 |
import os
|
2 |
import time
|
3 |
+
import json
|
4 |
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
from sqlalchemy import create_engine
|
7 |
from langchain_openai import ChatOpenAI
|
8 |
from langchain_community.agent_toolkits import create_sql_agent
|
|
|
10 |
from huggingface_hub import InferenceClient
|
11 |
import gradio as gr
|
12 |
from dotenv import load_dotenv
|
|
|
13 |
|
14 |
load_dotenv()
|
15 |
|
16 |
CSV_FILE_PATH = "tabela_tiago_formated.csv"
|
17 |
SQL_DB_PATH = "data.db"
|
18 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
19 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
20 |
LLAMA_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
21 |
+
LLAMA_MODEL_FINAL = "Qwen/QwQ-32B"
|
22 |
+
|
23 |
+
hf_client = InferenceClient(
|
24 |
+
provider="sambanova",
|
25 |
+
api_key=HF_TOKEN,
|
26 |
+
)
|
27 |
|
|
|
28 |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
29 |
|
30 |
query_cache = {}
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Criar banco de dados SQL a partir do JSON
|
33 |
def create_or_load_sql_database(csv_path, sql_db_path):
|
34 |
if os.path.exists(sql_db_path):
|
35 |
print("Banco de dados SQL já existe. Carregando...")
|
|
|
43 |
print("Banco de dados SQL criado com sucesso!")
|
44 |
return engine
|
45 |
|
46 |
+
engine = create_or_load_sql_database(JSON_FILE_PATH, SQL_DB_PATH)
|
47 |
db = SQLDatabase(engine=engine)
|
48 |
|
49 |
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
50 |
+
sql_agent = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True, max_iterations=20, return_intermediate_steps=True)
|
51 |
|
52 |
def generate_initial_context(db_sample):
|
53 |
return (
|
|
|
65 |
greetings = ["olá", "oi", "bom dia", "boa tarde", "boa noite", "oi, tudo bem?"]
|
66 |
return user_query.lower().strip() in greetings
|
67 |
|
68 |
+
def query_with_llama(user_query, db_sample, chat_history):
|
69 |
initial_context = generate_initial_context(db_sample)
|
70 |
+
|
71 |
+
formatted_history = "" # Não inclui histórico temporariamente
|
72 |
+
|
73 |
+
#formatted_history = "\n".join(
|
74 |
+
#[f"{msg['role'].capitalize()}: {msg['content']}" for msg in chat_history[-0:]]
|
75 |
+
#)
|
76 |
+
|
77 |
full_prompt = f"{initial_context}\n\nHistórico recente:\n{formatted_history}\n\nPergunta do usuário:\n{user_query}"
|
78 |
+
|
79 |
+
print("------- Modelo Llama 3.3: Gerando query SQL -------")
|
80 |
+
print(f"[DEBUG] Contexto enviado ao Llama:\n{full_prompt}\n")
|
81 |
start_time = time.time()
|
82 |
+
|
83 |
try:
|
84 |
response = hf_client.chat.completions.create(
|
85 |
model=LLAMA_MODEL,
|
86 |
messages=[{"role": "system", "content": full_prompt}],
|
87 |
+
max_tokens=600,
|
88 |
stream=False
|
89 |
)
|
90 |
llama_response = response["choices"][0]["message"]["content"]
|
91 |
end_time = time.time()
|
92 |
+
print(f"[DEBUG] Resposta do Llama: {llama_response.strip()}\n[Tempo de execução: {end_time - start_time:.2f}s]\n")
|
93 |
+
chat_history.append({"role": "assistant", "content": llama_response})
|
94 |
+
return llama_response.strip(), chat_history
|
95 |
except Exception as e:
|
96 |
+
print(f"[ERRO] Falha ao interagir com o Llama: {e}")
|
97 |
+
return None, chat_history
|
98 |
+
|
99 |
+
def refine_response_with_llama(user_query, sql_response):
|
100 |
+
prompt = f"""
|
101 |
+
Você é um assistente especializado em consolidar informações. Abaixo estão as entradas:\n
|
102 |
+
Pergunta:
|
103 |
+
{user_query}
|
104 |
+
Resposta do Agente:
|
105 |
+
{sql_response}\n
|
106 |
+
"""
|
107 |
+
|
108 |
+
print("------- Modelo Final: Refinando resposta -------")
|
109 |
+
print(f"[DEBUG] Contexto enviado ao Llama Final:\n{prompt}\n")
|
110 |
+
start_time = time.time()
|
111 |
|
112 |
+
try:
|
113 |
+
response = hf_client.chat.completions.create(
|
114 |
+
model=LLAMA_MODEL_FINAL,
|
115 |
+
messages=[{"role": "system", "content": prompt}],
|
116 |
+
max_tokens=8000
|
117 |
+
)
|
118 |
+
|
119 |
+
final_response = response["choices"][0]["message"]["content"]
|
120 |
+
end_time = time.time()
|
121 |
+
print(f"[DEBUG] Resposta do Llama Final: {final_response.strip()}\n[Tempo de execução: {end_time - start_time:.2f}s]\n")
|
122 |
+
return final_response.strip()
|
123 |
+
|
124 |
+
except Exception as e:
|
125 |
+
print(f"[ERRO] Falha ao interagir com o Llama Final: {e}")
|
126 |
+
return "Erro ao consolidar a resposta. Por favor, tente novamente."
|
127 |
+
|
128 |
+
def query_sql_agent(user_query, chat_history):
|
129 |
try:
|
130 |
if user_query in query_cache:
|
131 |
+
print("[CACHE] Retornando resposta do cache para a consulta: {user_query}")
|
132 |
+
return query_cache[user_query], chat_history
|
133 |
|
134 |
if is_greeting(user_query):
|
135 |
greeting_response = "Olá! Estou aqui para ajudar com suas consultas. Pergunte algo relacionado aos dados carregados no agente!"
|
136 |
+
return greeting_response, chat_history
|
|
|
137 |
|
138 |
column_data = pd.read_sql_query("SELECT * FROM tabela_tiago_formated LIMIT 10", engine)
|
139 |
+
|
140 |
+
llama_instruction, chat_history = query_with_llama(user_query, column_data, chat_history)
|
141 |
if not llama_instruction:
|
142 |
+
return "Erro: O modelo Llama não conseguiu gerar uma instrução válida.", chat_history
|
143 |
|
144 |
print("------- Agent SQL: Executando query -------")
|
145 |
+
print(f"[DEBUG] Instrução gerada pelo Llama:\n{llama_instruction}\n")
|
146 |
+
start_time = time.time()
|
147 |
response = sql_agent.invoke({"input": llama_instruction})
|
148 |
+
sql_response = response.get("output", "Erro ao obter a resposta do agente.")
|
149 |
+
end_time = time.time()
|
150 |
+
print(f"[DEBUG] Resposta do Agent SQL: {sql_response}\n[Tempo de execução: {end_time - start_time:.2f}s]\n")
|
151 |
+
|
152 |
+
final_response = refine_response_with_llama(user_query, sql_response)
|
153 |
+
query_cache[user_query] = final_response # Armazenar no cache
|
154 |
+
return final_response, chat_history
|
155 |
|
|
|
|
|
|
|
156 |
except Exception as e:
|
157 |
+
return f"Erro ao consultar o agente SQL: {e}", chat_history
|
158 |
|
159 |
+
def gradio_interface():
|
160 |
+
chat_history = []
|
161 |
+
|
162 |
+
def respond(user_input):
|
163 |
+
nonlocal chat_history
|
164 |
+
response, chat_history = query_sql_agent(user_input, chat_history)
|
165 |
+
chat_history.append({"role": "user", "content": user_input})
|
166 |
+
chat_history.append({"role": "assistant", "content": response})
|
167 |
+
return [{"role": "user", "content": user_input}, {"role": "assistant", "content": response}]
|
168 |
+
|
169 |
+
with gr.Blocks() as demo:
|
170 |
+
gr.Markdown("# Tributario Agent")
|
171 |
+
chatbot = gr.Chatbot(label="Tributario Result Agent", type="messages")
|
172 |
+
user_input = gr.Textbox(label="Digite sua pergunta")
|
173 |
+
submit_button = gr.Button("Enviar")
|
174 |
+
|
175 |
+
submit_button.click(respond, inputs=user_input, outputs=chatbot)
|
176 |
+
|
177 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
+
demo = gradio_interface()
|
181 |
demo.launch(share=False)
|