Score Updation
Browse files- inference.py +13 -11
inference.py
CHANGED
|
@@ -2,11 +2,11 @@ import asyncio
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
-
from openai import OpenAI
|
| 6 |
|
| 7 |
# REQUIRED VARIABLES
|
| 8 |
-
API_BASE_URL = os.environ["API_BASE_URL"]
|
| 9 |
-
API_KEY = os.environ["API_KEY"]
|
| 10 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 11 |
|
| 12 |
SPACE_URL = "https://arrowman123-customer-supp-env.hf.space"
|
|
@@ -19,7 +19,7 @@ MAX_TOTAL_REWARD = 20
|
|
| 19 |
SUCCESS_SCORE_THRESHOLD = 0.6
|
| 20 |
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def get_model_message(client, state):
|
| 24 |
prompt = f"""
|
| 25 |
You are a customer support agent.
|
|
@@ -39,10 +39,10 @@ refund / replace / reject / ask_proof
|
|
| 39 |
return response.choices[0].message.content.strip().lower()
|
| 40 |
except Exception as e:
|
| 41 |
print(f"[ERROR] LLM call failed: {e}")
|
| 42 |
-
return "reject"
|
| 43 |
|
| 44 |
|
| 45 |
-
#
|
| 46 |
def safe_post(url, json=None, retries=3):
|
| 47 |
for i in range(retries):
|
| 48 |
try:
|
|
@@ -53,7 +53,7 @@ def safe_post(url, json=None, retries=3):
|
|
| 53 |
raise Exception("Failed after retries")
|
| 54 |
|
| 55 |
|
| 56 |
-
#
|
| 57 |
def log_start(**kwargs):
|
| 58 |
print("[START]", kwargs, flush=True)
|
| 59 |
|
|
@@ -64,13 +64,12 @@ def log_end(**kwargs):
|
|
| 64 |
print("[END]", kwargs, flush=True)
|
| 65 |
|
| 66 |
|
| 67 |
-
#
|
| 68 |
async def main():
|
| 69 |
rewards = []
|
| 70 |
steps_taken = 0
|
| 71 |
success = False
|
| 72 |
|
| 73 |
-
# Initialize client using THEIR proxy
|
| 74 |
client = OpenAI(
|
| 75 |
base_url=API_BASE_URL,
|
| 76 |
api_key=API_KEY
|
|
@@ -89,7 +88,6 @@ async def main():
|
|
| 89 |
if done:
|
| 90 |
break
|
| 91 |
|
| 92 |
-
# USE LLM
|
| 93 |
message = get_model_message(client, state)
|
| 94 |
|
| 95 |
res = safe_post(
|
|
@@ -114,7 +112,10 @@ async def main():
|
|
| 114 |
)
|
| 115 |
|
| 116 |
score = sum(rewards) / MAX_TOTAL_REWARD if MAX_TOTAL_REWARD > 0 else 0.0
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
| 118 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 119 |
|
| 120 |
except Exception as e:
|
|
@@ -129,5 +130,6 @@ async def main():
|
|
| 129 |
)
|
| 130 |
|
| 131 |
|
|
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
asyncio.run(main())
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
+
from openai import OpenAI
|
| 6 |
|
| 7 |
# REQUIRED VARIABLES
|
| 8 |
+
API_BASE_URL = os.environ["API_BASE_URL"]
|
| 9 |
+
API_KEY = os.environ["API_KEY"]
|
| 10 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 11 |
|
| 12 |
SPACE_URL = "https://arrowman123-customer-supp-env.hf.space"
|
|
|
|
| 19 |
SUCCESS_SCORE_THRESHOLD = 0.6
|
| 20 |
|
| 21 |
|
| 22 |
+
# LLM AGENT
|
| 23 |
def get_model_message(client, state):
|
| 24 |
prompt = f"""
|
| 25 |
You are a customer support agent.
|
|
|
|
| 39 |
return response.choices[0].message.content.strip().lower()
|
| 40 |
except Exception as e:
|
| 41 |
print(f"[ERROR] LLM call failed: {e}")
|
| 42 |
+
return "reject"
|
| 43 |
|
| 44 |
|
| 45 |
+
# SAFE REQUEST
|
| 46 |
def safe_post(url, json=None, retries=3):
|
| 47 |
for i in range(retries):
|
| 48 |
try:
|
|
|
|
| 53 |
raise Exception("Failed after retries")
|
| 54 |
|
| 55 |
|
| 56 |
+
# LOGGING
|
| 57 |
def log_start(**kwargs):
|
| 58 |
print("[START]", kwargs, flush=True)
|
| 59 |
|
|
|
|
| 64 |
print("[END]", kwargs, flush=True)
|
| 65 |
|
| 66 |
|
| 67 |
+
# MAIN
|
| 68 |
async def main():
|
| 69 |
rewards = []
|
| 70 |
steps_taken = 0
|
| 71 |
success = False
|
| 72 |
|
|
|
|
| 73 |
client = OpenAI(
|
| 74 |
base_url=API_BASE_URL,
|
| 75 |
api_key=API_KEY
|
|
|
|
| 88 |
if done:
|
| 89 |
break
|
| 90 |
|
|
|
|
| 91 |
message = get_model_message(client, state)
|
| 92 |
|
| 93 |
res = safe_post(
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
score = sum(rewards) / MAX_TOTAL_REWARD if MAX_TOTAL_REWARD > 0 else 0.0
|
| 115 |
+
|
| 116 |
+
# ensure score strictly between 0 and 1
|
| 117 |
+
score = max(0.01, min(0.99, score))
|
| 118 |
+
|
| 119 |
success = score >= SUCCESS_SCORE_THRESHOLD
|
| 120 |
|
| 121 |
except Exception as e:
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
|
| 133 |
+
# ENTRY
|
| 134 |
if __name__ == "__main__":
|
| 135 |
asyncio.run(main())
|