Spaces:
Sleeping
Sleeping
feat(core): implement chat route with function-calling support
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
| 3 |
-
from flask import Flask, render_template
|
| 4 |
from user_data import get_user_history
|
|
|
|
| 5 |
from book_recommendations import (
|
| 6 |
recommend_fiction,
|
| 7 |
recommend_non_fiction,
|
|
@@ -29,21 +30,48 @@ magical_if = genai.GenerativeModel(
|
|
| 29 |
|
| 30 |
def ia_decision(user_id, history):
|
| 31 |
business_rules = """
|
| 32 |
-
Analise o histórico de compras do usuário e
|
| 33 |
-
|
|
|
|
| 34 |
Regras:
|
| 35 |
1. Se o usuário comprou mais livros de ficção, recomende ficção.
|
| 36 |
2. Se o usuário comprou mais livros de não ficção, recomende não ficção.
|
| 37 |
3. Se o usuário demonstrou interesse em ciência, recomende ciência.
|
| 38 |
-
4.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
user_decision = magical_if.start_chat(
|
| 41 |
enable_automatic_function_calling=True
|
| 42 |
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
f"
|
|
|
|
| 46 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
return response.text
|
| 48 |
|
| 49 |
|
|
@@ -53,6 +81,7 @@ def recommend(user_id):
|
|
| 53 |
if not history:
|
| 54 |
return "Usuário não encontrado", 404
|
| 55 |
ia_response = ia_decision(user_id, history)
|
|
|
|
| 56 |
return render_template(
|
| 57 |
'recommendation.html',
|
| 58 |
user_id=user_id,
|
|
@@ -60,5 +89,49 @@ def recommend(user_id):
|
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if __name__ == '__main__':
|
| 64 |
app.run(debug=True)
|
|
|
|
| 1 |
import os
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
from flask import Flask, render_template, request
|
| 4 |
from user_data import get_user_history
|
| 5 |
+
from logs import save_log
|
| 6 |
from book_recommendations import (
|
| 7 |
recommend_fiction,
|
| 8 |
recommend_non_fiction,
|
|
|
|
| 30 |
|
| 31 |
def ia_decision(user_id, history):
|
| 32 |
business_rules = """
|
| 33 |
+
Analise o histórico de compras do usuário e chame diretamente a função
|
| 34 |
+
correspondente à categoria de livros recomendada.
|
| 35 |
+
|
| 36 |
Regras:
|
| 37 |
1. Se o usuário comprou mais livros de ficção, recomende ficção.
|
| 38 |
2. Se o usuário comprou mais livros de não ficção, recomende não ficção.
|
| 39 |
3. Se o usuário demonstrou interesse em ciência, recomende ciência.
|
| 40 |
+
4. Se o usuário demonstrou interesse em fantasia, recomende fantasia.
|
| 41 |
+
5. Caso contrário, ofereça uma recomendação geral de ficção.
|
| 42 |
+
|
| 43 |
+
ATENÇÃO: Sempre retorne a resposta da função chamada.
|
| 44 |
+
Não explique, apenas chame.
|
| 45 |
"""
|
| 46 |
user_decision = magical_if.start_chat(
|
| 47 |
enable_automatic_function_calling=True
|
| 48 |
)
|
| 49 |
+
|
| 50 |
+
message = (
|
| 51 |
+
f"Histórico do usuário {user_id}: {history}\n"
|
| 52 |
+
f"Regras de negócio:\n{business_rules}"
|
| 53 |
)
|
| 54 |
+
|
| 55 |
+
response = user_decision.send_message(message)
|
| 56 |
+
|
| 57 |
+
# Tenta obter a chamada de função feita pela IA
|
| 58 |
+
try:
|
| 59 |
+
function_call = response.candidates[0].content.parts[0].function_call
|
| 60 |
+
function_name = function_call.name
|
| 61 |
+
|
| 62 |
+
user_function = {
|
| 63 |
+
"recommend_fiction": recommend_fiction,
|
| 64 |
+
"recommend_non_fiction": recommend_non_fiction,
|
| 65 |
+
"recommend_science": recommend_science,
|
| 66 |
+
"recommend_fantasy": recommend_fantasy
|
| 67 |
+
}.get(function_name)
|
| 68 |
+
|
| 69 |
+
if user_function:
|
| 70 |
+
return user_function(user_id)
|
| 71 |
+
except (AttributeError, IndexError):
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
+
# Caso nenhuma função tenha sido chamada, retorna o texto
|
| 75 |
return response.text
|
| 76 |
|
| 77 |
|
|
|
|
| 81 |
if not history:
|
| 82 |
return "Usuário não encontrado", 404
|
| 83 |
ia_response = ia_decision(user_id, history)
|
| 84 |
+
save_log(user_id, history, ia_response)
|
| 85 |
return render_template(
|
| 86 |
'recommendation.html',
|
| 87 |
user_id=user_id,
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
|
| 92 |
+
@app.route('/chat/<int:user_id>', methods=['GET', 'POST'])
|
| 93 |
+
def chat(user_id):
|
| 94 |
+
history = get_user_history(user_id)
|
| 95 |
+
if not history:
|
| 96 |
+
return "Usuário não encontrado", 404
|
| 97 |
+
|
| 98 |
+
response = None
|
| 99 |
+
|
| 100 |
+
if request.method == 'POST':
|
| 101 |
+
question = request.form.get('question')
|
| 102 |
+
user_decision = magical_if.start_chat(
|
| 103 |
+
enable_automatic_function_calling=True
|
| 104 |
+
)
|
| 105 |
+
gemini_response = user_decision.send_message(
|
| 106 |
+
f"Usuário {user_id}, histórico: {history}. Pergunta: {question}"
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
function_call = (
|
| 111 |
+
gemini_response
|
| 112 |
+
.candidates[0]
|
| 113 |
+
.content.parts[0]
|
| 114 |
+
.function_call
|
| 115 |
+
)
|
| 116 |
+
function_name = function_call.name
|
| 117 |
+
user_function = {
|
| 118 |
+
"recommend_fiction": recommend_fiction,
|
| 119 |
+
"recommend_non_fiction": recommend_non_fiction,
|
| 120 |
+
"recommend_science": recommend_science,
|
| 121 |
+
"recommend_fantasy": recommend_fantasy
|
| 122 |
+
}.get(function_name)
|
| 123 |
+
|
| 124 |
+
if user_function:
|
| 125 |
+
response = user_function(user_id)
|
| 126 |
+
else:
|
| 127 |
+
response = gemini_response.text
|
| 128 |
+
except (AttributeError, IndexError):
|
| 129 |
+
response = gemini_response.text
|
| 130 |
+
|
| 131 |
+
save_log(user_id, history, response)
|
| 132 |
+
|
| 133 |
+
return render_template('chat.html', user_id=user_id, response=response)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
if __name__ == '__main__':
|
| 137 |
app.run(debug=True)
|