ARYANPATEL-BIT commited on
Commit
ffdab97
·
2 Parent(s): 856296869eede1

final Changes

Browse files
Files changed (10) hide show
  1. Dockerfile +6 -0
  2. README.md +64 -1
  3. env.py +21 -0
  4. inference.py +6 -0
  5. pyproject.toml +37 -0
  6. requirements-docker.txt +6 -6
  7. server/__init__.py +1 -0
  8. server/app.py +41 -0
  9. test_env.py +1 -1
  10. uv.lock +0 -0
Dockerfile CHANGED
@@ -13,6 +13,8 @@ COPY env.py .
13
  COPY inference.py .
14
  COPY openenv.yaml .
15
  COPY README.md .
 
 
16
 
17
  # Non-root user
18
  RUN useradd --create-home appuser && chown -R appuser:appuser /app
@@ -20,4 +22,8 @@ USER appuser
20
 
21
  EXPOSE 7860
22
 
 
 
 
 
23
  CMD ["uvicorn", "env:app", "--host", "0.0.0.0", "--port", "7860"]
 
13
  COPY inference.py .
14
  COPY openenv.yaml .
15
  COPY README.md .
16
+ COPY test_env.py .
17
+ COPY audit_reward.py .
18
 
19
  # Non-root user
20
  RUN useradd --create-home appuser && chown -R appuser:appuser /app
 
22
 
23
  EXPOSE 7860
24
 
