hyb
2025-12-30 5e753a15ff53faab2261a53367e44d38caf87041
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
95
96
97
98
99
100
101
Ë
Wñúh*ãó–—dZddlZddlmZmZmZ    ddlmZddl
m Z ddl m Z mZmZddlmZGd„d    e«Zy#e$r    ddl    mZYŒ/wxYw)
z{GenAI LLM integration utilities for MySQL Connector/Python.
 
Provides MyLLM wrapper that issues ML_GENERATE calls via SQL.
éN)ÚAnyÚListÚOptional)ÚLLM)Ú PrivateAttr)Úatomic_transactionÚ execute_sqlÚformat_value_sql)ÚMySQLConnectionAbstractc    óª‡—eZdZUdZe«Zeed<Gd„d«Zdefˆfd„ Z        dde
de e e
d    e d
e
fd „Zed
efd „«Zed
e
fd „«ZˆxZS)ÚMyLLMaÆ
    Custom Large Language Model (LLM) interface for MySQL HeatWave.
 
    This class wraps the generation functionality provided by HeatWave LLMs,
    exposing an interface compatible with common LLM APIs for text generation.
    It provides full support for generative queries and limited support for
    agentic queries.
 
    Attributes:
        _db_connection (MySQLConnectionAbstract):
            Underlying MySQL connector database connection.
    Ú_db_connectioncó—eZdZdZdZy)ú MyLLM.ConfigaL
        Pydantic config for the model.
 
        By default, LangChain (through Pydantic BaseModel) does not allow
        setting or storing undeclared attributes such as _db_connection.
        Setting extra = "allow" makes it possible to store extra attributes
        on the class instance, which is required for MyLLM.
        ÚallowN)Ú__name__Ú
__module__Ú __qualname__Ú__doc__Úextra©óúLH:\Change_password\venv_build\Lib\site-packages\mysql/ai/genai/generation.pyÚConfigr@s„ñ     ð‰rrÚ db_connectioncó0•—t‰|«||_y)aS
        Initialize the MyLLM instance with an active MySQL database connection.
 
        Args:
            db_connection: A MySQL connection object used to run LLM queries.
 
        Notes:
            The db_connection is stored as a private attribute via object.__setattr__,
            which is compatible with Pydantic models.
        N)ÚsuperÚ__init__r)ÚselfrÚ    __class__s  €rrzMyLLM.__init__Lsø€ô    ‰ÑÔà+ˆÕrÚpromptÚstopÚkwargsÚreturnc ó(—|j«}|||d<t|«\}}t|j«5}d|›d}t    |||g|¢­¬«t j |j«d«d}    ddd«|    S#1swY    SxYw)ac
        Generate a text completion from the LLM for a given input prompt.
 
        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwgenai-ml-generate.html
                A full list of supported options (specified by kwargs) can be found under "options"
 
        Args:
            prompt: The input prompt string for the language model.
            stop: Optional list of stop strings to support agentic and chain-of-thought
                  reasoning workflows.
            **kwargs: Additional keyword arguments providing generation options to
                      the LLM (these are serialized to JSON and passed to the HeatWave syscall).
 
        Returns:
            The generated model output as a string.
            (The actual completion does NOT include the input prompt.)
 
        Raises:
            DatabaseError:
                If provided options are invalid or unsupported.
                If a database connection issue occurs.
                If an operational error occurs during execution.
 
        Implementation Notes:
            - Serializes kwargs into a SQL-compatible JSON string.
            - Calls the LLM stored procedure using a database cursor context.
            - Uses `sys.ML_GENERATE` on the server to produce the model output.
            - Expects the server response to be a JSON object with a 'text' key.
        NÚstop_sequenceszSELECT sys.ML_GENERATE("%s", z);)ÚparamsrÚtext)Úcopyr
rrr    ÚjsonÚloadsÚfetchone)
rr!r"r#ÚoptionsÚoptions_placeholderÚoptions_paramsÚcursorÚgenerate_queryÚ llm_responses
          rÚ_callz MyLLM._call[s¨€ðH—+‘+“-ˆØ Ð Ø(,ˆGÐ$Ñ %ä.>¸wÓ.GÑ+И^Ü  × 3Ñ 3Ó 4ð    D¸à!>Ð?RÐ>SÐSUÐXˆNÜ ˜ ¸Ð7PÀÑ7PÕ QäŸ:™: f§o¡oÓ&7¸Ñ&:Ó;¸FÑCˆL÷     DðÐ÷    DðÐús »ABÂBcó
—ddiS)zâ
        Return a dictionary of params that uniquely identify this LLM instance.
 
        Returns:
            dict: Dictionary of identifier parameters (should include
                model_name for tracing/caching).
        Ú
model_nameÚmysql_heatwave_llmr©rs rÚ_identifying_paramszMyLLM._identifying_paramss€ð Ð.ð
ð    
rcó—y)z¢
        Get the type name of this LLM implementation.
 
        Returns:
            A string identifying the LLM provider (used for logging or metrics).
        r6rr7s rÚ    _llm_typezMyLLM._llm_typešs€ð$r)N)rrrrrrr Ú__annotations__rrÚstrrrrr3ÚpropertyÚdictr8r:Ú __classcell__)r s@rr r 0sžø…ñ ñ/:«m€NÐ+Ó;÷
ñ
ð ,Ð&=õ ,ð$%)ñ0àð0ðt˜C‘yÑ!ð0ðð    0ð
 
ó 0ðdð
 
 Tò
 
óð
 
ðð$˜3ò$óô$rr )rr*ÚtypingrrrÚ#langchain_core.language_models.llmsrÚ ImportErrorÚlangchain.llms.baseÚpydanticrÚmysql.ai.utilsrr    r
Úmysql.connector.abstractsr r rrrú<module>rGsMðñ:ó
 ç&Ñ&ð(Ý7õ!çLÑLÝ=ôr$ˆCõr$øðò(ß'ð(ús’:º AÁA