-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
154 lines (129 loc) · 4.85 KB
/
app.py
File metadata and controls
154 lines (129 loc) · 4.85 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
from flask import Flask, send_from_directory, request, jsonify
import os
from dotenv import load_dotenv
load_dotenv()
from zenbot import *
from sage import generate_sage_reflection
from sage.sage_llm import SAGE_SYSTEM_INSTRUCTION
app = Flask(__name__, static_folder='docs')
#serve html file
@app.route('/')
def index():
return send_from_directory('docs', 'index.html')
# API routes must be registered before the catch-all static route
@app.route('/zenbot/get-bot-move', methods=['POST'])
def bot_move():
try:
data = request.get_json()
curboard = data.get('curBoard')
mode = data.get('mode')
whoseturn = data.get('whoseTurn')
castle_available = data.get('castleAvailable')
lastmove = data.get('lastMove', data.get('lastmove'))
if curboard:
if mode == "random":
chosen_move = play_random(curboard, whoseturn, castle_available, lastmove)
if mode == "minmax":
chosen_move = play_minmax(curboard, whoseturn, castle_available, lastmove)
if mode == "eval":
chosen_move = play_eval(curboard, whoseturn, castle_available, lastmove)
if mode == "sunfish":
chosen_move = play_sunfish(curboard, whoseturn, castle_available, lastmove)
return jsonify({
'success': True,
'move': chosen_move
})
else:
return jsonify({
'success': False,
'error': 'No legal moves to play'
}), 400
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
def _parse_include_engine_flag(raw) -> bool:
if raw is None:
return True
if isinstance(raw, bool):
return raw
if isinstance(raw, (int, float)):
return bool(raw)
s = str(raw).strip().lower()
if s in ('0', 'false', 'no', 'off'):
return False
return True
def _parse_include_llm_prompt_flag(raw) -> bool:
if raw is None:
return False
if isinstance(raw, bool):
return raw
if isinstance(raw, (int, float)):
return bool(raw)
s = str(raw).strip().lower()
if s in ('1', 'true', 'yes', 'on'):
return True
return False
@app.route('/api/sage-reflection', methods=['POST'])
def sage_reflection_route():
try:
data = request.get_json() or {}
board = data.get('board')
last_move = data.get('lastMove')
player_moved = data.get('playerMoved')
opening = data.get('opening') is True
prior_raw = data.get('priorReflections')
prior_reflections = []
if isinstance(prior_raw, list):
prior_reflections = [str(x).strip() for x in prior_raw if str(x).strip()]
whoseturn = data.get('whoseTurn')
if whoseturn not in ('w', 'b'):
whoseturn = None
castle_raw = data.get('castleAvailable')
castle_available = castle_raw if isinstance(castle_raw, dict) else None
include_engine = _parse_include_engine_flag(data.get('includeEngine'))
include_llm_prompt = _parse_include_llm_prompt_flag(data.get('includeLlmPrompt'))
if board is None:
return jsonify({
'success': False,
'error': 'Requête invalide : board requis.',
}), 400
common_kw = dict(
prior_reflections=prior_reflections,
whoseturn=whoseturn,
castle_available=castle_available,
include_engine=include_engine,
return_llm_prompt=include_llm_prompt,
)
if opening:
result = generate_sage_reflection(board, opening=True, **common_kw)
elif last_move is None or player_moved not in ('w', 'b'):
return jsonify({
'success': False,
'error': 'Requête invalide : lastMove et playerMoved (w|b) requis, ou opening: true.',
}), 400
else:
result = generate_sage_reflection(
board, last_move, player_moved, **common_kw
)
if include_llm_prompt:
message, user_prompt = result
return jsonify({
'success': True,
'message': message,
'llmUserPrompt': user_prompt,
'llmSystemPrompt': SAGE_SYSTEM_INSTRUCTION,
})
return jsonify({'success': True, 'message': result})
except RuntimeError as e:
return jsonify({'success': False, 'error': str(e)}), 503
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
# serve static files (after explicit routes)
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory('docs', path)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)