25
+ # Health check for HuggingFace Spaces
26
+ HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
27
+ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/')" || exit 1
28
+
29
  CMD ["uvicorn", "env:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,3 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # ☁️ DevOps/FinOps Cloud Optimizer — OpenEnv Environment
2
 
3
  **An AI agent environment where models learn to optimize cloud infrastructure cost vs. performance under realistic operational constraints.**
@@ -216,7 +232,7 @@ Generates a unique fleet configuration from the seed. Server count (3–7), work
216
 
217
  ```bash
218
  # 1. Clone and set up
219
- git clone <repo-url> && cd <repo-dir>
220
  python -m venv venv && source venv/bin/activate # or .\venv\Scripts\activate on Windows
221
  pip install -r requirements.txt
222
 
@@ -279,10 +295,57 @@ All scores are deterministic: same actions → same score, always.
279
  | `/reset` | POST | `{"task_id": "easy\|medium\|hard\|random", "seed": <int>}` | `{session_id, ...Observation}` |
280
  | `/step` | POST | `Action JSON` + `?session_id=ID` | `{observation, reward, done, info}` |
281
  | `/state` | GET | `?session_id=ID` | Full state with action history |
 
282
  | `/dashboard` | GET | `?session_id=ID` | Live HTML dashboard |
283
 
284
  ---
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  ## Technical Stack
287
 
288
  - **Python 3.11** + **FastAPI** + **Pydantic v2** + **uvicorn**
 
1
+ ---
2
+ title: FinOps Cloud Optimizer Environment
3
+ emoji: ☁️
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ pinned: false
8
+ app_port: 7860
9
+ tags:
10
+ - openenv
11
+ - devops
12
+ - finops
13
+ - reinforcement-learning
14
+ - agents
15
+ ---
16
+
17
  # ☁️ DevOps/FinOps Cloud Optimizer — OpenEnv Environment
18
 
19
  **An AI agent environment where models learn to optimize cloud infrastructure cost vs. performance under realistic operational constraints.**
 
232
 
233
  ```bash
234
  # 1. Clone and set up
235
+ git clone https://github.com/ARYANPATEL-BIT/RLV-2.git && cd RLV-2
236
  python -m venv venv && source venv/bin/activate # or .\venv\Scripts\activate on Windows
237
  pip install -r requirements.txt
238
 
 
295
  | `/reset` | POST | `{"task_id": "easy\|medium\|hard\|random", "seed": <int>}` | `{session_id, ...Observation}` |
296
  | `/step` | POST | `Action JSON` + `?session_id=ID` | `{observation, reward, done, info}` |
297
  | `/state` | GET | `?session_id=ID` | Full state with action history |
298
+ | `/info` | GET | — | Task catalog, instance types, reward formula |
299
  | `/dashboard` | GET | `?session_id=ID` | Live HTML dashboard |
300
 
301
  ---
302
 
303
+ ## Example Episode (Easy Task)
304
+
305
+ A complete episode walkthrough showing reset, one action, and the final state:
306
+
307
+ ```bash
308
+ # 1. Reset to easy task
309
+ curl -X POST http://localhost:7860/reset \
310
+ -H "Content-Type: application/json" \
311
+ -d '{"task_id": "easy"}'
312
+ ```
313
+
314
+ **Response:** An xlarge server ($1.60/hr) running two workloads. Budget is $0.25/hr.
315
+
316
+ ```json
317
+ {
318
+ "session_id": "a1b2c3d4",
319
+ "servers": [{"server_id": "srv-001", "instance_type": "xlarge", "cpu_cores": 16, "ram_gb": 32, "cost_per_hour": 1.6, "assigned_workloads": ["wl-web", "wl-api"]}],
320
+ "workloads": [
321
+ {"workload_id": "wl-web", "name": "Company Website", "required_cpu": 0.5, "required_ram": 1.0, "current_latency_ms": 40.0, "sla_latency_ms": 200.0, "is_critical": false},
322
+ {"workload_id": "wl-api", "name": "API Gateway", "required_cpu": 1.0, "required_ram": 1.5, "current_latency_ms": 30.0, "sla_latency_ms": 150.0, "is_critical": true}
323
+ ],
324
+ "total_cost_per_hour": 1.6, "budget_per_hour": 0.25, "sla_violations": 0,
325
+ "step_number": 0, "max_steps": 5, "done": false
326
+ }
327
+ ```
328
+
329
+ ```bash
330
+ # 2. Resize the server down to small (2 CPU, 4 GB — fits both workloads)
331
+ curl -X POST "http://localhost:7860/step?session_id=a1b2c3d4" \
332
+ -H "Content-Type: application/json" \
333
+ -d '{"action_type": "resize", "server_id": "srv-001", "instance_type": "small"}'
334
+ ```
335
+
336
+ **Response:** Cost drops from $1.60 to $0.20. SLAs still met. Score 0.718.
337
+
338
+ ```json
339
+ {
340
+ "observation": {"total_cost_per_hour": 0.2, "sla_violations": 0, "step_number": 1},
341
+ "reward": {"score": 0.718, "cost_efficiency": 0.92, "performance_score": 1.0, "penalty": 0.0,
342
+ "breakdown": "cost_eff=0.920×0.4 + perf=1.000×0.35 - penalty=0.000×0.25 = 0.7180"},
343
+ "done": false
344
+ }
345
+ ```
346
+
347
+ ---
348
+
349
  ## Technical Stack
350
 
351
  - **Python 3.11** + **FastAPI** + **Pydantic v2** + **uvicorn**
env.py CHANGED
@@ -23,6 +23,7 @@ from typing import Any, Dict, List, Literal, Optional, TypedDict
23
  import uvicorn
24
  from fastapi import FastAPI, HTTPException, Query
25
  from fastapi.responses import HTMLResponse
 
26
  from pydantic import BaseModel, Field
27
 
28
 
@@ -899,6 +900,15 @@ app = FastAPI(
899
  description="AI agent environment for cloud infrastructure cost vs. performance optimization.",
900
  version="2.0.0",
901
  )
 
 
 
 
 
 
 
 
 
902
  sessions = SessionManager()
903
 
904
 
@@ -906,6 +916,17 @@ sessions = SessionManager()
906
  def health_check():
907
  return {"status": "ok", "environment": "devops-finops-cloud-optimizer", "version": "2.0.0"}
908
 
 
 
 
 
 
 
 
 
 
 
 
909
 
910
  @app.post("/reset")
911
  def reset_endpoint(body: ResetRequest = ResetRequest()):
 
23
  import uvicorn
24
  from fastapi import FastAPI, HTTPException, Query
25
  from fastapi.responses import HTMLResponse
26
+ from fastapi.middleware.cors import CORSMiddleware
27
  from pydantic import BaseModel, Field
28
 
29
 
 
900
  description="AI agent environment for cloud infrastructure cost vs. performance optimization.",
901
  version="2.0.0",
902
  )
