|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | +from autogen_agentchat.task import TextMentionTermination, MaxMessageTermination |
| 4 | +from autogen_agentchat.teams import SelectorGroupChat |
| 5 | +from utils.models import MINI_MODEL |
| 6 | +from utils.llm_agent_creator import LLMAgentCreator |
| 7 | +import logging |
| 8 | +from custom_agents.sql_query_cache_agent import SqlQueryCacheAgent |
| 9 | +import json |
| 10 | + |
| 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("sql_schema_selection_agent") |
| 17 | +SQL_QUERY_CORRECTION_AGENT = LLMAgentCreator.create( |
| 18 | + "sql_query_correction_agent", |
| 19 | + target_engine="Microsoft SQL Server", |
| 20 | + 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.", |
| 21 | +) |
| 22 | +SQL_QUERY_CACHE_AGENT = SqlQueryCacheAgent() |
| 23 | +ANSWER_AGENT = LLMAgentCreator.create("answer_agent") |
| 24 | +QUESTION_DECOMPOSITION_AGENT = LLMAgentCreator.create("question_decomposition_agent") |
| 25 | + |
| 26 | + |
| 27 | +def text_2_sql_generator_selector_func(messages): |
| 28 | + logging.info("Messages: %s", messages) |
| 29 | + decision = None # Initialize decision variable |
| 30 | + |
| 31 | + if len(messages) == 1: |
| 32 | + decision = "sql_query_cache_agent" |
| 33 | + |
| 34 | + elif ( |
| 35 | + messages[-1].source == "sql_query_cache_agent" |
| 36 | + and messages[-1].content is not None |
| 37 | + ): |
| 38 | + cache_result = json.loads(messages[-1].content) |
| 39 | + if cache_result.get("cached_questions_and_schemas") is not None: |
| 40 | + decision = "sql_query_correction_agent" |
| 41 | + else: |
| 42 | + decision = "sql_schema_selection_agent" |
| 43 | + |
| 44 | + elif messages[-1].source == "question_decomposition_agent": |
| 45 | + decision = "sql_schema_selection_agent" |
| 46 | + |
| 47 | + elif messages[-1].source == "sql_schema_selection_agent": |
| 48 | + decision = "sql_query_generation_agent" |
| 49 | + |
| 50 | + elif ( |
| 51 | + messages[-1].source == "sql_query_correction_agent" |
| 52 | + and messages[-1].content == "VALIDATED" |
| 53 | + ): |
| 54 | + decision = "answer_agent" |
| 55 | + |
| 56 | + elif messages[-1].source == "sql_query_correction_agent": |
| 57 | + decision = "sql_query_correction_agent" |
| 58 | + |
| 59 | + # Log the decision |
| 60 | + logging.info("Decision: %s", decision) |
| 61 | + |
| 62 | + return decision |
| 63 | + |
| 64 | + |
| 65 | +termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10) |
| 66 | +text_2_sql_generator = SelectorGroupChat( |
| 67 | + [ |
| 68 | + SQL_QUERY_GENERATION_AGENT, |
| 69 | + SQL_SCHEMA_SELECTION_AGENT, |
| 70 | + SQL_QUERY_CORRECTION_AGENT, |
| 71 | + SQL_QUERY_CACHE_AGENT, |
| 72 | + ANSWER_AGENT, |
| 73 | + QUESTION_DECOMPOSITION_AGENT, |
| 74 | + ], |
| 75 | + allow_repeated_speaker=False, |
| 76 | + model_client=MINI_MODEL, |
| 77 | + termination_condition=termination, |
| 78 | + selector_func=text_2_sql_generator_selector_func, |
| 79 | +) |
0 commit comments