feat(regulatory): 为核查流程增加LLM复核记录
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from review_agent.llm import LLMConfigurationError, LLMRequestError, generate_completion
|
||||
|
||||
|
||||
@@ -22,6 +25,16 @@ def review_condition_fields(
|
||||
llm_fields: dict[str, str] = {}
|
||||
status = "skipped"
|
||||
error_message = ""
|
||||
if not _should_call_llm(completion_func):
|
||||
selected_fields, selected_sources = _select_fields(rule_fields, llm_fields)
|
||||
return {
|
||||
"status": status,
|
||||
"error_message": error_message,
|
||||
"rule_fields": _clean_fields(rule_fields),
|
||||
"llm_fields": llm_fields,
|
||||
"selected_fields": selected_fields,
|
||||
"selected_sources": selected_sources,
|
||||
}
|
||||
try:
|
||||
raw = (completion_func or generate_completion)(_condition_messages(text, rule_fields, file_context), temperature=0.0)
|
||||
payload = _parse_json_object(raw)
|
||||
@@ -48,6 +61,13 @@ def review_workflow_payload(
|
||||
payload: dict[str, Any],
|
||||
completion_func: Callable[..., str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
if not _should_call_llm(completion_func):
|
||||
return {
|
||||
"status": "skipped",
|
||||
"stage": stage,
|
||||
"result": {},
|
||||
"error_message": "",
|
||||
}
|
||||
try:
|
||||
raw = (completion_func or generate_completion)(_workflow_messages(stage, payload), temperature=0.0)
|
||||
parsed = _parse_json_object(raw)
|
||||
@@ -122,6 +142,14 @@ def _parse_json_object(raw: str) -> dict[str, Any]:
|
||||
return parsed
|
||||
|
||||
|
||||
def _should_call_llm(completion_func: Callable[..., str] | None) -> bool:
|
||||
if completion_func is not None:
|
||||
return True
|
||||
if os.environ.get("PYTEST_CURRENT_TEST") and not getattr(settings, "REGULATORY_LLM_REVIEW_ALLOW_TEST_CALLS", False):
|
||||
return False
|
||||
return bool(settings.LLM_API_KEY and settings.LLM_MODEL)
|
||||
|
||||
|
||||
def _clean_fields(fields: dict[str, Any]) -> dict[str, str]:
|
||||
clean = {}
|
||||
for label in FIELD_LABELS:
|
||||
|
||||
Reference in New Issue
Block a user