Skip to content

Commit 92b2368

Browse files
committed
Move to agent creator
1 parent a5499eb commit 92b2368

12 files changed

+174
-103
lines changed

text_2_sql/autogen/agentic_text_2_sql.ipynb

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 8,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"cell_type": "code",
16-
"execution_count": null,
16+
"execution_count": 9,
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
@@ -22,18 +22,95 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": null,
25+
"execution_count": 10,
2626
"metadata": {},
27-
"outputs": [],
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"True"
32+
]
33+
},
34+
"execution_count": 10,
35+
"metadata": {},
36+
"output_type": "execute_result"
37+
}
38+
],
2839
"source": [
2940
"dotenv.load_dotenv()"
3041
]
3142
},
3243
{
3344
"cell_type": "code",
34-
"execution_count": null,
45+
"execution_count": 11,
3546
"metadata": {},
36-
"outputs": [],
47+
"outputs": [
48+
{
49+
"name": "stderr",
50+
"output_type": "stream",
51+
"text": [
52+
"INFO:autogen_core:Publishing message of type GroupChatStart to all subscribers: {'message': TextMessage(source='user', models_usage=None, content='What are the total number of sales within 2008?')}\n",
53+
"INFO:autogen_core:Calling message handler for SQL_Query_Generation_Agent with message type GroupChatStart published by Unknown\n",
54+
"INFO:autogen_core:Calling message handler for SQL_Schema_Selection_Agent with message type GroupChatStart published by Unknown\n",
55+
"INFO:autogen_core:Calling message handler for SQL_Query_Correction_Agent with message type GroupChatStart published by Unknown\n",
56+
"INFO:autogen_core:Calling message handler for SQL_Query_Cache_Agent with message type GroupChatStart published by Unknown\n",
57+
"INFO:autogen_core:Calling message handler for Answer_Agent with message type GroupChatStart published by Unknown\n",
58+
"INFO:autogen_core:Calling message handler for Question_Decomposition_Agent with message type GroupChatStart published by Unknown\n",
59+
"INFO:autogen_core:Calling message handler for SQL_Query_Generation_Agent with message type GroupChatStart published by Unknown\n",
60+
"INFO:autogen_core:Calling message handler for SQL_Schema_Selection_Agent with message type GroupChatStart published by Unknown\n",
61+
"INFO:autogen_core:Calling message handler for SQL_Query_Correction_Agent with message type GroupChatStart published by Unknown\n",
62+
"INFO:autogen_core:Calling message handler for SQL_Query_Cache_Agent with message type GroupChatStart published by Unknown\n",
63+
"INFO:autogen_core:Calling message handler for Answer_Agent with message type GroupChatStart published by Unknown\n",
64+
"INFO:autogen_core:Calling message handler for Question_Decomposition_Agent with message type GroupChatStart published by Unknown\n",
65+
"INFO:autogen_core:Calling message handler for group_chat_manager with message type GroupChatStart published by Unknown\n",
66+
"INFO:autogen_core:Publishing message of type GroupChatStart to all subscribers: {'message': TextMessage(source='user', models_usage=None, content='What are the total number of sales within 2008?')}\n",
67+
"INFO:autogen_core:Calling message handler for collect_output_messages with message type GroupChatStart published by group_chat_manager/3b4d5ab6-7cfa-4179-8079-28bf0d1e0db4\n",
68+
"INFO:autogen_agentchat.events:source='user' models_usage=None content='What are the total number of sales within 2008?'\n",
69+
"INFO:httpx:HTTP Request: POST https://aoai-text2sql-adi.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-08-01-preview \"HTTP/1.1 200 OK\"\n",
70+
"INFO:autogen_core.events:{\"prompt_tokens\": 1801, \"completion_tokens\": 6, \"type\": \"LLMCall\"}\n",
71+
"INFO:autogen_core:Publishing message of type GroupChatRequestPublish to all subscribers: {}\n",
72+
"INFO:autogen_core:Calling message handler for SQL_Query_Cache_Agent with message type GroupChatRequestPublish published by group_chat_manager/3b4d5ab6-7cfa-4179-8079-28bf0d1e0db4\n",
73+
"INFO:httpx:HTTP Request: POST https://aoai-text2sql-adi.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-08-01-preview \"HTTP/1.1 200 OK\"\n",
74+
"INFO:autogen_core.events:{\"prompt_tokens\": 1582, \"completion_tokens\": 28, \"type\": \"LLMCall\"}\n",
75+
"INFO:autogen_core:Publishing message of type GroupChatMessage to all subscribers: {'message': ToolCallMessage(source='SQL_Query_Cache_Agent', models_usage=RequestUsage(prompt_tokens=1582, completion_tokens=28), content=[FunctionCall(id='call_CSvANg2842Hg57erUpUGssoe', arguments='{\"question\":\"What is the total number of sales orders in 2008?\"}', name='fetch_queries_from_cache')])}\n",
76+
"INFO:autogen_core:Calling message handler for collect_output_messages with message type GroupChatMessage published by SQL_Query_Cache_Agent/3b4d5ab6-7cfa-4179-8079-28bf0d1e0db4\n",
77+
"INFO:autogen_agentchat.events:source='SQL_Query_Cache_Agent' models_usage=RequestUsage(prompt_tokens=1582, completion_tokens=28) content=[FunctionCall(id='call_CSvANg2842Hg57erUpUGssoe', arguments='{\"question\":\"What is the total number of sales orders in 2008?\"}', name='fetch_queries_from_cache')]\n",
78+
"INFO:httpx:HTTP Request: POST https://aoai-text2sql-adi.openai.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2023-03-15-preview \"HTTP/1.1 200 OK\"\n",
79+
"INFO:azure.core.pipeline.policies.http_logging_policy:Request URL: 'https://aisearch-text2sql-adi.search.windows.net/indexes('text-2-sql-query-cache-index')/docs/search.post.search?api-version=REDACTED'\n",
80+
"Request method: 'POST'\n",
81+
"Request headers:\n",
82+
" 'Content-Type': 'application/json'\n",
83+
" 'Content-Length': '34704'\n",
84+
" 'api-key': 'REDACTED'\n",
85+
" 'Accept': 'application/json;odata.metadata=none'\n",
86+
" 'x-ms-client-request-id': '6a968312-a735-11ef-a762-0242ac110002'\n",
87+
" 'User-Agent': 'azsdk-python-search-documents/11.6.0b5 Python/3.12.6 (Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.36)'\n",
88+
"A body is sent with the request\n",
89+
"INFO:azure.core.pipeline.policies.http_logging_policy:Response status: 200\n",
90+
"Response headers:\n",
91+
" 'Transfer-Encoding': 'chunked'\n",
92+
" 'Content-Type': 'application/json; odata.metadata=none; odata.streaming=true; charset=utf-8'\n",
93+
" 'Content-Encoding': 'REDACTED'\n",
94+
" 'Vary': 'REDACTED'\n",
95+
" 'Server': 'Microsoft-IIS/10.0'\n",
96+
" 'Strict-Transport-Security': 'REDACTED'\n",
97+
" 'Preference-Applied': 'REDACTED'\n",
98+
" 'OData-Version': 'REDACTED'\n",
99+
" 'request-id': '6a968312-a735-11ef-a762-0242ac110002'\n",
100+
" 'elapsed-time': 'REDACTED'\n",
101+
" 'Strict-Transport-Security': 'REDACTED'\n",
102+
" 'Date': 'Wed, 20 Nov 2024 11:48:53 GMT'\n",
103+
"INFO:root:Results: []\n",
104+
"INFO:autogen_core:Publishing message of type GroupChatMessage to all subscribers: {'message': ToolCallResultMessage(source='SQL_Query_Cache_Agent', models_usage=None, content=[FunctionExecutionResult(content=\"{'cached_questions': None}\", call_id='call_CSvANg2842Hg57erUpUGssoe')])}\n",
105+
"INFO:autogen_core:Calling message handler for collect_output_messages with message type GroupChatMessage published by SQL_Query_Cache_Agent/3b4d5ab6-7cfa-4179-8079-28bf0d1e0db4\n",
106+
"INFO:autogen_agentchat.events:source='SQL_Query_Cache_Agent' models_usage=None content=[FunctionExecutionResult(content=\"{'cached_questions': None}\", call_id='call_CSvANg2842Hg57erUpUGssoe')]\n",
107+
"INFO:httpx:HTTP Request: POST https://aoai-text2sql-adi.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-08-01-preview \"HTTP/1.1 500 model_error\"\n",
108+
"INFO:openai._base_client:Retrying request to /chat/completions in 0.951392 seconds\n",
109+
"INFO:httpx:HTTP Request: POST https://aoai-text2sql-adi.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2024-08-01-preview \"HTTP/1.1 500 model_error\"\n",
110+
"INFO:openai._base_client:Retrying request to /chat/completions in 1.995870 seconds\n"
111+
]
112+
}
113+
],
37114
"source": [
38115
"result = await SELECTOR.run(task=\"What are the total number of sales within 2008?\")"
39116
]

text_2_sql/autogen/prompts/answer_agent.yaml renamed to text_2_sql/autogen/agents/answer_agent.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
model:
2+
gpt-4o-mini
13
description:
24
An agent that takes the final results from the SQL query and writes the answer to the user's question
35
system_message:

text_2_sql/autogen/prompts/question_decomposition_agent.yaml renamed to text_2_sql/autogen/agents/question_decomposition_agent.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
model:
2+
gpt-4o-mini
13
description:
24
An agent that will decompose the user's question into smaller parts to be used in the SQL queries. Use this agent when the user's question is too complex to be answered in one SQL query. Only use if the user's question is too complex to be answered in one SQL query.
35

text_2_sql/autogen/prompts/sql_query_cache_agent.yaml renamed to text_2_sql/autogen/agents/sql_query_cache_agent.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
model:
2+
gpt-4o-mini
13
description:
24
An agent that can take a user's question and use a pre-built SQL query cache to quickly fetch results for the user. Use this agent before all other agents to quickly fetch results for the user. If the results are not as expected, use the SQL Query Correction Agent to correct the SQL query if the schemas are similar. If no results are found, use the SQL Schema Selection Agent to select the correct schema.
35

@@ -6,3 +8,5 @@ system_message:
68
You are a helpful AI Assistant that specialises in using a pre-built SQL query cache to quickly fetch results for the user.
79

810
Use the tool and access to the pre-built query cache to ask the pass the user's question to the cache and fetch the results.
11+
tools:
12+
- sql_query_cache_tool

text_2_sql/autogen/prompts/sql_query_correction_agent.yaml renamed to text_2_sql/autogen/agents/sql_query_correction_agent.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
model:
2+
gpt-4o-mini
13
description:
24
An agent that will look at the SQL query, SQL query results and correct any mistakes in the SQL query to ensure the correct results are returned. Use this agent AFTER the SQL query has been executed and the results are not as expected.
35
system_message:
@@ -6,3 +8,6 @@ system_message:
68
Review the SQL query provided and correct any errors or issues that you find. Ensure that the corrected query returns the expected results.
79

810
If you are consistently unable to correct the SQL query and cannot use the schemas to answer the question. Say 'I am unable to correct the SQL query. Please ask another question.' and then end your answer with 'TERMINATE'
11+
tools:
12+
- sql_get_entity_schemas_tool
13+
- sql_query_execution_tool

text_2_sql/autogen/prompts/sql_query_generation_agent.yaml renamed to text_2_sql/autogen/agents/sql_query_generation_agent.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
model:
2+
gpt-4o-mini
13
description:
24
An agent that can generate SQL queries once given the schema and the user's question. It will run the SQL query to fetch the results. Use this agent after the SQL Schema Selection Agent has selected the correct schema.
35
system_message:
@@ -18,3 +20,5 @@ system_message:
1820
The complete entity relationship graph shows you all the entities and their relationships. You can use this information to get a better understanding of the schema and the relationships between the entities and request more schema information if needed.
1921
2022
Always run any SQL query you generate to return the results."
23+
tools:
24+
- sql_query_execution_tool

text_2_sql/autogen/prompts/sql_schema_selection_agent.yaml renamed to text_2_sql/autogen/agents/sql_schema_selection_agent.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
model:
2+
gpt-4o-mini
13
description:
24
An agent that can take a user's question and extract the schema of a view or table in the SQL Database by selecting the most relevant entity based on the search term.
35

@@ -6,3 +8,5 @@ system_message:
68
You are a helpful AI Assistant that specialises in selecting relevant SQL schemas to answer a given user's question.
79

810
Use the tools available to you to select the correct schemas that will help. Extract key terms from the user's question and use them to search for the correct schema.
11+
tools:
12+
- sql_get_entity_schemas_tool

text_2_sql/autogen/qna_agents.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

text_2_sql/autogen/selector.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from autogen_agentchat.task import TextMentionTermination
22
from autogen_agentchat.teams import SelectorGroupChat
33
from utils.models import MINI_MODEL
4+
from utils.agent_creator import AgentCreator
45

5-
from sql_tools_and_agents import (
6-
SQL_QUERY_GENERATION_AGENT,
7-
SQL_SCHEMA_SELECTION_AGENT,
8-
SQL_QUERY_CORRECTION_AGENT,
9-
SQL_QUERY_CACHE_AGENT,
10-
)
11-
from qna_agents import ANSWER_AGENT, QUESTION_DECOMPOSITION_AGENT
6+
SQL_QUERY_GENERATION_AGENT = AgentCreator.create("sql_query_generation_agent")
7+
SQL_SCHEMA_SELECTION_AGENT = AgentCreator.create("sql_schema_selection_agent")
8+
SQL_QUERY_CORRECTION_AGENT = AgentCreator.create("sql_query_correction_agent")
9+
SQL_QUERY_CACHE_AGENT = AgentCreator.create("sql_query_cache_agent")
10+
ANSWER_AGENT = AgentCreator.create("answer_agent")
11+
QUESTION_DECOMPOSITION_AGENT = AgentCreator.create("question_decomposition_agent")
1212

1313
termination = TextMentionTermination("TERMINATE")
1414
SELECTOR = SelectorGroupChat(

text_2_sql/autogen/sql_tools_and_agents.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)