DataBoySu commited on
Commit ·
a4c032a
1
Parent(s): bfed85f
pre thought
Browse filesFiles changed (2)
- inference.py+96-6
- models.py+2-1
15 unmodified lines1617181920212219 unmodified lines42434445464721 unmodified lines697071727374757677787915 unmodified linesfrom models import AmlActionAPI_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1") or "http://127.0.0.1:1234"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")19 unmodified lines""").strip()def log_start(task: str, env: str, model: str) -> None:print(f"[START] task={task} env={env} model={model}", flush=True)21 unmodified linestemperature=0.1,max_tokens=200,)return (completion.choices[0].message.content or "").strip()except Exception as exc:print(f"[DEBUG] Model request failed: {exc}", file=sys.stderr, flush=True)# Fallback to prevent crashreturn '{"action": {"action_type": "submit_decision", "decision": "CLEAR", "evidence_links": []}}'async def main() -> None:client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)15 unmodified lines1617181920212219 unmodified lines4243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011121 unmodified lines13313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816915 unmodified linesfrom 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")19 unmodified lines""").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 []if not choices:raise ValueError("Model response has no choices")first_choice = choices[0]message = getattr(first_choice, "message", None)if message is None:raise ValueError("Model response choice has no message")content = getattr(message, "content", None)if isinstance(content, str) and content.strip():return content.strip()if isinstance(content, list):chunks: List[str] = []for item in content:if isinstance(item, dict):text_val = item.get("text")if isinstance(text_val, str):chunks.append(text_val)merged = "".join(chunks).strip()if merged:return mergedraise ValueError("Model response content is empty")def _extract_text_from_responses_api(response: object) -> str:output_text = getattr(response, "output_text", None)if isinstance(output_text, str) and output_text.strip():return output_text.strip()output = getattr(response, "output", None) or []chunks: List[str] = []for item in output:content = getattr(item, "content", None) or []for part in content:text_val = getattr(part, "text", None)if isinstance(text_val, str):chunks.append(text_val)merged = "".join(chunks).strip()if merged:return mergedraise ValueError("Responses API output is empty")def _extract_text_from_completions_api(completion: object) -> str:choices = getattr(completion, "choices", None) or []if not choices:raise ValueError("Completions API response has no choices")first_choice = choices[0]text_val = getattr(first_choice, "text", None)if isinstance(text_val, str) and text_val.strip():return text_val.strip()raise ValueError("Completions API response text is empty")def log_start(task: str, env: str, model: str) -> None:print(f"[START] task={task} env={env} model={model}", flush=True)21 unmodified linestemperature=0.1,max_tokens=200,)return _extract_text_from_chat_completion(completion)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=200,)return _extract_text_from_responses_api(response)except Exception as responses_exc:try:completion = client.completions.create(model=MODEL_NAME,prompt=f"{SYSTEM_PROMPT}\n\n{user_prompt}",temperature=0.1,max_tokens=200,)return _extract_text_from_completions_api(completion)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 FALLBACK_ACTION_JSONasync def main() -> None:client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
50 unmodified lines5152535450 unmodified linesclass AmlAction(Action):action: Union[QueryTransactions, SearchTransactions, GetKYCRecord, SubmitDecision] = Field(discriminator='action_type')50 unmodified lines515253545550 unmodified linesclass AmlAction(Action):action: Union[QueryTransactions, SearchTransactions, GetKYCRecord, SubmitDecision] = Field(discriminator='action_type')
inference.py+96-6
models.py+2-1