Update app.py
Browse files
app.py
CHANGED
|
@@ -16,9 +16,24 @@ CSV_FILE_PATH = "anomalia_vendas.csv"
|
|
| 16 |
SQL_DB_PATH = "data.db"
|
| 17 |
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 18 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
-
LLAMA_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
| 23 |
|
| 24 |
query_cache = {}
|
|
@@ -50,7 +65,6 @@ sql_agent = create_sql_agent(llm, db=db, agent_type="openai-tools", verbose=True
|
|
| 50 |
def generate_initial_context(db_sample):
|
| 51 |
return (
|
| 52 |
f"Você é um assistente que gera queries SQL objetivas e eficientes. Sempre inclua LIMIT 15 nas queries. Aqui está o banco de dados:\n\n"
|
| 53 |
-
###f"Colunas: {', '.join(db_sample.columns)}\n"
|
| 54 |
f"Exemplos do banco de dados:\n{db_sample.head().to_string(index=False)}\n\n"
|
| 55 |
"\n***IMPORTANTE***: Detecte automaticamente o idioma da pergunta do usuário e responda sempre no mesmo idioma.\n"
|
| 56 |
"Essa base de dados representa o sellout de 2025, janeiro, fevereiro e março até dia 11, de uma farmácia.\n"
|
|
@@ -65,7 +79,10 @@ def is_greeting(user_query):
|
|
| 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):
|
|
|
|
|
|
|
|
|
|
| 69 |
initial_context = generate_initial_context(db_sample)
|
| 70 |
formatted_history = "\n".join(
|
| 71 |
[f"{msg['role'].capitalize()}: {msg['content']}" for msg in recent_history[-2:]]
|
|
@@ -73,25 +90,28 @@ def query_with_llama(user_query, db_sample):
|
|
| 73 |
|
| 74 |
full_prompt = f"{initial_context}\n\nHistórico recente:\n{formatted_history}\n\nPergunta do usuário:\n{user_query}"
|
| 75 |
|
| 76 |
-
logging.info(f"[DEBUG] Contexto enviado ao
|
| 77 |
|
| 78 |
start_time = time.time()
|
|
|
|
| 79 |
try:
|
| 80 |
response = hf_client.chat.completions.create(
|
| 81 |
-
model=
|
| 82 |
messages=[{"role": "system", "content": full_prompt}],
|
| 83 |
-
max_tokens=
|
| 84 |
stream=False
|
| 85 |
)
|
|
|
|
| 86 |
llama_response = response["choices"][0]["message"]["content"]
|
| 87 |
end_time = time.time()
|
| 88 |
-
logging.info(f"[DEBUG] Resposta do
|
| 89 |
-
return llama_response.strip()
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
-
logging.error(f"[ERRO] Falha ao interagir com o
|
| 92 |
-
return None
|
| 93 |
|
| 94 |
-
def query_sql_agent(user_query):
|
| 95 |
try:
|
| 96 |
if user_query in query_cache:
|
| 97 |
print(f"[CACHE] Retornando resposta do cache para a consulta: {user_query}")
|
|
@@ -103,7 +123,8 @@ def query_sql_agent(user_query):
|
|
| 103 |
return greeting_response
|
| 104 |
|
| 105 |
column_data = pd.read_sql_query("SELECT * FROM anomalia_vendas LIMIT 10", engine)
|
| 106 |
-
llama_instruction = query_with_llama(user_query, column_data)
|
|
|
|
| 107 |
if not llama_instruction:
|
| 108 |
return "Erro: O modelo Llama não conseguiu gerar uma instrução válida."
|
| 109 |
|
|
@@ -117,19 +138,27 @@ def query_sql_agent(user_query):
|
|
| 117 |
except Exception as e:
|
| 118 |
return f"Erro ao consultar o agente SQL: {e}"
|
| 119 |
|
| 120 |
-
def chatbot_response(user_input):
|
| 121 |
start_time = time.time()
|
| 122 |
-
response = query_sql_agent(user_input)
|
| 123 |
end_time = time.time()
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
recent_history.append({"role": "user", "content": user_input})
|
| 127 |
recent_history.append({"role": "assistant", "content": response})
|
| 128 |
-
|
| 129 |
if len(recent_history) > 4:
|
| 130 |
recent_history.pop(0)
|
| 131 |
recent_history.pop(0)
|
| 132 |
-
|
| 133 |
return response
|
| 134 |
|
| 135 |
def toggle_history():
|
|
@@ -138,24 +167,33 @@ def toggle_history():
|
|
| 138 |
return history_log if show_history_flag else {}
|
| 139 |
|
| 140 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 141 |
-
gr.Markdown("# Anomalia Agent")
|
| 142 |
-
|
| 143 |
-
msg = gr.Textbox(placeholder="Digite sua pergunta aqui...", label=" ", lines=1)
|
| 144 |
-
|
| 145 |
-
def respond(message, chat_history):
|
| 146 |
-
response = chatbot_response(message)
|
| 147 |
-
chat_history.append((message, response))
|
| 148 |
-
return "", chat_history
|
| 149 |
-
|
| 150 |
with gr.Row():
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
demo.launch(share=False)
|
|
|
|
| 16 |
SQL_DB_PATH = "data.db"
|
| 17 |
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 18 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 19 |
|
| 20 |
+
LLAMA_MODELS = {
|
| 21 |
+
"LLaMA 70B": "meta-llama/Llama-3.3-70B-Instruct",
|
| 22 |
+
"LlaMA 8B": "meta-llama/Llama-3.1-8B-Instruct",
|
| 23 |
+
"Qwen 32B": "Qwen/QwQ-32B"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
MAX_TOKENS_MAP = {
|
| 27 |
+
"meta-llama/Llama-3.3-70B-Instruct": 900,
|
| 28 |
+
"meta-llama/Llama-3.1-8B-Instruct": 600,
|
| 29 |
+
"Qwen/QwQ-32B": 8192
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
hf_client = InferenceClient(
|
| 33 |
+
provider="sambanova",
|
| 34 |
+
api_key=HF_TOKEN,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
| 38 |
|
| 39 |
query_cache = {}
|
|
|
|
| 65 |
def generate_initial_context(db_sample):
|
| 66 |
return (
|
| 67 |
f"Você é um assistente que gera queries SQL objetivas e eficientes. Sempre inclua LIMIT 15 nas queries. Aqui está o banco de dados:\n\n"
|
|
|
|
| 68 |
f"Exemplos do banco de dados:\n{db_sample.head().to_string(index=False)}\n\n"
|
| 69 |
"\n***IMPORTANTE***: Detecte automaticamente o idioma da pergunta do usuário e responda sempre no mesmo idioma.\n"
|
| 70 |
"Essa base de dados representa o sellout de 2025, janeiro, fevereiro e março até dia 11, de uma farmácia.\n"
|
|
|
|
| 79 |
greetings = ["olá", "oi", "bom dia", "boa tarde", "boa noite", "oi, tudo bem?"]
|
| 80 |
return user_query.lower().strip() in greetings
|
| 81 |
|
| 82 |
+
def query_with_llama(user_query, db_sample, selected_model_name):
|
| 83 |
+
model_id = LLAMA_MODELS[selected_model_name]
|
| 84 |
+
max_tokens = MAX_TOKENS_MAP.get(model_id, 512)
|
| 85 |
+
|
| 86 |
initial_context = generate_initial_context(db_sample)
|
| 87 |
formatted_history = "\n".join(
|
| 88 |
[f"{msg['role'].capitalize()}: {msg['content']}" for msg in recent_history[-2:]]
|
|
|
|
| 90 |
|
| 91 |
full_prompt = f"{initial_context}\n\nHistórico recente:\n{formatted_history}\n\nPergunta do usuário:\n{user_query}"
|
| 92 |
|
| 93 |
+
logging.info(f"[DEBUG] Contexto enviado ao ({selected_model_name}):\n{full_prompt}\n")
|
| 94 |
|
| 95 |
start_time = time.time()
|
| 96 |
+
|
| 97 |
try:
|
| 98 |
response = hf_client.chat.completions.create(
|
| 99 |
+
model=model_id,
|
| 100 |
messages=[{"role": "system", "content": full_prompt}],
|
| 101 |
+
max_tokens=max_tokens,
|
| 102 |
stream=False
|
| 103 |
)
|
| 104 |
+
|
| 105 |
llama_response = response["choices"][0]["message"]["content"]
|
| 106 |
end_time = time.time()
|
| 107 |
+
logging.info(f"[DEBUG] Resposta do {selected_model_name} para o Agent SQL:\n{llama_response.strip()}\n[Tempo de execução: {end_time - start_time:.2f}s]\n")
|
| 108 |
+
return llama_response.strip(), model_id
|
| 109 |
+
|
| 110 |
except Exception as e:
|
| 111 |
+
logging.error(f"[ERRO] Falha ao interagir com o modelo {selected_model_name}: {e}")
|
| 112 |
+
return None, model_id
|
| 113 |
|
| 114 |
+
def query_sql_agent(user_query, selected_model_name):
|
| 115 |
try:
|
| 116 |
if user_query in query_cache:
|
| 117 |
print(f"[CACHE] Retornando resposta do cache para a consulta: {user_query}")
|
|
|
|
| 123 |
return greeting_response
|
| 124 |
|
| 125 |
column_data = pd.read_sql_query("SELECT * FROM anomalia_vendas LIMIT 10", engine)
|
| 126 |
+
llama_instruction = query_with_llama(user_query, column_data, selected_model_name)
|
| 127 |
+
|
| 128 |
if not llama_instruction:
|
| 129 |
return "Erro: O modelo Llama não conseguiu gerar uma instrução válida."
|
| 130 |
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
return f"Erro ao consultar o agente SQL: {e}"
|
| 140 |
|
| 141 |
+
def chatbot_response(user_input, selected_model_name):
|
| 142 |
start_time = time.time()
|
| 143 |
+
response = query_sql_agent(user_input, selected_model_name)
|
| 144 |
end_time = time.time()
|
| 145 |
+
|
| 146 |
+
model_id = LLAMA_MODELS[selected_model_name]
|
| 147 |
+
|
| 148 |
+
history_log.append({
|
| 149 |
+
"Modelo LLM": model_id,
|
| 150 |
+
"Pergunta": user_input,
|
| 151 |
+
"Resposta": response,
|
| 152 |
+
"Tempo de Resposta (s)": round(end_time - start_time, 2)
|
| 153 |
+
})
|
| 154 |
+
|
| 155 |
recent_history.append({"role": "user", "content": user_input})
|
| 156 |
recent_history.append({"role": "assistant", "content": response})
|
| 157 |
+
|
| 158 |
if len(recent_history) > 4:
|
| 159 |
recent_history.pop(0)
|
| 160 |
recent_history.pop(0)
|
| 161 |
+
|
| 162 |
return response
|
| 163 |
|
| 164 |
def toggle_history():
|
|
|
|
| 167 |
return history_log if show_history_flag else {}
|
| 168 |
|
| 169 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 170 |
+
gr.Markdown("# 🧠 Anomalia Agent")
|
| 171 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
with gr.Row():
|
| 173 |
+
with gr.Column(scale=1):
|
| 174 |
+
gr.Markdown("## ⚙️ Configurações")
|
| 175 |
+
model_selector = gr.Dropdown(
|
| 176 |
+
choices=list(LLAMA_MODELS.keys()),
|
| 177 |
+
label="Escolha o Modelo LLM para gerar a query SQL",
|
| 178 |
+
value="LLaMA 70B"
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
with gr.Column(scale=4):
|
| 182 |
+
chatbot = gr.Chatbot(height=600)
|
| 183 |
+
msg = gr.Textbox(placeholder="Digite sua pergunta aqui...", label=" ", lines=1)
|
| 184 |
+
btn = gr.Button("Enviar", variant="primary")
|
| 185 |
+
history_btn = gr.Button("Histórico", variant="secondary")
|
| 186 |
+
|
| 187 |
+
def respond(message, chat_history, selected_model_name):
|
| 188 |
+
response = chatbot_response(message, selected_model_name)
|
| 189 |
+
chat_history.append((message, response))
|
| 190 |
+
return "", chat_history
|
| 191 |
+
|
| 192 |
+
msg.submit(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
| 193 |
+
btn.click(respond, [msg, chatbot, model_selector], [msg, chatbot])
|
| 194 |
+
|
| 195 |
+
history_output = gr.JSON()
|
| 196 |
+
history_btn.click(toggle_history, inputs=[], outputs=history_output)
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
demo.launch(share=False)
|