|
7 | 7 | import logging |
8 | 8 | from agents.custom_agents.sql_query_cache_agent import SqlQueryCacheAgent |
9 | 9 | import json |
| 10 | +import os |
10 | 11 |
|
11 | | -SQL_QUERY_GENERATION_AGENT = LLMAgentCreator.create( |
12 | | - "sql_query_generation_agent", |
13 | | - target_engine="Microsoft SQL Server", |
14 | | - engine_specific_rules="Use TOP X to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error.", |
15 | | -) |
16 | | -SQL_SCHEMA_SELECTION_AGENT = LLMAgentCreator.create( |
17 | | - "sql_schema_selection_agent", |
18 | | - use_case="Sales data for a company that specializes in selling products online.", |
19 | | -) |
20 | | -SQL_QUERY_CORRECTION_AGENT = LLMAgentCreator.create( |
21 | | - "sql_query_correction_agent", |
22 | | - target_engine="Microsoft SQL Server", |
23 | | - engine_specific_rules="Use TOP X to limit the number of rows returned instead of LIMIT X. NEVER USE LIMIT X as it produces a syntax error.", |
24 | | -) |
25 | | -SQL_QUERY_CACHE_AGENT = SqlQueryCacheAgent() |
26 | | -ANSWER_AGENT = LLMAgentCreator.create("answer_agent") |
27 | | -QUESTION_DECOMPOSITION_AGENT = LLMAgentCreator.create("question_decomposition_agent") |
28 | | - |
29 | | - |
30 | | -def text_2_sql_generator_selector_func(messages): |
31 | | - logging.info("Messages: %s", messages) |
32 | | - decision = None # Initialize decision variable |
33 | | - |
34 | | - if len(messages) == 1: |
35 | | - decision = "sql_query_cache_agent" |
36 | | - |
37 | | - elif ( |
38 | | - messages[-1].source == "sql_query_cache_agent" |
39 | | - and messages[-1].content is not None |
40 | | - ): |
41 | | - cache_result = json.loads(messages[-1].content) |
42 | | - if cache_result.get("cached_questions_and_schemas") is not None: |
| 12 | + |
| 13 | +class AgenticText2Sql: |
| 14 | + def __init__(self, target_engine: str, engine_specific_rules: str): |
| 15 | + self.use_query_cache = False |
| 16 | + self.pre_run_query_cache = False |
| 17 | + |
| 18 | + self.target_engine = target_engine |
| 19 | + self.engine_specific_rules = engine_specific_rules |
| 20 | + |
| 21 | + self.set_mode() |
| 22 | + |
| 23 | + def set_mode(self): |
| 24 | + """Set the mode of the plugin based on the environment variables.""" |
| 25 | + self.use_query_cache = ( |
| 26 | + os.environ.get("Text2Sql__UseQueryCache", "False").lower() == "true" |
| 27 | + ) |
| 28 | + |
| 29 | + self.pre_run_query_cache = ( |
| 30 | + os.environ.get("Text2Sql__PreRunQueryCache", "False").lower() == "true" |
| 31 | + ) |
| 32 | + |
| 33 | + @property |
| 34 | + def agents(self): |
| 35 | + """Define the agents for the chat.""" |
| 36 | + SQL_QUERY_GENERATION_AGENT = LLMAgentCreator.create( |
| 37 | + "sql_query_generation_agent", |
| 38 | + target_engine=self.target_engine, |
| 39 | + engine_specific_rules=self.engine_specific_rules, |
| 40 | + ) |
| 41 | + SQL_SCHEMA_SELECTION_AGENT = LLMAgentCreator.create( |
| 42 | + "sql_schema_selection_agent", |
| 43 | + use_case="Sales data for a company that specializes in selling products online.", |
| 44 | + ) |
| 45 | + SQL_QUERY_CORRECTION_AGENT = LLMAgentCreator.create( |
| 46 | + "sql_query_correction_agent", |
| 47 | + target_engine=self.target_engine, |
| 48 | + engine_specific_rules=self.engine_specific_rules, |
| 49 | + ) |
| 50 | + |
| 51 | + ANSWER_AGENT = LLMAgentCreator.create("answer_agent") |
| 52 | + QUESTION_DECOMPOSITION_AGENT = LLMAgentCreator.create( |
| 53 | + "question_decomposition_agent" |
| 54 | + ) |
| 55 | + |
| 56 | + agents = [ |
| 57 | + SQL_QUERY_GENERATION_AGENT, |
| 58 | + SQL_SCHEMA_SELECTION_AGENT, |
| 59 | + SQL_QUERY_CORRECTION_AGENT, |
| 60 | + ANSWER_AGENT, |
| 61 | + QUESTION_DECOMPOSITION_AGENT, |
| 62 | + ] |
| 63 | + |
| 64 | + if self.use_query_cache: |
| 65 | + SQL_QUERY_CACHE_AGENT = SqlQueryCacheAgent() |
| 66 | + agents.append(SQL_QUERY_CACHE_AGENT) |
| 67 | + |
| 68 | + return agents |
| 69 | + |
| 70 | + @property |
| 71 | + def termination_condition(self): |
| 72 | + """Define the termination condition for the chat.""" |
| 73 | + termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10) |
| 74 | + return termination |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def selector(messages): |
| 78 | + logging.info("Messages: %s", messages) |
| 79 | + decision = None # Initialize decision variable |
| 80 | + |
| 81 | + if len(messages) == 1: |
| 82 | + decision = "sql_query_cache_agent" |
| 83 | + |
| 84 | + elif ( |
| 85 | + messages[-1].source == "sql_query_cache_agent" |
| 86 | + and messages[-1].content is not None |
| 87 | + ): |
| 88 | + cache_result = json.loads(messages[-1].content) |
| 89 | + if cache_result.get("cached_questions_and_schemas") is not None: |
| 90 | + decision = "sql_query_correction_agent" |
| 91 | + else: |
| 92 | + decision = "sql_schema_selection_agent" |
| 93 | + |
| 94 | + elif messages[-1].source == "sql_query_cache_agent": |
| 95 | + decision = "question_decomposition_agent" |
| 96 | + |
| 97 | + elif messages[-1].source == "question_decomposition_agent": |
| 98 | + decomposition_result = json.loads(messages[-1].content) |
| 99 | + |
| 100 | + if len(decomposition_result["entities"]) == 1: |
| 101 | + decision = "sql_schema_selection_agent" |
| 102 | + else: |
| 103 | + decision = "parallel_sql_flow_agent" |
| 104 | + |
| 105 | + elif messages[-1].source == "sql_schema_selection_agent": |
| 106 | + decision = "sql_query_generation_agent" |
| 107 | + |
| 108 | + elif ( |
| 109 | + messages[-1].source == "sql_query_correction_agent" |
| 110 | + and messages[-1].content == "VALIDATED" |
| 111 | + ): |
| 112 | + decision = "answer_agent" |
| 113 | + |
| 114 | + elif messages[-1].source == "sql_query_correction_agent": |
43 | 115 | decision = "sql_query_correction_agent" |
44 | | - else: |
45 | | - decision = "sql_schema_selection_agent" |
46 | | - |
47 | | - elif messages[-1].source == "question_decomposition_agent": |
48 | | - decomposition_result = json.loads(messages[-1].content) |
49 | | - |
50 | | - if len(decomposition_result["entities"]) == 1: |
51 | | - decision = "sql_schema_selection_agent" |
52 | | - else: |
53 | | - decision = "parallel_sql_flow_agent" |
54 | | - |
55 | | - elif messages[-1].source == "sql_schema_selection_agent": |
56 | | - decision = "sql_query_generation_agent" |
57 | | - |
58 | | - elif ( |
59 | | - messages[-1].source == "sql_query_correction_agent" |
60 | | - and messages[-1].content == "VALIDATED" |
61 | | - ): |
62 | | - decision = "answer_agent" |
63 | | - |
64 | | - elif messages[-1].source == "sql_query_correction_agent": |
65 | | - decision = "sql_query_correction_agent" |
66 | | - |
67 | | - # Log the decision |
68 | | - logging.info("Decision: %s", decision) |
69 | | - |
70 | | - return decision |
71 | | - |
72 | | - |
73 | | -termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10) |
74 | | -text_2_sql_generator = SelectorGroupChat( |
75 | | - [ |
76 | | - SQL_QUERY_GENERATION_AGENT, |
77 | | - SQL_SCHEMA_SELECTION_AGENT, |
78 | | - SQL_QUERY_CORRECTION_AGENT, |
79 | | - SQL_QUERY_CACHE_AGENT, |
80 | | - ANSWER_AGENT, |
81 | | - QUESTION_DECOMPOSITION_AGENT, |
82 | | - ], |
83 | | - allow_repeated_speaker=False, |
84 | | - model_client=MINI_MODEL, |
85 | | - termination_condition=termination, |
86 | | - selector_func=text_2_sql_generator_selector_func, |
87 | | -) |
| 116 | + |
| 117 | + # Log the decision |
| 118 | + logging.info("Decision: %s", decision) |
| 119 | + |
| 120 | + return decision |
| 121 | + |
| 122 | + @property |
| 123 | + def agentic_flow(self): |
| 124 | + """Run the agentic flow for the given question. |
| 125 | +
|
| 126 | + Args: |
| 127 | + ---- |
| 128 | + question (str): The question to run the agentic flow on.""" |
| 129 | + agentic_flow = SelectorGroupChat( |
| 130 | + self.agents, |
| 131 | + allow_repeated_speaker=False, |
| 132 | + model_client=MINI_MODEL, |
| 133 | + termination_condition=self.termination_condition, |
| 134 | + selector_func=AgenticText2Sql.selector, |
| 135 | + ) |
| 136 | + |
| 137 | + return agentic_flow |
0 commit comments