From aa579769fa6f1847336cdf58f9e48690850a6e03 Mon Sep 17 00:00:00 2001
From: Maximilian Schmidt <maximilian.schmidt@ims.uni-stuttgart.de>
Date: Mon, 29 Apr 2024 10:54:32 +0200
Subject: [PATCH] Add  HTTP API endpoints

---
 api/main.py     | 44 ++++++++++++++++++++++++++++++++++++++++++++
 optimization.py |  3 +--
 2 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 api/main.py

diff --git a/api/main.py b/api/main.py
new file mode 100644
index 0000000..50a4437
--- /dev/null
+++ b/api/main.py
@@ -0,0 +1,44 @@
+from fastapi import FastAPI, Request
+
+# from api.optimization import Backend
+
+DEBUG = True
+
+app = FastAPI(debug=DEBUG, title="Prompt Optimization Backend")
+
+# api = Backend(debug=DEBUG)
+
+def test():
+    pass
+
+
+# @app.get("/test")
+# async def test_long_operation(request: Request):
+#     loop = asyncio.get_event_loop()
+#     result = await loop.run_in_executor(pool, test)
+#     return "ok"
+
+
+# start optimization
+@app.get("/run/{num_iterations}")
+async def run(num_iterations: int) -> str:
+    # api.run_optimization(num_iterations)
+    return "ok"
+
+
+# TODO turn actions into router and allow to set actions dynamically
+# perform optimizer-specific action
+@app.get("/action/evolve/")
+async def evolve(prompt1: str, prompt2: str) -> str:
+    return f"This is the evolved prompt taking prompts {prompt1} and {prompt2} into account."
+
+
+# get current genealogy of prompts
+@app.get("/family_tree/get")
+async def get_family() -> dict:
+    return dict()
+
+
+@app.get("/")
+def main(request: Request) -> str:
+    return "server running"
diff --git a/optimization.py b/optimization.py
index 9f18365..417c12c 100644
--- a/optimization.py
+++ b/optimization.py
@@ -49,10 +49,9 @@ class PromptOptimization:
         self._init()
 
     def _init(self):
-        # use caching for evaluation
+        # family_tree contains the relation of prompts to its parents
         self.family_tree: dict[str, tuple[str, ...] | None] = {}
         # all_prompts contains a list of Prompt objects that took part in the optimization
-        # converting prompts to Prompt object
         self.all_prompts: dict[str, Prompt] = {}
 
     def reset(self):
-- 
GitLab