Skip to content

client

  • chat_structured validates against any Pydantic model.
  • chat is a convenience wrapper that validates with LLMDiagnosis.
  • Persists every normalized reply under data/llm_raw/<sid>_<UTC>.json.

Classes:

Functions:

LLMResponseValidationError

Bases: ValueError

chat

chat(
    system: str,
    user: str,
    *,
    session_id: str | None = None,
    llm: StructuredLLM | None = None,
    llm_settings: LLMSettings | None = None,
) -> LLMDiagnosis
Source code in src/hiperhealth/agents/client.py
def chat(
    system: str,
    user: str,
    *,
    session_id: str | None = None,
    llm: StructuredLLM | None = None,
    llm_settings: LLMSettings | None = None,
) -> LLMDiagnosis:
    """
    title: Send system / user prompts and return a validated ``LLMDiagnosis``.
    parameters:
      system:
        type: str
        description: Value for system.
      user:
        type: str
        description: Value for user.
      session_id:
        type: str | None
        description: Value for session_id.
      llm:
        type: StructuredLLM | None
        description: Value for llm.
      llm_settings:
        type: LLMSettings | None
        description: Value for llm_settings.
    returns:
      type: LLMDiagnosis
      description: Return value.
    """
    return chat_structured(
        system,
        user,
        LLMDiagnosis,
        session_id=session_id,
        llm=llm,
        llm_settings=llm_settings,
    )

chat_structured

chat_structured(
    system: str,
    user: str,
    output_type: type[TModel],
    *,
    session_id: str | None = None,
    llm: StructuredLLM | None = None,
    llm_settings: LLMSettings | None = None,
) -> TModel
Source code in src/hiperhealth/agents/client.py
def chat_structured(
    system: str,
    user: str,
    output_type: type[TModel],
    *,
    session_id: str | None = None,
    llm: StructuredLLM | None = None,
    llm_settings: LLMSettings | None = None,
) -> TModel:
    """
    title: Send prompts and return a validated Pydantic model of any type.
    parameters:
      system:
        type: str
        description: Value for system.
      user:
        type: str
        description: Value for user.
      output_type:
        type: type[TModel]
        description: Pydantic model class to validate the response against.
      session_id:
        type: str | None
        description: Value for session_id.
      llm:
        type: StructuredLLM | None
        description: Value for llm.
      llm_settings:
        type: LLMSettings | None
        description: Value for llm_settings.
    returns:
      type: TModel
      description: Return value.
    """
    effective_llm = llm or _get_llm(llm_settings)

    try:
        result = _call_llm_structured(effective_llm, system, user, output_type)
    except (ValidationError, TypeError) as exc:
        raise LLMResponseValidationError(
            f'LLM response is not valid {output_type.__name__}: {exc}'
        ) from exc

    effective_settings = llm_settings or load_diagnostics_llm_settings()
    if effective_settings.persist_raw:
        dump_llm_json(result.model_dump_json(), session_id)
    return result

dump_llm_json

dump_llm_json(text: str, sid: str | None) -> None
Source code in src/hiperhealth/agents/client.py
def dump_llm_json(text: str, sid: str | None) -> None:
    """
    title: Save *text* to data/llm_raw/<timestamp>_<sid>.json.
    summary: If *sid* is None, a random 8-char token is used instead.
    parameters:
      text:
        type: str
        description: Value for text.
      sid:
        type: str | None
        description: Value for sid.
    """
    _RAW_DIR.mkdir(parents=True, exist_ok=True)
    ts = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ')
    suffix = sid or uuid.uuid4().hex[:8]
    (_RAW_DIR / f'{ts}_{suffix}.json').write_text(text, encoding='utf-8')