forked from sentient-engineering/agent-q
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
48 lines (35 loc) · 1.25 KB
/
server.py
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
import asyncio
from flask import Flask, jsonify, request
from agentq.__main__ import run_agent_sync
from agentq.core.mcts.browser_mcts import main as run_browser_mcts
app = Flask(__name__)
@app.route("/execute", methods=["GET"])
def execute_command():
goal = request.args.get("goal")
if not goal:
return jsonify({"error": "No command provided"}), 400
# Ensure we have an event loop
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Run the agent asynchronously
result = run_agent_sync(command=goal)
return jsonify({"result": result})
@app.route("/execute_mcts", methods=["GET"])
def run_mcts():
objective = request.args.get("goal")
if not objective:
return jsonify({"error": "No objective provided"}), 400
# Ensure we have an event loop
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Run the MCTS algorithm asynchronously
result = loop.run_until_complete(run_browser_mcts(objective, eval_mode=True))
return result
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, threaded=False)