DataBoySu commited on
Commit ·
9670629
1
Parent(s): acfb96b
infernece
Browse files2 unmodified lines345678910111213141516171819202122233 unmodified lines2728293031323334353637383940414243444546474849505152535455565716 unmodified lines74757677787914 unmodified lines94959697989931 unmodified lines1311321331341351361371381391401411421431441451461475 unmodified lines1531541551561571581591605 unmodified lines1661671681691701711721731741751761771781791804 unmodified lines18518618718818919010 unmodified lines2012022032042052062072082092102 unmodified lines2132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802813 unmodified lines2852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153166 unmodified lines3233243253263272 unmodified linesLoops through all 3 tasks to satisfy the Phase 2 Validator."""import asyncioimport osimport jsonimport textwrapimport sysimport refrom typing import List, Optionalfrom openai import OpenAI# Adjust the import based on your openenv server setup# If running locally without docker wrapper for validation, you might need to import your Env directlyfrom server.AML_env_environment import AmlEnvironmentfrom models import AmlActionAPI_BASE_URL = os.getenv("API_BASE_URL") or "http://127.0.0.1:1234/v1"MODEL_NAME = os.getenv("MODEL_NAME", "openai/gpt-oss-20b")HF_TOKEN = os.getenv("HF_TOKEN") or "lm-studio"LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")3 unmodified linesBENCHMARK = "aml_investigator"MAX_STEPS = 25SYSTEM_PROMPT = textwrap.dedent("""You are a Tier 1 AML Compliance Investigator.You must investigate the provided alert by querying the bank's internal APIs.You have a strict API budget. Be efficient.Respond with EXACTLY ONE valid JSON object representing your action. Do not include markdown formatting or explanations.Available Action JSON Schemas:1. {"action": {"action_type": "query_transactions", "account_id": "ACC-XXXX", "limit": 10, "offset": 0}}2. {"action": {"action_type": "search_transactions", "account_id": "ACC-XXXX", "keyword": "invoice"}}3. {"action": {"action_type": "get_kyc_record", "entity_id": "ENT-XXXX"}}4. {"action": {"action_type": "submit_decision", "decision": "FRAUD", "evidence_links": ["ACC-1234"]}} (Use "CLEAR" for False Positives with empty evidence_links).Token-saving style rule:- Think in caveman style (short, simple words).- Never output prose. Output JSON only.Data rule:- get_kyc_record must use ENT-XXXX only, never ACC-XXXX.""").strip()FALLBACK_ACTION_JSON = '{"action": {"action_type": "submit_decision", "decision": "CLEAR", "evidence_links": []}}'def _extract_text_from_chat_completion(completion: object) -> str:choices = getattr(completion, "choices", None) or []16 unmodified linestext_val = item.get("text")if isinstance(text_val, str):chunks.append(text_val)merged = "".join(chunks).strip()if merged:return merged14 unmodified linestext_val = getattr(part, "text", None)if isinstance(text_val, str):chunks.append(text_val)merged = "".join(chunks).strip()if merged:31 unmodified linesreturn textdef _build_recovery_action_from_obs(obs_dict: dict) -> dict:"""Use a non-terminal fallback action when model output is malformed."""alert = str(obs_dict.get("alert_details", "") or "")match = re.search(r"ACC-\d+", alert)if match:return {"action": {"action_type": "query_transactions","account_id": match.group(0),"limit": 10,"offset": 0,}}return {5 unmodified lines}def _ensure_valid_action_json(raw_text: str, obs_dict: dict) -> str:"""Guarantee a valid action JSON string for downstream parsing."""candidate = _coerce_json_object(raw_text)try:payload = json.loads(candidate)5 unmodified linesaction_type = action.get("action_type")if not isinstance(action_type, str):raise ValueError("missing 'action_type' string")return json.dumps(payload, ensure_ascii=True)except Exception as exc:recovery_json = _build_recovery_action_from_obs(obs_dict)print(f"[DEBUG] Non-JSON/invalid model action; using recovery action ({exc})",file=sys.stderr,flush=True,)return json.dumps(recovery_json, ensure_ascii=True)def log_start(task: str, env: str, model: str) -> None:print(f"[START] task={task} env={env} model={model}", flush=True)4 unmodified linesdone_val = str(done).lower()print(f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}", flush=True)def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:rewards_str = ",".join(f"{r:.2f}" for r in rewards)print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}", flush=True)10 unmodified linescompact = compact.replace("\n", " ").strip()print(f"[THOUGHT] step={step} thought={compact}", file=sys.stderr, flush=True)def get_model_message(client: OpenAI, obs_dict: dict, history: List[str]) -> str:history_block = "\n".join(history[-5:]) if history else "No previous steps."user_prompt = f"Observation:\n{json.dumps(obs_dict, indent=2)}\n\nHistory:\n{history_block}\n\nProvide your next JSON action:"try:completion = client.chat.completions.create(model=MODEL_NAME,2 unmodified lines{"role": "user", "content": user_prompt},],temperature=0.0,max_tokens=1000,response_format={"type": "json_object"},)return _ensure_valid_action_json(_extract_text_from_chat_completion(completion), obs_dict)except Exception as chat_exc:# Retry via Responses API for OpenAI-compatible providers that do not# populate chat.completions choices consistently.try:response = client.responses.create(model=MODEL_NAME,instructions=SYSTEM_PROMPT,input=user_prompt,max_output_tokens=1000,)return _ensure_valid_action_json(_extract_text_from_responses_api(response), obs_dict)except Exception as responses_exc:try:completion = client.completions.create(model=MODEL_NAME,prompt=f"{SYSTEM_PROMPT}\n\n{user_prompt}",temperature=0.0,max_tokens=200,)return _ensure_valid_action_json(_extract_text_from_completions_api(completion), obs_dict)except Exception as completions_exc:print(("[DEBUG] Model request failed: "f"chat={chat_exc}; responses={responses_exc}; completions={completions_exc}"),file=sys.stderr,flush=True,)return _ensure_valid_action_json(FALLBACK_ACTION_JSON, obs_dict)async def main() -> None:client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)# Initialize your environment natively for the baseline scriptenv = AmlEnvironment()for task_name in TASKS:history: List[str] = []rewards: List[float] = []steps_taken = 0score = 0.0success = Falsehad_parse_error = Falselog_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)try:obs = env.reset(task=task_name)for step in range(1, MAX_STEPS + 1):if obs.done:breakobs_dict = obs.model_dump()action_str = get_model_message(client, obs_dict, history)# Parse LLM string to Pydantic Modelaction_for_log = action_strtry:clean_str = _coerce_json_object(action_str)action_json = json.loads(clean_str)3 unmodified linesthought_for_log = f"do {action_type} now"log_thought(step=step, thought=thought_for_log)action_obj = AmlAction.model_validate(action_json)error = Noneexcept Exception as e:# Errors are data! If the LLM writes bad JSON, we catch it and force a dummy action# so the environment can return a schema error to the LLM.had_parse_error = Trueerror = f"JSON Parse/Schema Error: {str(e)}"log_thought(step=step, thought="parse fail; use recovery action")recovery_json = _build_recovery_action_from_obs(obs_dict)action_obj = AmlAction.model_validate(recovery_json)action_for_log = json.dumps(recovery_json, ensure_ascii=True)obs = env.step(action_obj)reward = obs.reward or 0.0done = obs.donerewards.append(reward)steps_taken = steplog_step(step=step, action=action_for_log.replace('\n', ''), reward=reward, done=done, error=error)history.append(f"Step {step}: Action: {action_str} -> Result: {obs.last_action_result} | Error: {obs.error_message}")if done:break# Keep score in open interval (0,1) and avoid false positives on parse failures.if had_parse_error or obs.error_message:score = 0.05elif "submit_decision" in (obs.last_action or ""):6 unmodified linesfinally:log_end(success=success, steps=steps_taken, score=score, rewards=rewards)if __name__ == "__main__":asyncio.run(main())2 unmodified lines3456789101112131415161718192021223 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737416 unmodified lines91929394959697989910014 unmodified lines11511611711811912012112212312431 unmodified lines1561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612625 unmodified lines2682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982995 unmodified lines3053063073083093103113123133143154 unmodified lines32032132232332432532610 unmodified lines3373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723732 unmodified lines3763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554563 unmodified lines4604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325336 unmodified lines5405415425435445452 unmodified linesLoops through all 3 tasks to satisfy the Phase 2 Validator."""import asyncioimport jsonimport osimport reimport sysimport textwrapfrom typing import Any, Dict, List, Optional, Tuplefrom openai import OpenAIfrom server.AML_env_environment import AmlEnvironmentfrom models import AmlActionAPI_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"MODEL_NAME = os.getenv("MODEL_NAME", "openai/gpt-oss-20b")HF_TOKEN = os.getenv("HF_TOKEN") or "lm-studio"LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")3 unmodified linesBENCHMARK = "aml_investigator"MAX_STEPS = 25OBS_RESULT_MAX_ITEMS = 8HISTORY_MAX_STEPS = 3HISTORY_MAX_CHARS = 1600TEXT_CLIP_CHARS = 320SYSTEM_PROMPT = textwrap.dedent("""You are a Tier 1 AML Compliance Investigator.You must investigate the provided alert by querying the bank's internal APIs.You have a strict API budget. Be efficient.Respond with EXACTLY ONE valid JSON object representing your action. Do not include markdown formatting or explanations.Available Action JSON Schemas:1. {"action": {"action_type": "query_transactions", "account_id": "ACC-XXXX", "limit": 10, "offset": 0}}2. {"action": {"action_type": "search_transactions", "account_id": "ACC-XXXX", "keyword": "invoice"}}3. {"action": {"action_type": "get_kyc_record", "entity_id": "ENT-XXXX"}}4. {"action": {"action_type": "submit_decision", "decision": "FRAUD", "evidence_links": ["ACC-1234"]}} (Use "CLEAR" for False Positives with empty evidence_links).Required top-level JSON format:{"thought": {"observation": "...","plan": "...","action": "..."},"action": {...}}Thought rules:- Use caveman style: short, simple, low-token wording.- Keep thought informative but brief.- observation = what clue found now.- plan = next investigation goal.- action = exact tool call you will make now.Data rules:- get_kyc_record must use ENT-XXXX only, never ACC-XXXX.- submit_decision only when evidence is enough; else keep investigating.- Use only the alert, the current observation, and the recent history shown here.""").strip()def _extract_text_from_chat_completion(completion: object) -> str:choices = getattr(completion, "choices", None) or []16 unmodified linestext_val = item.get("text")if isinstance(text_val, str):chunks.append(text_val)else:text_val = getattr(item, "text", None)if isinstance(text_val, str):chunks.append(text_val)merged = "".join(chunks).strip()if merged:return merged14 unmodified linestext_val = getattr(part, "text", None)if isinstance(text_val, str):chunks.append(text_val)elif isinstance(part, dict):maybe_text = part.get("text")if isinstance(maybe_text, str):chunks.append(maybe_text)merged = "".join(chunks).strip()if merged:31 unmodified linesreturn textdef _clip_text(value: Any, max_chars: int = TEXT_CLIP_CHARS) -> str:text = str(value).replace("\n", " ").strip()if len(text) <= max_chars:return textreturn text[: max_chars - 3] + "..."def _compact_record(record: Dict[str, Any]) -> Dict[str, Any]:keep_keys = ["txn_id","timestamp","sender_account","receiver_account","amount","memo_text","account_id","owner_entity_id","status","entity_id","name","type","registration_address","directors",]compact: Dict[str, Any] = {}for key in keep_keys:if key not in record:continuevalue = record.get(key)if key == "directors" and isinstance(value, list):compact[key] = value[:4]if len(value) > 4:compact["directors_truncated"] = len(value) - 4continueif isinstance(value, str):compact[key] = _clip_text(value, max_chars=180)else:compact[key] = valuereturn compactdef _compact_action_result(last_action: Optional[str], value: Any) -> Any:if value is None:return Noneif isinstance(value, list):items = []for item in value[:OBS_RESULT_MAX_ITEMS]:if isinstance(item, dict):items.append(_compact_record(item))else:items.append(_clip_text(item))return {"kind": "list","count": len(value),"items": items,"truncated": len(value) > OBS_RESULT_MAX_ITEMS,"source_action": last_action,}if isinstance(value, dict):return _compact_record(value)if isinstance(value, str):return _clip_text(value, max_chars=420)return valuedef _build_model_observation(obs_dict: Dict[str, Any]) -> Dict[str, Any]:return {"alert_details": obs_dict.get("alert_details"),"budget_remaining": obs_dict.get("budget_remaining"),"last_action": obs_dict.get("last_action"),"last_action_result": _compact_action_result(obs_dict.get("last_action"), obs_dict.get("last_action_result")),"error_message": _clip_text(obs_dict.get("error_message")) if obs_dict.get("error_message") else None,"done": obs_dict.get("done"),"reward": obs_dict.get("reward"),}def _render_history(history: List[Dict[str, Any]]) -> str:if not history:return "No previous steps."entries = history[-HISTORY_MAX_STEPS:]lines = [json.dumps(item, ensure_ascii=True, separators=(",", ":")) for item in entries]while lines and len("\n".join(lines)) > HISTORY_MAX_CHARS:lines.pop(0)return "\n".join(lines) if lines else "No previous steps."def _build_recovery_action_from_obs(obs_dict: dict, next_offsets: Dict[str, int]) -> dict:"""Use a non-terminal fallback action when model output is malformed."""alert = str(obs_dict.get("alert_details", "") or "")match = re.search(r"ACC-\d+", alert)if match:account_id = match.group(0)offset = next_offsets.get(account_id, 0)next_offsets[account_id] = offset + 10return {"action": {"action_type": "query_transactions","account_id": account_id,"limit": 10,"offset": offset,}}return {5 unmodified lines}def _normalize_thought(payload: Dict[str, Any]) -> None:action = payload.get("action") if isinstance(payload.get("action"), dict) else {}action_type = action.get("action_type", "unknown")if "thought" not in payload or not isinstance(payload.get("thought"), dict):payload["thought"] = {"observation": "see current clue now.","plan": "find next real link.","action": f"do {action_type} now.",}returnthought = payload["thought"]for key, fallback in (("observation", "see clue now."),("plan", "next check key link."),("action", f"do {action_type} now."),):value = thought.get(key)if not isinstance(value, str) or not value.strip():thought[key] = fallbackelse:thought[key] = _clip_text(value, max_chars=140)def _try_validate_action_json(raw_text: str) -> Optional[str]:"""Return canonical JSON string if valid, else None."""candidate = _coerce_json_object(raw_text)try:payload = json.loads(candidate)5 unmodified linesaction_type = action.get("action_type")if not isinstance(action_type, str):raise ValueError("missing 'action_type' string")_normalize_thought(payload)return json.dumps(payload, ensure_ascii=True)except Exception:return Nonedef log_start(task: str, env: str, model: str) -> None:print(f"[START] task={task} env={env} model={model}", flush=True)4 unmodified linesdone_val = str(done).lower()print(f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}", flush=True)def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:rewards_str = ",".join(f"{r:.2f}" for r in rewards)print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}", flush=True)10 unmodified linescompact = compact.replace("\n", " ").strip()print(f"[THOUGHT] step={step} thought={compact}", file=sys.stderr, flush=True)def get_model_message(client: OpenAI,obs_dict: dict,history: List[Dict[str, Any]],next_offsets: Dict[str, int],) -> Tuple[str, bool]:model_obs = _build_model_observation(obs_dict)history_block = _render_history(history)user_prompt = (f"Observation:\n{json.dumps(model_obs, ensure_ascii=True, indent=2)}\n\n"f"History:\n{history_block}\n\n""Return exactly one JSON object with keys: thought, action.")parse_errors: List[str] = []try:response = client.responses.create(model=MODEL_NAME,instructions=SYSTEM_PROMPT,input=user_prompt,max_output_tokens=700,)raw_text = _extract_text_from_responses_api(response)canonical = _try_validate_action_json(raw_text)if canonical is not None:return canonical, Falseparse_errors.append("responses:invalid_json")except Exception as responses_exc:parse_errors.append(f"responses:{responses_exc}")try:completion = client.chat.completions.create(model=MODEL_NAME,2 unmodified lines{"role": "user", "content": user_prompt},],temperature=0.0,max_tokens=700,)raw_text = _extract_text_from_chat_completion(completion)canonical = _try_validate_action_json(raw_text)if canonical is not None:return canonical, Falseparse_errors.append("chat:invalid_json")except Exception as chat_exc:parse_errors.append(f"chat:{chat_exc}")try:completion = client.completions.create(model=MODEL_NAME,prompt=f"{SYSTEM_PROMPT}\n\n{user_prompt}",temperature=0.0,max_tokens=280,)raw_text = _extract_text_from_completions_api(completion)canonical = _try_validate_action_json(raw_text)if canonical is not None:return canonical, Falseparse_errors.append("completions:invalid_json")except Exception as completions_exc:parse_errors.append(f"completions:{completions_exc}")recovery_json = _build_recovery_action_from_obs(obs_dict, next_offsets)print(("[DEBUG] Non-JSON/invalid model action; using recovery action "f"({'; '.join(parse_errors)})"),file=sys.stderr,flush=True,)recovery_payload = {"thought": {"observation": "model output bad json.","plan": "use safe step. keep investigate.","action": "query alert account next page.",},"action": recovery_json["action"],}return json.dumps(recovery_payload, ensure_ascii=True), Trueasync def main() -> None:client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)env = AmlEnvironment()for task_name in TASKS:history: List[Dict[str, Any]] = []rewards: List[float] = []steps_taken = 0score = 0.0success = Falsehad_parse_error = Falsenext_offsets: Dict[str, int] = {}query_seen_counts: Dict[Tuple[str, int], int] = {}log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)try:obs = env.reset(task=task_name)for step in range(1, MAX_STEPS + 1):if obs.done:breakobs_dict = obs.model_dump()action_str, used_recovery = get_model_message(client, obs_dict, history, next_offsets)if used_recovery:had_parse_error = Trueaction_for_log = action_straction_payload_for_history: Dict[str, Any] = {}try:clean_str = _coerce_json_object(action_str)action_json = json.loads(clean_str)3 unmodified linesthought_for_log = f"do {action_type} now"log_thought(step=step, thought=thought_for_log)action_obj = AmlAction.model_validate(action_json)action_payload_for_history = action_json.get("action", {}) if isinstance(action_json, dict) else {}action_for_log = json.dumps({"action": action_payload_for_history}, ensure_ascii=True)if action_payload_for_history.get("action_type") == "query_transactions":acc = action_payload_for_history.get("account_id")offset = int(action_payload_for_history.get("offset", 0))limit = int(action_payload_for_history.get("limit", 10))if isinstance(acc, str):query_key = (acc, offset)query_seen_counts[query_key] = query_seen_counts.get(query_key, 0) + 1# Hard guardrail: avoid wasting budget on repeated same page.if task_name == "aml_hard" and query_seen_counts[query_key] > 2:new_offset = max(next_offsets.get(acc, offset + max(limit, 1)), offset + max(limit, 1))action_json["action"]["offset"] = new_offsetaction_json["thought"]["plan"] = _clip_text(f"repeat page seen. move to next offset {new_offset}.",max_chars=120,)action_json["thought"]["action"] = _clip_text(f"query_transactions {acc} offset {new_offset}",max_chars=120,)action_for_log = json.dumps(action_json, ensure_ascii=True)action_obj = AmlAction.model_validate(action_json)offset = new_offsetnext_offsets[acc] = max(next_offsets.get(acc, 0), offset + max(limit, 1))error = Noneexcept Exception as e:had_parse_error = Trueerror = f"JSON Parse/Schema Error: {str(e)}"log_thought(step=step, thought="parse fail; use recovery action")recovery_json = _build_recovery_action_from_obs(obs_dict, next_offsets)recovery_payload = {"thought": {"observation": "parse fail now.","plan": "safe step, keep digging.","action": "query alert next page.",},"action": recovery_json["action"],}action_obj = AmlAction.model_validate(recovery_payload)action_payload_for_history = recovery_payload["action"]action_for_log = json.dumps({"action": action_payload_for_history}, ensure_ascii=True)obs = env.step(action_obj)reward = obs.reward or 0.0done = obs.donerewards.append(reward)steps_taken = steplog_step(step=step, action=action_for_log.replace("\n", ""), reward=reward, done=done, error=error)history.append({"step": step,"action": action_payload_for_history,"result": _compact_action_result(obs.last_action, obs.last_action_result),"error": _clip_text(obs.error_message) if obs.error_message else None,"budget_remaining": obs.budget_remaining,})if len(history) > 24:history = history[-24:]if done:breakif had_parse_error or obs.error_message:score = 0.05elif "submit_decision" in (obs.last_action or ""):6 unmodified linesfinally:log_end(success=success, steps=steps_taken, score=score, rewards=rewards)if __name__ == "__main__":asyncio.run(main())
inference.py+300-82