fix: Sync Flask app using direct httpx
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""BECOMINGONE Flask API."""
|
"""BECOMINGONE Flask API - Sync version."""
|
||||||
|
|
||||||
|
import requests
|
||||||
from flask import Flask, request, jsonify, render_template_string
|
from flask import Flask, request, jsonify, render_template_string
|
||||||
from becomingone.llm_integrator import EmissaryLLM
|
|
||||||
import asyncio
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Initialize models
|
# Ollama endpoints
|
||||||
MASTER = EmissaryLLM(model='llama3.1:8b')
|
MASTER_URL = "http://localhost:11434/api/chat"
|
||||||
EMISSARY = EmissaryLLM(model='deepseek-coder-v2:lite')
|
EMISSARY_URL = "http://localhost:11434/api/chat"
|
||||||
|
|
||||||
HTML = '''<!DOCTYPE html>
|
HTML = '''<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@@ -44,8 +43,8 @@ HTML = '''<!DOCTYPE html>
|
|||||||
body: JSON.stringify({prompt: p})
|
body: JSON.stringify({prompt: p})
|
||||||
});
|
});
|
||||||
const d = await r.json();
|
const d = await r.json();
|
||||||
let h = '<div class="master"><b>🧠 Master</b><br>' + d.master.response + '</div>';
|
let h = '<div class="master"><b>🧠 Master (llama3.1)</b><br>' + d.master.response + '</div>';
|
||||||
h += '<div class="emissary"><b>⚡ Emissary</b><br>' + d.emissary.response + '</div>';
|
h += '<div class="emissary"><b>⚡ Emissary (deepseek-coder)</b><br>' + d.emissary.response + '</div>';
|
||||||
document.getElementById('response').innerHTML = h;
|
document.getElementById('response').innerHTML = h;
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
document.getElementById('response').innerHTML = '<div class="master">Error: ' + e + '</div>';
|
document.getElementById('response').innerHTML = '<div class="master">Error: ' + e + '</div>';
|
||||||
@@ -65,25 +64,34 @@ def health():
|
|||||||
|
|
||||||
@app.route('/api/chat', methods=['POST'])
|
@app.route('/api/chat', methods=['POST'])
|
||||||
def chat():
|
def chat():
|
||||||
data = request.json
|
data = request.get_json(silent=True) or {}
|
||||||
prompt = data.get('prompt', 'Hello')
|
prompt = data.get('prompt', 'Hello')
|
||||||
|
|
||||||
# Run async in sync context
|
|
||||||
loop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(loop)
|
|
||||||
try:
|
try:
|
||||||
m, e = loop.run_until_complete(asyncio.gather(
|
# Master (llama3.1)
|
||||||
MASTER.respond(prompt),
|
master_resp = requests.post(MASTER_URL, json={
|
||||||
EMISSARY.respond(prompt)
|
"model": "llama3.1:8b",
|
||||||
))
|
"messages": [{"role": "user", "content": prompt}],
|
||||||
|
"stream": False
|
||||||
|
}, timeout=60)
|
||||||
|
master_data = master_resp.json()
|
||||||
|
master_text = master_data.get("message", {}).get("content", str(master_data))[:500]
|
||||||
|
|
||||||
|
# Emissary (deepseek-coder)
|
||||||
|
emissary_resp = requests.post(EMISSARY_URL, json={
|
||||||
|
"model": "deepseek-coder-v2:lite",
|
||||||
|
"messages": [{"role": "user", "content": prompt}],
|
||||||
|
"stream": False
|
||||||
|
}, timeout=60)
|
||||||
|
emissary_data = emissary_resp.json()
|
||||||
|
emissary_text = emissary_data.get("message", {}).get("content", str(emissary_data))[:500]
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'master': {'response': m.get('response', '')[:500]},
|
'master': {'response': master_text},
|
||||||
'emissary': {'response': e.get('response', '')[:500]}
|
'emissary': {'response': emissary_text}
|
||||||
})
|
})
|
||||||
except Exception as ex:
|
except Exception as e:
|
||||||
return jsonify({'error': str(ex)})
|
return jsonify({'error': str(e)})
|
||||||
finally:
|
|
||||||
loop.close()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print("Starting BECOMINGONE on http://192.168.1.6:8001")
|
print("Starting BECOMINGONE on http://192.168.1.6:8001")
|
||||||
|
|||||||
Reference in New Issue
Block a user