903
+
904
+ app.add_middleware(
905
+ CORSMiddleware,
906
+ allow_origins=["*"],
907
+ allow_credentials=True,
908
+ allow_methods=["*"],
909
+ allow_headers=["*"],
910
+ )
911
+
912
  sessions = SessionManager()
913
 
914
 
 
916
  def health_check():
917
  return {"status": "ok", "environment": "devops-finops-cloud-optimizer", "version": "2.0.0"}
918
 
919
+ @app.get("/info")
920
+ def info_endpoint():
921
+ return {
922
+ "environment": "devops-finops-cloud-optimizer",
923
+ "version": "2.0.0",
924
+ "tasks": TASK_CONFIGS,
925
+ "instance_catalog": INSTANCE_CATALOG,
926
+ "reward_formula": "score = (cost_efficiency × 0.40) + (performance_score × 0.35) - (penalty × 0.25)",
927
+ "endpoints": ["/reset", "/step", "/state", "/info", "/dashboard"]
928
+ }
929
+
930
 
931
  @app.post("/reset")
932
  def reset_endpoint(body: ResetRequest = ResetRequest()):
inference.py CHANGED
@@ -211,6 +211,7 @@ def run_task(client: OpenAI, task_id: str) -> float:
211
  max_steps = obs.get("max_steps", 5)
212
  final_score = 0.0
213
  reward = {}
 
214
 
215
  print(f" Session: {session_id}")
