Find answers from the community

Updated 4 months ago

is there any agentic example based on :1) text 2 sql where query(incoming text) might com

@kapa.ai is there any agentic example based on :
1) text 2 sql where query(incoming text) might come such that "agent need to work on CoT, by checking if incoming text need 1 or multiple tables to get an anwer ?
2) query may have date related details also like "what are bugs raised in last 1 month?"
2) if multiple table are required then fetch data from those tables
3) combine data and rteturn response.
k
A
4 comments
Please stand by @Anuj as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Yes, there are examples and resources available that can help you build an agent based on Text-to-SQL.

The LlamaIndex project provides a way to build a "text-to-SQL assistant" that can interact with a structured database. This assistant can handle complex queries that may require data from multiple tables. However, the specific example of handling date-related queries like "what are bugs raised in the last 1 month?" is not explicitly mentioned in the provided knowledge sources.

Here is an example of setting up a simple text-to-SQL tool using LlamaIndex:

Plain Text
from llama_index.core.query_engine import NLSQLTableQueryEngine
from llama_index.core.tools import QueryEngineTool

sql_query_engine = NLSQLTableQueryEngine(
    sql_database=sql_database,
    tables=["albums", "tracks", "artists"],
    verbose=True,
)
sql_tool = QueryEngineTool.from_defaults(
    query_engine=sql_query_engine,
    name="sql_tool",
    description=(
        "Useful for translating a natural language query into a SQL query"
    ),
)

(Source: Jerryjliu > Llama_index > Blob > Main > Docs > Docs > Examples > Agent > Agent_runner > Building an Agent around a Query Pipeline > Setup Text-to-SQL Query Engine / Tool ())

This example sets up a simple text-to-SQL tool that can translate a natural language query into a SQL query and execute it against a database. The tables parameter in the NLSQLTableQueryEngine can be a list of multiple tables.
For more complex tasks, you can build a more sophisticated agent pipeline. For example, you can build an agent pipeline that only does text-to-SQL, with retry-logic. This agent pipeline can generate a modified query that outputs the right result given the user input and previous conversation history. (Source: LlamaIndex > En > Latest > Examples > Agent > Agent_runner > run this e2e > Setup Simple Retry Agent Pipeline for Text-to-SQL ())

Please note that executing arbitrary SQL queries can be a security risk. It is recommended to take precautions as needed, such as using restricted roles, read-only databases, sandboxing, etc. (Source: Jerryjliu > Llama_index > Blob > Main > Docs > Docs > Examples > Agent > OpenAI Agent + Query Engine Experimental Cookbook > Joint Text-to-SQL and Semantic Search ())
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
thanks let me check these links!
Add a reply
Sign up and join the conversation on Discord