1
00:00:00,000 --> 00:00:03,840
[MUSIC]

2
00:00:03,840 --> 00:00:08,680
Welcome to this video, where you will learn about implementing LangChain's AI-powered

3
00:00:08,680 --> 00:00:14,079
SQL agent. After watching this video, you'll be able to explain the setup process for using

4
00:00:14,079 --> 00:00:19,280
an AI-powered LangChain SQL agent and describe how to run a natural language query using

5
00:00:19,280 --> 00:00:21,239
the LangChain SQL agent.

6
00:00:21,239 --> 00:00:23,582
Let's explore the setup process.

7
00:00:23,582 --> 00:00:30,920
First, create the Python virtual environment using the command virtualenv my_env to manage

8
00:00:30,920 --> 00:00:32,906
project dependencies.

9
00:00:32,906 --> 00:00:40,959
Install the required libraries including the ibm-watsonx-ai, LangChain, and mysql-connector-python

10
00:00:40,959 --> 00:00:44,560
to facilitate AI integration and database connectivity.

11
00:00:44,560 --> 00:00:48,960
Now it's time to launch your SQL server in the development environment.

12
00:00:48,960 --> 00:00:53,599
In this instance, open the MySQL window and select Create.

13
00:00:54,124 --> 00:01:00,200
Next, launch the MySQL server within the development environment. Allow approximately 15 seconds

14
00:01:00,200 --> 00:01:03,493
for the database to become active and ready for use.

15
00:01:03,493 --> 00:01:09,260
Select the MySQL CLI button so that you can interact with the server in a terminal window.

16
00:01:09,260 --> 00:01:15,617
A new terminal tab opens to the MySQL monitor. You will see MySQL as the root location.

17
00:01:15,617 --> 00:01:21,866
This implementation uses the Chinook database, a sample dataset representing a digital media store.

18
00:01:21,866 --> 00:01:26,639
It includes various tables that we'll query using our AI-powered system.

19
00:01:26,639 --> 00:01:31,080
The graph shows the Entity Relationship Diagram (or ERD) that illustrates how the different

20
00:01:31,080 --> 00:01:35,879
components such as the album name, artist information, track information, and media

21
00:01:35,879 --> 00:01:40,088
information in the Chinook database connect and interact with each other.

22
00:01:40,453 --> 00:01:47,410
To load the database, begin by selecting wget and the URL of the Chinook MySQL SQL

23
00:01:47,419 --> 00:01:49,739
file to fetch the SQL script.

24
00:01:49,739 --> 00:01:57,699
Next, in the MySQL terminal window, _SOURCE chinook-mysql.sql to load the contents

25
00:01:57,699 --> 00:02:00,248
of the SQL script into your database.

26
00:02:00,248 --> 00:02:05,182
Then run the command _SHOW DATABASES; to verify the creation of the database.

27
00:02:05,182 --> 00:02:10,100
Now run an SQL query so that you can verify that you can read the data. For example, suppose

28
00:02:10,100 --> 00:02:14,488
you want to know how many albums the Chinook database contains. To find this information,

29
00:02:14,488 --> 00:02:16,533
you could run the following SQL commands:

30
00:02:16,533 --> 00:02:17,546
USE Chinook;

31
00:02:17,546 --> 00:02:20,284
SELECT COUNT(*) FROM Album

32
00:02:20,284 --> 00:02:25,760
Now it's time to load the IBM watsonx.ai Granite large language model.

33
00:02:25,760 --> 00:02:31,093
To load the Granite large language model using IBM watsonx.ai, follow these steps.

34
00:02:31,093 --> 00:02:34,382
First create a dictionary to store credential information.

35
00:02:34,382 --> 00:02:36,328
Then set up the model credentials.

36
00:02:36,328 --> 00:02:38,693
Now initialize the model parameters.

37
00:02:38,693 --> 00:02:42,968
These parameters control how the model generates text during processing.

38
00:02:42,968 --> 00:02:47,860
First set the MAX_NEW_TOKENS parameters to define the maximum number

39
00:02:47,860 --> 00:02:50,817
of tokens that the model can generate in a single run.

40
00:02:50,817 --> 00:02:55,137
Configure the TEMPERATURE parameter to adjust the randomness of the generated text.

41
00:02:55,137 --> 00:02:59,619
Lower values make the output more predictable and deterministic, while higher values introduce

42
00:02:59,619 --> 00:03:01,699
greater creativity and variability.

43
00:03:01,699 --> 00:03:05,182
Next, specify the project and space IDs.

44
00:03:05,182 --> 00:03:07,955
The space ID identifies the project namespace.

45
00:03:07,955 --> 00:03:11,662
In this example, the project ID is "skills-network".

46
00:03:11,662 --> 00:03:15,653
Set the space ID to "None" if no specific space ID is required.

47
00:03:15,653 --> 00:03:20,179
If you are working in a local or unsecured environment, you might need to disable SSL

48
00:03:20,179 --> 00:03:23,191
verification by setting verify to false.

49
00:03:23,191 --> 00:03:26,782
Next, load the large language model using IBM watsonx.

50
00:03:26,782 --> 00:03:29,155
To begin, import the necessary classes.

51
00:03:29,155 --> 00:03:37,964
Specify the model from the ibm_watsonx_ai.foundation_models to initialize the watsonx.ai model.

52
00:03:37,964 --> 00:03:44,168
Then specify the watsonxLLM from langchain_ibm to integrate the model with LangChain.

53
00:03:44,168 --> 00:03:51,422
Initialize the watsonx.ai model by providing the model_id, which is the identifier for the specific model.

54
00:03:51,422 --> 00:03:56,666
Credentials, which is a dictionary containing authentication details, such as the API key.

