Arrowman123 commited on
Commit
0ec06a8
·
verified ·
1 Parent(s): e55d846

Upload 7 files

Browse files
Files changed (7) hide show
  1. Dockerfile +11 -0
  2. api.py +54 -0
  3. dataset.py +111 -0
  4. environment.py +42 -0
  5. inference.py +102 -0
  6. openenv.yaml +16 -0
  7. test_env.py +17 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . .
6
+
7
+ RUN pip install fastapi uvicorn pydantic
8
+
9
+ EXPOSE 7860
10
+
11
+ CMD ["python", "-m", "uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
api.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from environment import SupportEnv
4
+ from dataset import cases
5
+
6
+ app = FastAPI()
7
+
8
+ env = SupportEnv(cases)
9
+
10
+ # -------- Models --------
11
+ class Action(BaseModel):
12
+ message: str
13
+
14
+ # -------- Parser --------
15
+ def parse_action(message):
16
+ message = message.lower()
17
+
18
+ if "refund" in message:
19
+ return "refund"
20
+ elif "replace" in message:
21
+ return "replace"
22
+ elif "reject" in message:
23
+ return "reject"
24
+ elif "proof" in message:
25
+ return "ask_proof"
26
+ else:
27
+ return "unknown"
28
+
29
+ # -------- Routes --------
30
+ @app.post("/reset")
31
+ def reset():
32
+ obs = env.reset()
33
+
34
+ return {
35
+ "observation": {
36
+ "echoed_message": obs
37
+ },
38
+ "reward": 0.0,
39
+ "done": False
40
+ }
41
+
42
+ @app.post("/step")
43
+ def step(action: Action):
44
+ parsed_action = parse_action(action.message)
45
+
46
+ obs, reward, done = env.step(parsed_action)
47
+
48
+ return {
49
+ "observation": {
50
+ "echoed_message": obs
51
+ },
52
+ "reward": reward,
53
+ "done": done
54
+ }
dataset.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ cases = [
2
+
3
+ # =========================
4
+ # 🟢 1. GENUINE REQUESTS
5
+ # =========================
6
+
7
+ {
8
+ "query": "I received a damaged product, want a refund",
9
+ "days": 2,
10
+ "has_proof": True,
11
+ "correct_action": "refund"
12
+ },
13
+ {
14
+ "query": "The item is broken, can I get a replacement?",
15
+ "days": 1,
16
+ "has_proof": True,
17
+ "correct_action": "replace"
18
+ },
19
+ {
20
+ "query": "Wrong item delivered, I ordered something else",
21
+ "days": 3,
22
+ "has_proof": True,
23
+ "correct_action": "replace"
24
+ },
25
+ {
26
+ "query": "Product stopped working within 5 days",
27
+ "days": 5,
28
+ "has_proof": True,
29
+ "correct_action": "refund"
30
+ },
31
+ {
32
+ "query": "I want to return the product, it is defective",
33
+ "days": 6,
34
+ "has_proof": True,
35
+ "correct_action": "refund"
36
+ },
37
+
38
+ # =========================
39
+ # 🔴 2. CUSTOMER LYING
40
+ # =========================
41
+
42
+ {
43
+ "query": "I never received my order but tracking says delivered",
44
+ "days": 4,
45
+ "has_proof": False,
46
+ "delivery_status": "delivered",
47
+ "correct_action": "reject"
48
+ },
49
+ {
50
+ "query": "Product is damaged (no images attached)",
51
+ "days": 3,
52
+ "has_proof": False,
53
+ "correct_action": "ask_proof"
54
+ },
55
+ {
56
+ "query": "I want refund but I already used the product",
57
+ "days": 5,
58
+ "has_proof": False,
59
+ "used_product": True,
60
+ "correct_action": "reject"
61
+ },
62
+ {
63
+ "query": "Claiming wrong item but order history matches delivery",
64
+ "days": 2,
65
+ "has_proof": False,
66
+ "correct_action": "reject"
67
+ },
68
+ {
69
+ "query": "Requesting refund after clearly using the product",
70
+ "days": 4,
71
+ "has_proof": False,
72
+ "used_product": True,
73
+ "correct_action": "reject"
74
+ },
75
+
76
+ # =========================
77
+ # 🟡 3. SUSPICIOUS REQUESTS
78
+ # =========================
79
+
80
+ {
81
+ "query": "Product seems damaged but not sure",
82
+ "days": 2,
83
+ "has_proof": False,
84
+ "correct_action": "ask_proof"
85
+ },
86
+ {
87
+ "query": "I think I got a defective item",
88
+ "days": 3,
89
+ "has_proof": False,
90
+ "correct_action": "ask_proof"
91
+ },
92
+ {
93
+ "query": "Item not working properly, can you help?",
94
+ "days": 4,
95
+ "has_proof": False,
96
+ "correct_action": "ask_proof"
97
+ },
98
+ {
99
+ "query": "Received product but facing issues",
100
+ "days": 5,
101
+ "has_proof": False,
102
+ "correct_action": "ask_proof"
103
+ },
104
+ {
105
+ "query": "The product quality is not good",
106
+ "days": 6,
107
+ "has_proof": False,
108
+ "correct_action": "ask_proof"
109
+ }
110
+
111
+ ]
environment.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ class SupportEnv:
4
+ def __init__(self, cases):
5
+ self.cases = cases
6
+ self.index = 0
7
+
8
+ def reset(self):
9
+ self.index = 0
10
+ return self._get_obs()
11
+
12
+ def step(self, action):
13
+ case = self.cases[self.index]
14
+ correct = case["correct_action"]
15
+
16
+ # reward logic
17
+ if action == correct:
18
+ reward = 1.0
19
+ elif action == "ask_proof" and correct == "ask_proof":
20
+ reward = 0.8
21
+ else:
22
+ reward = -0.5
23
+
24
+ # move to next case
25
+ self.index += 1
26
+ done = self.index >= len(self.cases)
27
+
28
+ obs = self._get_obs() if not done else "All cases completed."
29
+
30
+ return obs, reward, done
31
+
32
+ def _get_obs(self):
33
+ case = self.cases[self.index]
34
+
35
+ return f"""
36
+ Customer Query: {case['query']}
37
+ Days since delivery: {case['days']}
38
+ Proof provided: {case.get('has_proof', False)}
39
+
40
+ Choose one action:
41
+ refund / replace / reject / ask_proof
42
+ """
inference.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from openai import OpenAI
3
+
4
+ API_BASE_URL = "https://api.openai.com/v1"
5
+ API_KEY = "your_api_key_here"
6
+ MODEL_NAME = "gpt-4o-mini"
7
+
8
+ IMAGE_NAME = "support-env"
9
+ TASK_NAME = "customer-support"
10
+ BENCHMARK = "openenv"
11
+
12
+ MAX_STEPS = 20
13
+ MAX_TOTAL_REWARD = 20
14
+ SUCCESS_SCORE_THRESHOLD = 0.6
15
+
16
+
17
+ def get_model_message(client, step, state, last_reward, history):
18
+ prompt = f"""
19
+ You are a customer support agent.
20
+
21
+ State:
22
+ {state}
23
+
24
+ Choose one action:
25
+ refund / replace / reject / ask_proof
26
+
27
+ Respond with your decision.
28
+ """
29
+ response = client.chat.completions.create(
30
+ model=MODEL_NAME,
31
+ messages=[{"role": "user", "content": prompt}]
32
+ )
33
+
34
+ return response.choices[0].message.content
35
+
36
+
37
+ def log_start(**kwargs):
38
+ print("[START]", kwargs, flush=True)
39
+
40
+
41
+ def log_step(**kwargs):
42
+ print("[STEP]", kwargs, flush=True)
43
+
44
+
45
+ def log_end(**kwargs):
46
+ print("[END]", kwargs, flush=True)
47
+
48
+
49
+ async def main():
50
+ client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
51
+
52
+ from openenv import Env
53
+ env = await Env.from_docker_image(IMAGE_NAME)
54
+
55
+ history = []
56
+ rewards = []
57
+ steps_taken = 0
58
+ score = 0.0
59
+ success = False
60
+
61
+ log_start(task=TASK_NAME, env=BENCHMARK, model=MODEL_NAME)
62
+
63
+ try:
64
+ result = await env.reset()
65
+ last_state = result.observation.echoed_message
66
+ last_reward = 0.0
67
+
68
+ for step in range(1, MAX_STEPS + 1):
69
+ if result.done:
70
+ break
71
+
72
+ message = get_model_message(client, step, last_state, last_reward, history)
73
+
74
+ result = await env.step({"message": message})
75
+
76
+ reward = result.reward or 0.0
77
+ done = result.done
78
+
79
+ rewards.append(reward)
80
+ steps_taken = step
81
+ last_state = result.observation.echoed_message
82
+ last_reward = reward
83
+
84
+ log_step(step=step, action=message, reward=reward, done=done, error=None)
85
+
86
+ history.append(f"{message} -> {reward}")
87
+
88
+ if done:
89
+ break
90
+
91
+ score = sum(rewards) / MAX_TOTAL_REWARD
92
+ score = max(0.0, min(1.0, score))
93
+ success = score >= SUCCESS_SCORE_THRESHOLD
94
+
95
+ finally:
96
+ await env.close()
97
+ log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
98
+
99
+
100
+ if __name__ == "__main__":
101
+ asyncio.run(main())
102
+
openenv.yaml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: customer-support-env
2
+ description: Simulated e-commerce customer support environment for evaluating AI agents on handling refund, replacement, and fraud detection scenarios.
3
+
4
+ version: 1.0
5
+
6
+ entrypoint:
7
+ type: http
8
+ url: http://localhost:7860
9
+
10
+ tasks:
11
+ - name: easy
12
+ description: Handle straightforward genuine customer requests
13
+ - name: medium
14
+ description: Handle policy-based decisions and edge cases
15
+ - name: hard
16
+ description: Handle suspicious or ambiguous requests requiring clarification
test_env.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from environment import SupportEnv
2
+ from dataset import cases # your dataset file
3
+
4
+ env = SupportEnv(cases)
5
+
6
+ obs = env.reset()
7
+ done = False
8
+
9
+ while not done:
10
+ print(obs)
11
+
12
+ action = input("Enter action: ") # simulate agent
13
+
14
+ obs, reward, done = env.step(action)
15
+
16
+ print(f"Reward: {reward}")
17
+ print("-" * 40)