sql_chat_agent
langroid/agent/special/sql/sql_chat_agent.py
Agent that allows interaction with an SQL database using SQLAlchemy library. The agent can execute SQL queries in the database and return the result.
Functionality includes: - adding table and column context - asking a question about a SQL schema
SQLChatAgentConfig
¶
Bases: ChatAgentConfig
addressing_prefix: str = SEND_TO
class-attribute
instance-attribute
¶
Optional, but strongly recommended, context descriptions for tables, columns, and relationships. It should be a dictionary where each key is a table name and its value is another dictionary.
In this inner dictionary: - The 'description' key corresponds to a string description of the table. - The 'columns' key corresponds to another dictionary where each key is a column name and its value is a string description of that column. - The 'relationships' key corresponds to another dictionary where each key is another table name and the value is a description of the relationship to that table.
If multi_schema support is enabled, the tables names in the description should be of the form 'schema_name.table_name'.
For example: { 'table1': { 'description': 'description of table1', 'columns': { 'column1': 'description of column1 in table1', 'column2': 'description of column2 in table1' } }, 'table2': { 'description': 'description of table2', 'columns': { 'column3': 'description of column3 in table2', 'column4': 'description of column4 in table2' } } }
SQLChatAgent(config)
¶
Bases: ChatAgent
Agent for chatting with a SQL database
Raises:
Type | Description |
---|---|
ValueError
|
If database information is not provided in the config. |
Source code in langroid/agent/special/sql/sql_chat_agent.py
handle_message_fallback(msg)
¶
Handle the scenario where current msg is not a tool. Special handling is only needed if the message was from the LLM (as indicated by self.llm_responded).
Source code in langroid/agent/special/sql/sql_chat_agent.py
retry_query(e, query)
¶
Generate an error message for a failed SQL query and return it.
Parameters: e (Exception): The exception raised during the SQL query execution. query (str): The SQL query that failed.
Returns: str: The error message.
Source code in langroid/agent/special/sql/sql_chat_agent.py
run_query(msg)
¶
Handle a RunQueryTool message by executing a SQL query and returning the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
RunQueryTool
|
The tool-message to handle. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The result of executing the SQL query. |
Source code in langroid/agent/special/sql/sql_chat_agent.py
get_table_names(msg)
¶
Handle a GetTableNamesTool message by returning the names of all tables in the database.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The names of all tables in the database. |
Source code in langroid/agent/special/sql/sql_chat_agent.py
get_table_schema(msg)
¶
Handle a GetTableSchemaTool message by returning the schema of all provided tables in the database.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The schema of all provided tables in the database. |
Source code in langroid/agent/special/sql/sql_chat_agent.py
get_column_descriptions(msg)
¶
Handle a GetColumnDescriptionsTool message by returning the descriptions of all provided columns from the database.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The descriptions of all provided columns from the database. |