AI Meets SQL: Developing AI Database Agent in 10 Minutes

  Рет қаралды 6,155

Denis Magda

Denis Magda

Күн бұрын

Learn new tech by building a simple AI-powered SQL agent for your favorite SQL database.
This agent utilizes LangChain to create a flow that takes user questions in plain English, then uses an LLM (Large Language Model) to generate a SQL request. It executes the request on your database and then uses the LLM again to respond as a human would, or to convert the response into a JSON object for your downstream APIs.
0:00 SQL agent's architecture
0:51 Building the agent
7:00 Testing the agent
9:48 Homework
The Jupyter Notebook (do it yourself!):
github.com/dmagda/DevMastersD...
Curious to learn even more about databases? Follow me here:
* Medium: / magda7817
* Twitter: / denismagda

Пікірлер: 44
@slowjocrow6451
@slowjocrow6451 3 ай бұрын
Crazy stuff, thanks for the video
@applepeel1662
@applepeel1662 4 ай бұрын
Really cool! Thanks a lot
@vamsiraghu3258
@vamsiraghu3258 4 ай бұрын
excellent explanation and the demo. Thank you!
@DevMastersDb
@DevMastersDb 4 ай бұрын
Thank you for feedback! Glad you found it useful.
@supimon9146
@supimon9146 5 ай бұрын
many thanks and keep going my friend !
@DevMastersDb
@DevMastersDb 5 ай бұрын
Glad you liked it my friend!
@stevenhkdb
@stevenhkdb 3 ай бұрын
crazy useful and stragit to the point!!!
@DevMastersDb
@DevMastersDb 3 ай бұрын
Yep, this stuff is crazy. And that’s just the beginning. It’s gonna be much wilder soon )
@ivonne8412
@ivonne8412 4 ай бұрын
Nice well explained content, subscribed!
@DevMastersDb
@DevMastersDb 4 ай бұрын
Welcome aboard! Glad you found the tutorial useful 👍
@anuragangara2619
@anuragangara2619 Ай бұрын
Thanks for this video! Got some really promising results in just a few hours! Quick question -- I don't need the context to have the full table ["products", "users", etc]. For my use-case I only need it to have context for the user. (i.e. products for user 1005, purchases for user 1005, etc) If I provide the full tables in include_tables, it very quickly reaches the token limit. Is there a way to dynamically reduce the amount of context when initializing the Langchain Database agent?
@DevMastersDb
@DevMastersDb Ай бұрын
Excellent! glad you managed to get it working on your end 👍 Try to instruct the LLM to retrieve that context dynamically. For instance, you can say to execute the query "select name, price from products where id = {id}" setting the id as a parameter. Then, the LLM can perform this request over the database and pull a user-specific context. Also, LangChain support various tools/actions that let LLM pull info from other sources or perform various actions: python.langchain.com/v0.1/docs/modules/tools/
@anuragangara2619
@anuragangara2619 Ай бұрын
@@DevMastersDb That makes sense, the issue is (unless I'm misunderstanding), passing entire tables to the LLM, regardless of whether the LLM knows that it should filter down to a subset of data, seems to take a lot of tokens. i.e. We're providing a lot of context, and then asking the LLM to disregard most of it (as opposed to providing the narrow context in the first place). As a result, if I add more than two tables, I get the error: {'error': {'message': "This model's maximum context length is 4097 tokens, however you requested 10494 tokens. Please reduce your prompt; or completion length.", 'type': 'invalid_request_error', 'param': None, 'code': None}} I'm extremely new to this stuff (just a day or two), so I could totally be missing something! One thing I'm going to try next is to create a View with the data after I've applied some joins and filters to it and then pass the view in include_tables instead, so I'm providing just the minimum context the model would need.. not sure if that'll work, or is even the right way of thinking about it 🤔
@DevMastersDb
@DevMastersDb Ай бұрын
@@anuragangara2619 how many tables do you folks have? First things, yes, ask the agent to look into those which are necessary by defining them in the "include_tables". The LLM will pull only the metadata of those tables and not the actual data. So, it should fit in the context window. And then in your system message for the LLM you also say that use this query to pull the actual data for a selected user. If that data set doesn't fit into the context, then try to filter by the user_id/product_id and some time frame. Anyway, take it slowly and learn LangChain capabilities by playing with a few tutorials. You'll definitely get it figured!
@anuragangara2619
@anuragangara2619 Ай бұрын
@@DevMastersDb Hmm, odd -- only 5 or 6 tables; personal project, so just me. I'll look into that then! Thanks so much for the great video, really enjoyed this!
@DevMastersDb
@DevMastersDb Ай бұрын
@@anuragangara2619 make sure that LLM doesn’t pull data, enable the “verbose” mode to see what happens. Also, start with 1-2 tables and then add more, see when it breaks. It might have to discover the root cause. Glad you liked the video, thanks for feedback!
@anagai
@anagai 3 ай бұрын
what model is this using? Can we do this with Azure openai?
@DevMastersDb
@DevMastersDb 3 ай бұрын
I used OpenAI GPT 4. Absolutely, you can use other models including Azure OpenAI
@user-xd6ut8vm4d
@user-xd6ut8vm4d 5 ай бұрын
Thank you so much. Just one query prepare_agent_prompt Trying to create 5 different agent prompts example query with Mysql, Query with Postgres, Query with Aurora. Should I create 5 different agent prompt or as per {input_text} it will manage
@DevMastersDb
@DevMastersDb 5 ай бұрын
Thanks! Glad you liked the video.👍 The {input_text} is the user prompt (your actual question) that you are asking later in the notebook. What you can do is to add {database_dialect} as another parameter to the agent prompt’s text and pass Postgres, MySQL or another db name. If the behavior between databases is very different, then just create a unique agent prompt for every database.
@kollisreekanth
@kollisreekanth 3 ай бұрын
Really wonderful video. Thanks for sharing it with everyone. I just have a question, can we use this with NoSql databases like MongoDB/DynamoDB?
@DevMastersDb
@DevMastersDb 3 ай бұрын
Glad you found the video useful! As for MongoDB and other NoSQL databases, I don’t see that LangChain supports agents for them. But some folks found a way how to create custom agents using foundational capabilities of LangChain: omershahzad.medium.com/agent-for-mongodb-langchain-ccf69913a11a
@kollisreekanth
@kollisreekanth 3 ай бұрын
@@DevMastersDb thank you so much for the quick reply. Appreciate it 🙏🏼
@user-xd6ut8vm4d
@user-xd6ut8vm4d 5 ай бұрын
second query want to switch between different API using chat prompt in context with this how agents will work can you please create a video or share some document.
@DevMastersDb
@DevMastersDb 5 ай бұрын
You mean you want to use another LLM, the one that is different from OpenAI? If yes, LangChain supports many LLM providers.
@user-xd6ut8vm4d
@user-xd6ut8vm4d 5 ай бұрын
@@DevMastersDb API i mean our software application API routes I want to call API as per prompt for example. /teacher /student if prompt is show student details student route should execute. So shall i go with agent concept please guide Thank you so much for your prompt response
@DevMastersDb
@DevMastersDb 5 ай бұрын
I see. In fact, the model (at least OpenAI GPT4) can make a call to your API depending on a user prompt. You need to create an OpenAI spec that defines your /teacher and /student endpoints. Then the GPT model will execute one of the endpoints depending on a prompt. Check my latest video on this topic. This should be a perfect starting point for you: kzfaq.info/get/bejne/j9mYbMeozsecdoE.htmlsi=fr9bWEOFrmSaAsO1
@Mostafa_Sharaf_4_9
@Mostafa_Sharaf_4_9 4 ай бұрын
what if I want my agent to add data to the database ,for example If I want to make an agent for a clinic that book a new appointment for the patient and store his information in the database
@DevMastersDb
@DevMastersDb 4 ай бұрын
Yes, the agent can modify data by translating your requests into INSERT, UPDATE, DELETE, and other DML/DDL statements. In the video, the agent connects to the database using a read-only user role. The agent still can generate a statement updating the database but the database won’t let it execute the statement. Overall, you can create a database role/user for the agent with broader permission and use it at your own risk.
@Mostafa_Sharaf_4_9
@Mostafa_Sharaf_4_9 4 ай бұрын
@@DevMastersDb can you please tell me how to make a database with border permission ?
@DevMastersDb
@DevMastersDb 4 ай бұрын
@@Mostafa_Sharaf_4_9 if you follow the Jupyter notebook shared in the description of the video, then you'll find this code that opens a connection to the database SQLDatabase.from_uri( "postgresql+psycopg2://sql_agent:password@localhost:5432/postgres", include_tables=["products", "users", "purchases", "product_inventory"]); Now, replace "sql_agent" with "postgres" user in that snippet and the agent will connect to the database using the "postgres" user that can do anything.
@Mostafa_Sharaf_4_9
@Mostafa_Sharaf_4_9 4 ай бұрын
thank you , last question please , how to make agent with custom prompt with memory ? @@DevMastersDb
@huiraym
@huiraym 5 ай бұрын
Wonderful video!!! What if the table or column names or even the data are not english like, i.e. a product table is named PD and columns are id, prodcode, typecode, or some combination of columns for a particular info. Data sometimes can be some ridiculous abbreviation. how would you educate it? Via prompt? Thanks so much😂
@DevMastersDb
@DevMastersDb 5 ай бұрын
Thanks, glad you liked it! Non-English data is easy as long as the OpenAI GPT LLM speaks and understands many languages. For instance, you can run the Jupyter notebook from the video and ask questions in a different language and/or insert non English data. The LLM should handle it easily. As for cumbersome abbreviations, I have no idea for now :) but I’ll sort this out while preparing for the following live stream next week. Join if you wish, I’ll make sure to answer the question: kzfaq.info-UvpSBHJFdU?si=oirCEwPCnHRJUBYQ
@GeriFitrah
@GeriFitrah 4 ай бұрын
Will it work with chat history?
@DevMastersDb
@DevMastersDb 4 ай бұрын
Yes, you need to tweak the current implementation as follows: 1. Store the history in some variable like "chat_history". 2. Pass this variable to the agent prompt that is generated by the "prepare_agent_prompt" method. 3. You can append the chat history to the end "agent_prompt" variable as follows ".... also, consider the following chat history {chat_history}"
@slowjocrow6451
@slowjocrow6451 3 ай бұрын
What is RAG?
@DevMastersDb
@DevMastersDb 3 ай бұрын
RAG stands for retrieval-augmented generation. It's a technique to enhance the behavior of an LLM by providing it with more context. Usually, you get that context from your own database that stores your own data. For instance, let's say you ask ChatGTP to recommend a few places to stay in NYC between April 1-7, 2023. ChatGPT doesn't know those details, it was trained on some generic data from the past and didn't have access to the private data of Expedia or Booking.com. But Expedia/Booking's own AI assistant can easily address this task by using the RAG approach. You ask their assistant to recommend the places, they query data from the database and feed it as a context to an LLM (that can be GPT), and then the LLM responds to you like a human would.
@slowjocrow6451
@slowjocrow6451 3 ай бұрын
@@DevMastersDb Great explanation thanks. So is your langchain example RAG? Because it's providing extra metadata etc to your query? I've looked at a few examples of langchain and it seems to match my idea of what RAG is, but langchain doesn't call itself RAG so maybe I'm missing something. Trying to figure out what all these new buzzwords mean hah
@DevMastersDb
@DevMastersDb 3 ай бұрын
@@slowjocrow6451 yep, LangChain doesn't have any RAG-specific APIs and it doesn't need them. But when you create those chains (with LangChain) and some parts of the chain retrieve additional information from a database or another resource and feed this information as an extra context to an LLM - then you're effectively creating a RAG-based solution with LangChain. Hope it makes sense. Also, I found this LangChain cookbook useful, take a look: python.langchain.com/docs/expression_language/cookbook/retrieval
@applepeel1662
@applepeel1662 4 ай бұрын
Really cool! Thanks a lot
@DevMastersDb
@DevMastersDb 4 ай бұрын
Glad you liked it! Anything else you'd like to learn about? It should be related to databases (the focus of my channel)
Robust Text-to-SQL With LangChain: Claude 3 vs GPT-4
19:40
Rabbitmetrics
Рет қаралды 3,2 М.
Llama3 + CrewAI + Groq = Email AI Agent
14:27
Sam Witteveen
Рет қаралды 53 М.
1❤️
00:17
Nonomen ノノメン
Рет қаралды 13 МЛН
Tom & Jerry !! 😂😂
00:59
Tibo InShape
Рет қаралды 67 МЛН
Me: Don't cross there's cars coming
00:16
LOL
Рет қаралды 14 МЛН
PostgreSQL Internals in Action: MVCC
16:06
Denis Magda
Рет қаралды 414
Chat with SQL and Tabular Databases using LLM Agents (DON'T USE RAG!)
58:54
Build an SQL Agent with Llama 3 | Langchain | Ollama
20:28
TheAILearner
Рет қаралды 3,5 М.
AI Pioneer Shows The Power of AI AGENTS - "The Future Is Agentic"
23:47
Use AI to chat with a relational SQL database with natural language!
22:53
Reliable, fully local RAG agents with LLaMA3
21:19
LangChain
Рет қаралды 101 М.
Хотела заскамить на Айфон!😱📱(@gertieinar)
0:21
Взрывная История
Рет қаралды 5 МЛН
Урна с айфонами!
0:30
По ту сторону Гугла
Рет қаралды 8 МЛН
Что еще за съемные фронталки от Vivo? #vivo
0:41
Собери ПК и Получи 10,000₽
1:00
build monsters
Рет қаралды 2,3 МЛН