File size: 2,402 Bytes
d416acc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# the main environment file  

from fastapi import FastAPI
from pydantic import BaseModel
from environment.api_triage_env import APITriageEnv

# creating an app and environment
app = FastAPI()
env = APITriageEnv()

# defining a request model for /step endpoint
# for fastapi so that it can understand that we expecting a JSON with an action field that is a text dtype
class ActionRequest(BaseModel):
  action: str


@app.post("/reset")

def reset():
  """ 
  Starting a new API debugging episode 
  """
  
  print("INFO : reset endpoint is called , new debugging session started ")
  state = env.reset()
  return {
    "step" : state.step,
    "max_steps": state.max_steps,
    "incident_summary": state.incident_summary,
    "logs": state.logs,
    "response_code":state.response_code,
    "fix_applied": state.fix_applied,
    "is_resolved" : state.is_resolved
  }


@app.get("/state")

def state():
  """
  HELPs to return the current observation of the episode.
  """
  print("INFO : current state of the Episode as follows ")
  current = env.state()
  return {
    "step" : current.step,
    "max_steps": current.max_steps,
    "incident_summary": current.incident_summary,
    "logs": current.logs,
    "response_code": current.response_code,
    "fix_applied": current.fix_applied,
    "is_resolved" : current.is_resolved
  }


@app.post("/step")

def step(request: ActionRequest):
  """
  the agent sends an action and our environment will preocess it 
  and update the state , returns what happened.
  """
  
  """
  action = what the agent wants to do (text)
  observation = what the agent sees after doing it (object with 7 fields)
  """

  action = request.action
  print(f"INFO : Action received: {action}")
  

  # calling env.step() from api_triage_env.py file to process the action
  observation , reward , done , info = env.step(action)

  # here returning the result 
  return {
    "observation": {
    "step" : observation.step,
    "max_steps": observation.max_steps,
    "incident_summary": observation.incident_summary,
    "logs": observation.logs,
    "response_code": observation.response_code,
    "fix_applied": observation.fix_applied,
    "is_resolved" : observation.is_resolved
    },
    "reward": reward,
    "done": done,
    "info": info,
  }

def main():
    import uvicorn
    uvicorn.run("app:app", host="0.0.0.0", port=7860)

if __name__ == "__main__":
    main()