55
00:03:56,666 --> 00:04:02,800
Params, such as generation parameters, such as max_new_tokens and temperature,

56
00:04:02,800 --> 00:04:08,035
and the project_id, which is your IBM watsonx project ID,

57
00:04:08,035 --> 00:04:11,460
and optionally the space ID if one exists.

58
00:04:11,460 --> 00:04:16,019
To use the watsonx.ai model within the LangChain framework, wrap the model using

59
00:04:16,019 --> 00:04:21,140
the watsonxLLM class which provides seamless integration with LangChain's features including

60
00:04:21,140 --> 00:04:27,324
chains and tools including chain-of-thought processing, text generation, and reasoning steps.

61
00:04:27,324 --> 00:04:32,915
This means you can build interactive chatbots, connect it to SQL agents for database querying,

62
00:04:32,915 --> 00:04:36,231
and integrate the model with vector stores for semantic search.

63
00:04:36,542 --> 00:04:40,311
Complete the following five steps to connect to the MySQL database.

64
00:04:40,311 --> 00:04:43,440
First, specify the MySQL connection parameters.

65
00:04:43,440 --> 00:04:47,537
To establish a connection, first define the necessary parameters including:

66
00:04:47,537 --> 00:04:51,641
mysql_username: your MySQL username

67
00:04:51,641 --> 00:04:55,645
mysql_password: your MySQL password

68
00:04:55,645 --> 00:05:01,217
mysql_host: the IP address or hostname for your MySQL server

69
00:05:01,217 --> 00:05:07,290
mysql_port: the port number for MySQL (default is 3306)

70
00:05:07,290 --> 00:05:13,299
and the database_name: the name of the database you created, in this case, is Chinook.

71
00:05:13,859 --> 00:05:19,100
Next, build the URI (Uniform Resource Identifier), which is a connection string that combines

72
00:05:19,100 --> 00:05:22,800
all the parameters into a single string for easy connection setup.

73
00:05:22,800 --> 00:05:30,026
Use LangChain's SQLDatabase.from_URI method for seamless integration to the MySQL server.

74
00:05:30,231 --> 00:05:34,940
By combining an AI model with the database connection, you create an SQL agent using

75
00:05:34,940 --> 00:05:39,191
LangChain's create_sql_agent function.

76
00:05:39,191 --> 00:05:43,420
This agent will handle the translation of natural language queries into SQL.

77
00:05:43,760 --> 00:05:47,075
First, set up the SQL agent from LangChain.

78
00:05:47,075 --> 00:05:51,324
Import create_sql_agent from LangChain.

79
00:05:51,324 --> 00:05:56,220
You are creating an SQL agent, which connects the LLM to a database so the agent can answer

80
00:05:56,220 --> 00:05:59,395
database-related questions using natural language.

81
00:05:59,395 --> 00:06:04,204
Next, pass in the LLM and the database shown as db.

82
00:06:04,204 --> 00:06:08,373
Set verbose to True if you want to see more details while the code runs.

83
00:06:08,373 --> 00:06:14,328
And finally, set the agent type to a zero-shot agent that performs a reasoning step before acting.

84
00:06:14,586 --> 00:06:19,022
Test your setup with a simple query: "How many Albums are listed in the database?"

85
00:06:19,022 --> 00:06:22,213
The system sends a natural language query to the agent.

86
00:06:22,213 --> 00:06:26,417
Then the LLM translates or invokes the query to SQL.

87
00:06:26,417 --> 00:06:28,622
The query runs against the database.

88
00:06:28,622 --> 00:06:33,146
The SQL agent responds with the correct count, demonstrating successful integration.

89
00:06:33,146 --> 00:06:38,019
By setting verbose equal to True in the code, you get to see not only the final answer but

90
00:06:38,019 --> 00:06:42,779
also the complete thought process including all actions taken by the LLM and the actual

91
00:06:42,779 --> 00:06:46,426
SQL queries it generates to arrive to that answer.

92
00:06:46,426 --> 00:06:51,484
When running correctly, the agent will confirm that there are 347 albums in the database,

93
00:06:51,484 --> 00:06:54,933
matching exactly what a direct SQL query would return.

94
00:06:54,933 --> 00:06:56,977
And the chain finishes.

95
00:06:56,977 --> 00:06:58,488
Let's recap what you've learned.

96
00:06:58,488 --> 00:07:02,640
Begin the implementation process by creating a Python virtual environment.

97
00:07:02,640 --> 00:07:06,008
This is done by running the virtualenv command.

98
00:07:06,008 --> 00:07:14,400
Install the necessary libraries, which include ibm-watsonx-ai, LangChain, and mysql-connector-python.

99
00:07:14,400 --> 00:07:20,240
These libraries enable the AI model's integration, LangChain functionality, and database connectivity.

100
00:07:20,240 --> 00:07:24,657
After the virtual environment is set up, you need to launch the MySQL server to access

101
00:07:24,657 --> 00:07:27,315
the MySQL CLI (Command Line Interface).

102
00:07:27,315 --> 00:07:31,804
To allow LangChain to interact with the database, you must build a database connector.

103
00:07:31,804 --> 00:07:36,053
LangChain requires an agent to interact with the database using natural language.

104
00:07:36,053 --> 00:07:41,875
Import the create_sql_agent function from LangChain's community toolkit.

105
00:07:41,875 --> 00:07:47,582
Initialize the agent with the LLM instance, the database connection, and the agent type.

106
00:07:47,582 --> 00:07:52,737
Enabling the verbose parameter to view detailed logs of the query translation and execution,

107
00:07:52,737 --> 00:07:57,546
and to run a query, you use a natural language query along with the SQL agent.

108
00:07:58,133 --> 00:08:00,897
[MUSIC]