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