File size: 2,729 Bytes
a006332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Hardcoded identity helpers for Delta Ultra Mini.

This module keeps the immutable public identity of the model in one place and
provides a small interceptor for identity questions. The interceptor is used
before neural generation so the model never has to infer facts about itself.
"""

from __future__ import annotations

import logging
import os
import re
from typing import Any

logging.basicConfig(level=os.getenv("DELTA_LOG_LEVEL", "INFO").upper())
logger = logging.getLogger(__name__)

_IDENTITY: dict[str, str] = {
    "name": "Delta",
    "version": "Ultra Mini",
    "created_by": "Flame Corporation",
    "parameters": "~50 milhões",
    "purpose": "Assistente conversacional inteligente via API",
}

_IDENTITY_PATTERNS: tuple[re.Pattern[str], ...] = tuple(
    re.compile(pattern, flags=re.IGNORECASE)
    for pattern in (
        r"qual.*seu nome",
        r"who are you",
        r"quem.*voc[eê]",
        r"sua vers[aã]o",
        r"your version",
        r"quem.*criou",
        r"who made you",
        r"quantos par[aâ]metros",
        r"how many parameters",
    )
)


def get_identity() -> dict[str, str]:
    """Return Delta's immutable identity.

    Returns:
        A copy of the identity dictionary.
    """

    return dict(_IDENTITY)


def is_identity_question(text: str) -> bool:
    """Check whether a user message asks about Delta's identity.

    Args:
        text: User-provided text.

    Returns:
        True when the text matches one of the hardcoded identity regexes.
    """

    return any(pattern.search(text) for pattern in _IDENTITY_PATTERNS)


def identity_response(text: str) -> str | None:
    """Return a deterministic identity answer when the input asks for it.

    Args:
        text: User-provided text.

    Returns:
        A precise identity answer, or None when the input should go to the
        neural model.
    """

    if not is_identity_question(text):
        return None

    identity: dict[str, Any] = get_identity()
    lowered = text.lower()
    if re.search(r"quantos par[aâ]metros|how many parameters", lowered):
        return f"Eu sou {identity['name']} {identity['version']} e tenho {identity['parameters']} de parâmetros."
    if re.search(r"sua vers[aã]o|your version", lowered):
        return f"Minha versão é {identity['version']}."
    if re.search(r"quem.*criou|who made you", lowered):
        return f"Fui criada pela {identity['created_by']}."
    if re.search(r"qual.*seu nome", lowered):
        return f"Meu nome é {identity['name']}."
    return (
        f"Eu sou {identity['name']} {identity['version']}, criada pela "
        f"{identity['created_by']}. Meu propósito é ser um "
        f"{identity['purpose'].lower()}."
    )