216
  print(f" Budget: ${obs.get('budget_per_hour', 0):.2f}/hr | "
@@ -224,6 +225,10 @@ def run_task(client: OpenAI, task_id: str) -> float:
224
  break
225
 
226
  user_prompt = build_user_prompt(obs)
 
 
 
 
227
  messages.append({"role": "user", "content": user_prompt})
228
 
229
  # Keep conversation manageable (last 6 exchanges + system)
@@ -249,6 +254,7 @@ def run_task(client: OpenAI, task_id: str) -> float:
249
  done = step_result.get("done", False)
250
  final_score = reward.get("score", 0.0)
251
  info_msg = step_result.get("info", {}).get("action_result", "")
 
252
 
253
  print(f" -> score={final_score:.4f} | {info_msg[:70]}")
254
 
 
211
  max_steps = obs.get("max_steps", 5)
212
  final_score = 0.0
213
  reward = {}
214
+ action_log = []
215
 
216
  print(f" Session: {session_id}")
217
  print(f" Budget: ${obs.get('budget_per_hour', 0):.2f}/hr | "
 
225
  break
226
 
227
  user_prompt = build_user_prompt(obs)
228
+ if action_log:
229
+ user_prompt += f"\n\n## Action History\nYou have taken {len(action_log)} actions so far:\n"
230
+ for past_step, (past_a, past_m) in enumerate(action_log):
231
+ user_prompt += f" Step {past_step+1}: {past_a.get('action_type', '?')} -> {past_m[:70]}\n"
232
  messages.append({"role": "user", "content": user_prompt})
233
 
234
  # Keep conversation manageable (last 6 exchanges + system)
 
254
  done = step_result.get("done", False)
255
  final_score = reward.get("score", 0.0)
256
  info_msg = step_result.get("info", {}).get("action_result", "")
257
+ action_log.append((action, info_msg))
258
 
259
  print(f" -> score={final_score:.4f} | {info_msg[:70]}")
260
 
pyproject.toml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2026. All rights reserved.
2
+
3
+ [build-system]
4
+ requires = ["setuptools>=45", "wheel"]
5
+ build-backend = "setuptools.build_meta"
6
+
7
+ [project]
8
+ name = "openenv-devops-finops"
9
+ version = "2.0.0"
10
+ description = "AI agent environment for cloud infrastructure cost vs performance optimization."
11
+ authors = [
12
+ { name = "aryanpatel" }
13
+ ]
14
+ readme = "README.md"
15
+ requires-python = ">=3.10"
16
+ dependencies = [
17
+ "fastapi>=0.104.0",
18
+ "uvicorn>=0.24.0",
19
+ "pydantic>=2.0.0",
20
+ "openai>=1.0.0",
21
+ "requests>=2.31.0",
22
+ "openenv-core[core]>=0.2.0",
23
+ ]
24
+
25
+ [project.optional-dependencies]
26
+ dev = [
27
+ "pytest>=8.0.0",
28
+ ]
29
+
30
+ [project.scripts]
31
+ # Server entry point - enables running via: uv run --project . server
32
+ server = "devops_finops.server.app:main"
33
+
34
+ [tool.setuptools]
35
+ include-package-data = true
36
+ packages = ["devops_finops", "devops_finops.server"]
37
+ package-dir = { "devops_finops" = ".", "devops_finops.server" = "server" }
requirements-docker.txt CHANGED
@@ -1,8 +1,8 @@
1
  # requirements-docker.txt — Minimal deps for the OpenEnv environment
2
- # Used by Dockerfile only (not the course repo's requirements.txt)
3
 
4
- fastapi>=0.104.0
5
- uvicorn>=0.24.0
6
- pydantic>=2.0.0
7
- openai>=1.0.0
8
- requests>=2.31.0
 
1
  # requirements-docker.txt — Minimal deps for the OpenEnv environment
2
+ # Used by Dockerfile only. Versions pinned for reproducible builds.
3
 
4
+ fastapi==0.115.6
5
+ uvicorn==0.34.0
6
+ pydantic==2.10.4
7
+ openai==1.58.1
8
+ requests==2.32.3
server/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # Server package for DevOps/FinOps OpenEnv
server/app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ FastAPI application entry point for the DevOps/FinOps OpenEnv Environment.
3
+
4
+ This module creates an HTTP server exposing the cloud optimization
5
+ environment over HTTP endpoints.
6
+
7
+ Usage:
8
+ # Development:
9
+ uvicorn server.app:app --reload --host 0.0.0.0 --port 7860
10
+
11
+ # Production:
12
+ uvicorn server.app:app --host 0.0.0.0 --port 7860
13
+
14
+ # Or run directly:
15
+ uv run --project . server
16
+ """
17
+
18
+ import sys
19
+ import os
20
+
21
+ # Ensure the root directory is in the path so we can import env.py
22
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
23
+
24
+ from env import app # noqa: E402 — re-export the FastAPI app
25
+
26
+
27
+ def main():
28
+ """
29
+ Entry point for direct execution via uv run or python -m.
30
+
31
+ This function enables running the server without Docker:
32
+ uv run --project . server
33
+ python -m devops_finops.server.app
34
+ """
35
+ import uvicorn
36
+
37
+ uvicorn.run(app, host="0.0.0.0", port=7860)
38
+
39
+
40
+ if __name__ == "__main__":
41
+ main()
test_env.py CHANGED
@@ -62,7 +62,7 @@ def test_cost_efficiency():
62
  _compute_cost_efficiency(0.25, budget) > _compute_cost_efficiency(budget, budget))
63
  check("Over budget < at budget",
64
  _compute_cost_efficiency(0.80, budget) < _compute_cost_efficiency(budget, budget))
65
- check("At 2x budget → 0.0", _compute_cost_efficiency(1.00, budget) == 0.0)
66
  check("Monotonically decreasing",
67
  _compute_cost_efficiency(0.10, budget) >
68
  _compute_cost_efficiency(0.30, budget) >
 
62
  _compute_cost_efficiency(0.25, budget) > _compute_cost_efficiency(budget, budget))
63
  check("Over budget < at budget",
64
  _compute_cost_efficiency(0.80, budget) < _compute_cost_efficiency(budget, budget))
65
+ check("At 3x budget → 0.0", _compute_cost_efficiency(1.50, budget) == 0.0)
66
  check("Monotonically decreasing",
67
  _compute_cost_efficiency(0.10, budget) >
68
  _compute_cost_efficiency(0.30, budget) >
uv.lock ADDED
The diff for this file is too large to render. See raw diff