Update app-backup.py
Browse files- app-backup.py +52 -49
app-backup.py
CHANGED
@@ -12,11 +12,11 @@ from typing import List, Dict, Optional, Any
|
|
12 |
import logging
|
13 |
import uuid
|
14 |
|
15 |
-
#
|
16 |
try:
|
17 |
from recursive_thinking_ai import EnhancedRecursiveThinkingChat
|
18 |
except ModuleNotFoundError:
|
19 |
-
#
|
20 |
import sys
|
21 |
sys.path.append('.')
|
22 |
from recursive_thinking_ai import EnhancedRecursiveThinkingChat
|
@@ -29,8 +29,8 @@ logging.basicConfig(
|
|
29 |
logger = logging.getLogger(__name__)
|
30 |
|
31 |
app = FastAPI(
|
32 |
-
title="Chain
|
33 |
-
description="
|
34 |
version="1.0.0"
|
35 |
)
|
36 |
|
@@ -46,14 +46,14 @@ app.add_middleware(
|
|
46 |
# Create a dictionary to store chat instances
|
47 |
chat_instances = {}
|
48 |
|
49 |
-
#
|
50 |
API_KEY = os.getenv("OPENROUTE_API")
|
51 |
if not API_KEY:
|
52 |
-
logger.warning("OPENROUTE_API
|
53 |
|
54 |
# Pydantic models for request/response validation
|
55 |
class ChatConfig(BaseModel):
|
56 |
-
# api_key
|
57 |
model: str = "mistralai/mistral-small-3.1-24b-instruct:free"
|
58 |
temperature: Optional[float] = Field(default=0.7, ge=0.0, le=1.0)
|
59 |
|
@@ -82,7 +82,7 @@ class InitResponse(BaseModel):
|
|
82 |
session_id: str
|
83 |
status: str
|
84 |
|
85 |
-
#
|
86 |
@app.get("/", response_class=HTMLResponse)
|
87 |
async def root():
|
88 |
"""Root endpoint with a simple HTML interface"""
|
@@ -90,7 +90,7 @@ async def root():
|
|
90 |
<!DOCTYPE html>
|
91 |
<html>
|
92 |
<head>
|
93 |
-
<title>
|
94 |
<style>
|
95 |
body {{
|
96 |
font-family: Arial, sans-serif;
|
@@ -149,48 +149,53 @@ async def root():
|
|
149 |
</style>
|
150 |
</head>
|
151 |
<body>
|
152 |
-
<h1>
|
153 |
<div class="container">
|
154 |
<div id="init-form">
|
155 |
-
<h2>1.
|
156 |
|
157 |
-
<!-- API
|
158 |
-
<label for="model"
|
159 |
<input type="text" id="model" value="mistralai/mistral-small-3.1-24b-instruct:free">
|
160 |
|
161 |
-
<label for="temperature"
|
162 |
<input type="number" id="temperature" min="0" max="1" step="0.1" value="0.7">
|
163 |
|
164 |
-
<button onclick="initializeChat()"
|
165 |
</div>
|
166 |
|
167 |
<div id="chat-form" style="display: none;">
|
168 |
-
<h2>2.
|
169 |
-
<p
|
170 |
|
171 |
-
<label for="message"
|
172 |
-
<textarea id="message" rows="4" placeholder="
|
173 |
|
174 |
-
<label for="thinking-rounds"
|
175 |
-
<input type="number" id="thinking-rounds" min="1" max="10" placeholder="
|
176 |
|
177 |
-
<label for="alternatives"
|
178 |
<input type="number" id="alternatives" min="1" max="5" value="3">
|
179 |
|
180 |
-
<button onclick="sendMessage()"
|
181 |
-
<button onclick="resetChat()" style="background-color: #f44336;"
|
182 |
</div>
|
183 |
|
184 |
<div id="response-container" style="display: none;">
|
185 |
-
<h2>3.
|
186 |
-
<div id="response"
|
187 |
<div class="log">
|
188 |
-
<h3
|
189 |
<div id="thinking-log"></div>
|
190 |
</div>
|
191 |
</div>
|
192 |
</div>
|
193 |
|
|
|
|
|
|
|
|
|
|
|
194 |
<script>
|
195 |
let currentSessionId = null;
|
196 |
|
@@ -219,16 +224,16 @@ async def root():
|
|
219 |
document.getElementById('chat-form').style.display = 'block';
|
220 |
document.getElementById('response-container').style.display = 'block';
|
221 |
}} else {{
|
222 |
-
alert('
|
223 |
}}
|
224 |
}} catch (error) {{
|
225 |
-
alert('
|
226 |
}}
|
227 |
}}
|
228 |
|
229 |
async function sendMessage() {{
|
230 |
if (!currentSessionId) {{
|
231 |
-
alert('
|
232 |
return;
|
233 |
}}
|
234 |
|
@@ -237,11 +242,11 @@ async def root():
|
|
237 |
const alternatives = document.getElementById('alternatives').value;
|
238 |
|
239 |
if (!message) {{
|
240 |
-
alert('
|
241 |
return;
|
242 |
}}
|
243 |
|
244 |
-
document.getElementById('response').textContent = '
|
245 |
document.getElementById('thinking-log').textContent = '';
|
246 |
|
247 |
try {{
|
@@ -266,22 +271,21 @@ async def root():
|
|
266 |
// Display thinking history
|
267 |
let thinkingLogHTML = '';
|
268 |
data.thinking_history.forEach(item => {{
|
269 |
-
const selected = item.selected ? ' β
|
270 |
-
|
271 |
-
thinkingLogHTML += "<p><strong>λΌμ΄λ " + item.round + selected + ":</strong> ";
|
272 |
|
273 |
if (item.explanation && item.selected) {{
|
274 |
-
thinkingLogHTML += "<br><em
|
275 |
}}
|
276 |
thinkingLogHTML += "</p>";
|
277 |
}});
|
278 |
|
279 |
document.getElementById('thinking-log').innerHTML = thinkingLogHTML;
|
280 |
}} else {{
|
281 |
-
document.getElementById('response').textContent = '
|
282 |
}}
|
283 |
}} catch (error) {{
|
284 |
-
document.getElementById('response').textContent = '
|
285 |
}}
|
286 |
}}
|
287 |
|
@@ -300,7 +304,7 @@ async def root():
|
|
300 |
"""
|
301 |
return html_content
|
302 |
|
303 |
-
#
|
304 |
@app.get("/health")
|
305 |
async def health_check():
|
306 |
"""Health check endpoint"""
|
@@ -308,14 +312,14 @@ async def health_check():
|
|
308 |
|
309 |
@app.post("/api/initialize", response_model=InitResponse)
|
310 |
async def initialize_chat(config: ChatConfig):
|
311 |
-
"""Initialize a new chat session using environment API key"""
|
312 |
try:
|
313 |
# Generate a session ID
|
314 |
session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
|
315 |
|
316 |
-
#
|
317 |
if not API_KEY:
|
318 |
-
raise HTTPException(status_code=400, detail="OPENROUTE_API
|
319 |
|
320 |
# Initialize the chat instance
|
321 |
chat = EnhancedRecursiveThinkingChat(
|
@@ -336,7 +340,7 @@ async def initialize_chat(config: ChatConfig):
|
|
336 |
|
337 |
@app.post("/api/send_message")
|
338 |
async def send_message(request: MessageRequest):
|
339 |
-
"""Send a message and get a response with thinking process"""
|
340 |
try:
|
341 |
if request.session_id not in chat_instances:
|
342 |
raise HTTPException(status_code=404, detail="Session not found")
|
@@ -387,7 +391,7 @@ async def send_message(request: MessageRequest):
|
|
387 |
|
388 |
@app.post("/api/save")
|
389 |
async def save_conversation(request: SaveRequest):
|
390 |
-
"""Save the conversation or full thinking log"""
|
391 |
try:
|
392 |
if request.session_id not in chat_instances:
|
393 |
raise HTTPException(status_code=404, detail="Session not found")
|
@@ -486,7 +490,7 @@ class ConnectionManager:
|
|
486 |
|
487 |
manager = ConnectionManager()
|
488 |
|
489 |
-
# WebSocket for streaming thinking process
|
490 |
@app.websocket("/ws/{session_id}")
|
491 |
async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
492 |
try:
|
@@ -499,7 +503,7 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
499 |
|
500 |
chat = chat_instances[session_id]["chat"]
|
501 |
|
502 |
-
# Set up a custom callback to stream thinking process
|
503 |
original_call_api = chat._call_api
|
504 |
|
505 |
async def stream_callback(chunk):
|
@@ -592,16 +596,15 @@ async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
|
592 |
except:
|
593 |
pass
|
594 |
finally:
|
595 |
-
# Restore original method if needed
|
596 |
if 'chat' in locals() and 'original_call_api' in locals():
|
597 |
chat._call_api = original_call_api
|
598 |
|
599 |
# Make sure to disconnect
|
600 |
manager.disconnect(session_id)
|
601 |
|
602 |
-
#
|
603 |
if __name__ == "__main__":
|
604 |
-
# νκΉ
νμ΄μ€ μ€νμ΄μ€μμ μ€νμ ν¬νΈ 7860 μ¬μ©
|
605 |
port = 7860
|
606 |
print(f"Starting server on port {port}")
|
607 |
uvicorn.run("app:app", host="0.0.0.0", port=port)
|
|
|
12 |
import logging
|
13 |
import uuid
|
14 |
|
15 |
+
# If the module does not exist, try to import from the current directory
|
16 |
try:
|
17 |
from recursive_thinking_ai import EnhancedRecursiveThinkingChat
|
18 |
except ModuleNotFoundError:
|
19 |
+
# The file recursive_thinking_ai.py must exist in the current directory
|
20 |
import sys
|
21 |
sys.path.append('.')
|
22 |
from recursive_thinking_ai import EnhancedRecursiveThinkingChat
|
|
|
29 |
logger = logging.getLogger(__name__)
|
30 |
|
31 |
app = FastAPI(
|
32 |
+
title="Chain-of-Recursive-Thoughts: TEST",
|
33 |
+
description="https://github.com/PhialsBasement/Chain-of-Recursive-Thoughts",
|
34 |
version="1.0.0"
|
35 |
)
|
36 |
|
|
|
46 |
# Create a dictionary to store chat instances
|
47 |
chat_instances = {}
|
48 |
|
49 |
+
# Retrieve API key from environment variable
|
50 |
API_KEY = os.getenv("OPENROUTE_API")
|
51 |
if not API_KEY:
|
52 |
+
logger.warning("The OPENROUTE_API environment variable is not set. Some features may not work.")
|
53 |
|
54 |
# Pydantic models for request/response validation
|
55 |
class ChatConfig(BaseModel):
|
56 |
+
# Removed api_key field; only model and temperature are received
|
57 |
model: str = "mistralai/mistral-small-3.1-24b-instruct:free"
|
58 |
temperature: Optional[float] = Field(default=0.7, ge=0.0, le=1.0)
|
59 |
|
|
|
82 |
session_id: str
|
83 |
status: str
|
84 |
|
85 |
+
# Simple HTML interface (API key input form removed)
|
86 |
@app.get("/", response_class=HTMLResponse)
|
87 |
async def root():
|
88 |
"""Root endpoint with a simple HTML interface"""
|
|
|
90 |
<!DOCTYPE html>
|
91 |
<html>
|
92 |
<head>
|
93 |
+
<title>Chain-of-Recursive-Thoughts: TEST</title>
|
94 |
<style>
|
95 |
body {{
|
96 |
font-family: Arial, sans-serif;
|
|
|
149 |
</style>
|
150 |
</head>
|
151 |
<body>
|
152 |
+
<h1>Chain-of-Recursive-Thoughts: TEST</h1>
|
153 |
<div class="container">
|
154 |
<div id="init-form">
|
155 |
+
<h2>1. Initialize Chat</h2>
|
156 |
|
157 |
+
<!-- API key input removed -->
|
158 |
+
<label for="model">Model:</label>
|
159 |
<input type="text" id="model" value="mistralai/mistral-small-3.1-24b-instruct:free">
|
160 |
|
161 |
+
<label for="temperature">Temperature:</label>
|
162 |
<input type="number" id="temperature" min="0" max="1" step="0.1" value="0.7">
|
163 |
|
164 |
+
<button onclick="initializeChat()">Initialize</button>
|
165 |
</div>
|
166 |
|
167 |
<div id="chat-form" style="display: none;">
|
168 |
+
<h2>2. Send Message</h2>
|
169 |
+
<p>Session ID: <span id="session-id"></span></p>
|
170 |
|
171 |
+
<label for="message">Message:</label>
|
172 |
+
<textarea id="message" rows="4" placeholder="Enter your message"></textarea>
|
173 |
|
174 |
+
<label for="thinking-rounds">Thinking Rounds (optional):</label>
|
175 |
+
<input type="number" id="thinking-rounds" min="1" max="10" placeholder="Auto">
|
176 |
|
177 |
+
<label for="alternatives">Number of Alternatives (optional):</label>
|
178 |
<input type="number" id="alternatives" min="1" max="5" value="3">
|
179 |
|
180 |
+
<button onclick="sendMessage()">Send</button>
|
181 |
+
<button onclick="resetChat()" style="background-color: #f44336;">Reset</button>
|
182 |
</div>
|
183 |
|
184 |
<div id="response-container" style="display: none;">
|
185 |
+
<h2>3. Response</h2>
|
186 |
+
<div id="response">The response will appear here...</div>
|
187 |
<div class="log">
|
188 |
+
<h3>Thinking Process Log:</h3>
|
189 |
<div id="thinking-log"></div>
|
190 |
</div>
|
191 |
</div>
|
192 |
</div>
|
193 |
|
194 |
+
<div style="margin-top: 30px;">
|
195 |
+
<p>Repo: https://github.com/PhialsBasement/Chain-of-Recursive-Thoughts</p>
|
196 |
+
<p>Community: https://discord.gg/openfreeai</p>
|
197 |
+
</div>
|
198 |
+
|
199 |
<script>
|
200 |
let currentSessionId = null;
|
201 |
|
|
|
224 |
document.getElementById('chat-form').style.display = 'block';
|
225 |
document.getElementById('response-container').style.display = 'block';
|
226 |
}} else {{
|
227 |
+
alert('Initialization failed: ' + (data.detail || 'Unknown error'));
|
228 |
}}
|
229 |
}} catch (error) {{
|
230 |
+
alert('An error occurred: ' + error.message);
|
231 |
}}
|
232 |
}}
|
233 |
|
234 |
async function sendMessage() {{
|
235 |
if (!currentSessionId) {{
|
236 |
+
alert('Please initialize a chat session first.');
|
237 |
return;
|
238 |
}}
|
239 |
|
|
|
242 |
const alternatives = document.getElementById('alternatives').value;
|
243 |
|
244 |
if (!message) {{
|
245 |
+
alert('Please enter a message.');
|
246 |
return;
|
247 |
}}
|
248 |
|
249 |
+
document.getElementById('response').textContent = 'Processing...';
|
250 |
document.getElementById('thinking-log').textContent = '';
|
251 |
|
252 |
try {{
|
|
|
271 |
// Display thinking history
|
272 |
let thinkingLogHTML = '';
|
273 |
data.thinking_history.forEach(item => {{
|
274 |
+
const selected = item.selected ? ' β Selected' : '';
|
275 |
+
thinkingLogHTML += "<p><strong>Round " + item.round + selected + ":</strong> ";
|
|
|
276 |
|
277 |
if (item.explanation && item.selected) {{
|
278 |
+
thinkingLogHTML += "<br><em>Reason for selection: " + item.explanation + "</em>";
|
279 |
}}
|
280 |
thinkingLogHTML += "</p>";
|
281 |
}});
|
282 |
|
283 |
document.getElementById('thinking-log').innerHTML = thinkingLogHTML;
|
284 |
}} else {{
|
285 |
+
document.getElementById('response').textContent = 'Error: ' + (data.detail || 'Unknown error');
|
286 |
}}
|
287 |
}} catch (error) {{
|
288 |
+
document.getElementById('response').textContent = 'An error occurred: ' + error.message;
|
289 |
}}
|
290 |
}}
|
291 |
|
|
|
304 |
"""
|
305 |
return html_content
|
306 |
|
307 |
+
# Health check endpoint
|
308 |
@app.get("/health")
|
309 |
async def health_check():
|
310 |
"""Health check endpoint"""
|
|
|
312 |
|
313 |
@app.post("/api/initialize", response_model=InitResponse)
|
314 |
async def initialize_chat(config: ChatConfig):
|
315 |
+
"""Initialize a new chat session using the environment API key"""
|
316 |
try:
|
317 |
# Generate a session ID
|
318 |
session_id = f"session_{datetime.now().strftime('%Y%m%d%H%M%S')}_{uuid.uuid4().hex[:8]}"
|
319 |
|
320 |
+
# If the environment variable is missing, raise an error (or warning)
|
321 |
if not API_KEY:
|
322 |
+
raise HTTPException(status_code=400, detail="The OPENROUTE_API environment variable is not set.")
|
323 |
|
324 |
# Initialize the chat instance
|
325 |
chat = EnhancedRecursiveThinkingChat(
|
|
|
340 |
|
341 |
@app.post("/api/send_message")
|
342 |
async def send_message(request: MessageRequest):
|
343 |
+
"""Send a message and get a response with the thinking process"""
|
344 |
try:
|
345 |
if request.session_id not in chat_instances:
|
346 |
raise HTTPException(status_code=404, detail="Session not found")
|
|
|
391 |
|
392 |
@app.post("/api/save")
|
393 |
async def save_conversation(request: SaveRequest):
|
394 |
+
"""Save the conversation or the full thinking log"""
|
395 |
try:
|
396 |
if request.session_id not in chat_instances:
|
397 |
raise HTTPException(status_code=404, detail="Session not found")
|
|
|
490 |
|
491 |
manager = ConnectionManager()
|
492 |
|
493 |
+
# WebSocket for streaming the thinking process
|
494 |
@app.websocket("/ws/{session_id}")
|
495 |
async def websocket_endpoint(websocket: WebSocket, session_id: str):
|
496 |
try:
|
|
|
503 |
|
504 |
chat = chat_instances[session_id]["chat"]
|
505 |
|
506 |
+
# Set up a custom callback to stream the thinking process
|
507 |
original_call_api = chat._call_api
|
508 |
|
509 |
async def stream_callback(chunk):
|
|
|
596 |
except:
|
597 |
pass
|
598 |
finally:
|
599 |
+
# Restore the original method if needed
|
600 |
if 'chat' in locals() and 'original_call_api' in locals():
|
601 |
chat._call_api = original_call_api
|
602 |
|
603 |
# Make sure to disconnect
|
604 |
manager.disconnect(session_id)
|
605 |
|
606 |
+
# Use port 7860 for Hugging Face Spaces
|
607 |
if __name__ == "__main__":
|
|
|
608 |
port = 7860
|
609 |
print(f"Starting server on port {port}")
|
610 |
uvicorn.run("app:app", host="0.0.0.0", port=port)
|