mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 14:08:08 +01:00
prompt engineering, train_api date parsing changes
This commit is contained in:
130
thirdparty/train_api.py
vendored
130
thirdparty/train_api.py
vendored
@@ -15,14 +15,29 @@ import string
|
||||
|
||||
|
||||
def parse_datetime(datetime_str):
|
||||
# Parse YYYY-MM-DDTHH:MM format
|
||||
try:
|
||||
date_part, time_part = datetime_str.split('T')
|
||||
year, month, day = map(int, date_part.split('-'))
|
||||
hour, minute = map(int, time_part.split(':'))
|
||||
return year, month, day, hour, minute
|
||||
except:
|
||||
return None, None, None, None, None
|
||||
# Remove trailing 'Z' if present
|
||||
if datetime_str.endswith("Z"):
|
||||
datetime_str = datetime_str[:-1]
|
||||
|
||||
formats = [
|
||||
"%Y-%m-%dT%H:%M", # e.g. "2025-04-18T09:00"
|
||||
"%Y-%m-%dT%H:%M:%S", # e.g. "2025-04-18T09:00:00"
|
||||
"%Y-%m-%d %H:%M:%S", # e.g. "2025-04-18 09:00:00"
|
||||
]
|
||||
|
||||
for fmt in formats:
|
||||
try:
|
||||
parsed = time.strptime(datetime_str, fmt)
|
||||
return (
|
||||
parsed.tm_year,
|
||||
parsed.tm_mon,
|
||||
parsed.tm_mday,
|
||||
parsed.tm_hour,
|
||||
parsed.tm_min,
|
||||
)
|
||||
except ValueError:
|
||||
continue
|
||||
return None, None, None, None, None
|
||||
|
||||
|
||||
class TrainServer(BaseHTTPRequestHandler):
|
||||
@@ -47,7 +62,7 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
def write_response(self, response):
|
||||
try:
|
||||
# Python 3
|
||||
self.wfile.write(response.encode('utf-8'))
|
||||
self.wfile.write(response.encode("utf-8"))
|
||||
except AttributeError:
|
||||
# Python 1.5.2
|
||||
self.wfile.write(response)
|
||||
@@ -73,7 +88,7 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
|
||||
# Journey takes 1-2 hours
|
||||
duration = 60 + random.randint(0, 60)
|
||||
arr_hour = (adj_hour + (duration // 60))
|
||||
arr_hour = adj_hour + (duration // 60)
|
||||
arr_minute = (adj_minute + (duration % 60)) % 60
|
||||
arr_day = adj_day + (arr_hour // 24)
|
||||
arr_hour = arr_hour % 24
|
||||
@@ -83,10 +98,14 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
"type": "outbound",
|
||||
"departure": origin,
|
||||
"arrival": destination,
|
||||
"departure_time": format_datetime(year, month, adj_day, adj_hour, adj_minute),
|
||||
"arrival_time": format_datetime(year, month, arr_day, arr_hour, arr_minute),
|
||||
"departure_time": format_datetime(
|
||||
year, month, adj_day, adj_hour, adj_minute
|
||||
),
|
||||
"arrival_time": format_datetime(
|
||||
year, month, arr_day, arr_hour, arr_minute
|
||||
),
|
||||
"platform": str(random.randint(1, 8)),
|
||||
"price": round(30 + random.random() * 50, 2)
|
||||
"price": round(30 + random.random() * 50, 2),
|
||||
}
|
||||
journeys.append(journey)
|
||||
|
||||
@@ -102,7 +121,7 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
adj_hour = adj_hour % 24
|
||||
|
||||
duration = 60 + random.randint(0, 60)
|
||||
arr_hour = (adj_hour + (duration // 60))
|
||||
arr_hour = adj_hour + (duration // 60)
|
||||
arr_minute = (adj_minute + (duration % 60)) % 60
|
||||
arr_day = adj_day + (arr_hour // 24)
|
||||
arr_hour = arr_hour % 24
|
||||
@@ -112,10 +131,14 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
"type": "return",
|
||||
"departure": destination,
|
||||
"arrival": origin,
|
||||
"departure_time": format_datetime(year, month, adj_day, adj_hour, adj_minute),
|
||||
"arrival_time": format_datetime(year, month, arr_day, arr_hour, arr_minute),
|
||||
"departure_time": format_datetime(
|
||||
year, month, adj_day, adj_hour, adj_minute
|
||||
),
|
||||
"arrival_time": format_datetime(
|
||||
year, month, arr_day, arr_hour, arr_minute
|
||||
),
|
||||
"platform": str(random.randint(1, 8)),
|
||||
"price": round(30 + random.random() * 50, 2)
|
||||
"price": round(30 + random.random() * 50, 2),
|
||||
}
|
||||
journeys.append(journey)
|
||||
|
||||
@@ -124,49 +147,57 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
parsed_url = urlparse(self.path)
|
||||
|
||||
if parsed_url.path == '/api/journeys':
|
||||
if parsed_url.path == "/api/journeys":
|
||||
try:
|
||||
params = parse_qs(parsed_url.query)
|
||||
origin = params.get('from', [''])[0]
|
||||
destination = params.get('to', [''])[0]
|
||||
outbound_datetime = params.get('outbound_time', [''])[0]
|
||||
return_datetime = params.get('return_time', [''])[0]
|
||||
origin = params.get("from", [""])[0]
|
||||
destination = params.get("to", [""])[0]
|
||||
outbound_datetime = params.get("outbound_time", [""])[0]
|
||||
return_datetime = params.get("return_time", [""])[0]
|
||||
|
||||
if not origin or not destination or not outbound_datetime:
|
||||
self.send_response(400)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
self.write_response(self.format_json({
|
||||
"error": "Required parameters: 'from', 'to', and 'outbound_time'"
|
||||
}))
|
||||
self.write_response(
|
||||
self.format_json(
|
||||
{
|
||||
"error": "Required parameters: 'from', 'to', and 'outbound_time'"
|
||||
}
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
# Parse datetimes
|
||||
out_dt = parse_datetime(outbound_datetime)
|
||||
ret_dt = parse_datetime(return_datetime) if return_datetime else (
|
||||
None, None, None, None, None)
|
||||
ret_dt = (
|
||||
parse_datetime(return_datetime)
|
||||
if return_datetime
|
||||
else (None, None, None, None, None)
|
||||
)
|
||||
|
||||
if out_dt[0] is None:
|
||||
self.send_response(400)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
self.write_response(self.format_json({
|
||||
"error": "Invalid datetime format. Use YYYY-MM-DDTHH:MM"
|
||||
}))
|
||||
self.write_response(
|
||||
self.format_json(
|
||||
{"error": "Invalid datetime format. Use YYYY-MM-DDTHH:MM"}
|
||||
)
|
||||
)
|
||||
return
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
|
||||
journeys = self.generate_journeys(
|
||||
origin, destination, out_dt, ret_dt)
|
||||
journeys = self.generate_journeys(origin, destination, out_dt, ret_dt)
|
||||
response = self.format_json({"journeys": journeys})
|
||||
self.write_response(response)
|
||||
|
||||
except Exception as e:
|
||||
self.send_response(500)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
self.write_response(self.format_json({"error": str(e)}))
|
||||
else:
|
||||
@@ -176,20 +207,23 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
parsed_url = urlparse(self.path)
|
||||
|
||||
if parsed_url.path.startswith('/api/book/'):
|
||||
journey_id = parsed_url.path.split('/')[-1]
|
||||
if parsed_url.path.startswith("/api/book/"):
|
||||
journey_id = parsed_url.path.split("/")[-1]
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.end_headers()
|
||||
|
||||
booking_ref = "BR" + \
|
||||
"".join([random.choice(string.digits) for _ in range(5)])
|
||||
booking_ref = "BR" + "".join(
|
||||
[random.choice(string.digits) for _ in range(5)]
|
||||
)
|
||||
|
||||
response = self.format_json({
|
||||
"booking_reference": booking_ref,
|
||||
"journey_id": journey_id,
|
||||
"status": "confirmed"
|
||||
})
|
||||
response = self.format_json(
|
||||
{
|
||||
"booking_reference": booking_ref,
|
||||
"journey_id": journey_id,
|
||||
"status": "confirmed",
|
||||
}
|
||||
)
|
||||
self.write_response(response)
|
||||
|
||||
else:
|
||||
@@ -198,10 +232,10 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
|
||||
|
||||
def run_server():
|
||||
server = HTTPServer(('', 8080), TrainServer)
|
||||
server = HTTPServer(("", 8080), TrainServer)
|
||||
print("Train booking server starting on port 8080...")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
run_server()
|
||||
|
||||
Reference in New Issue
Block a user