Skip to content

Commit

Permalink
Add SQL Chain for README.md and QuickStart
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaWhiteGG committed Jun 6, 2023
1 parent 71af523 commit 3afea76
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Up until now, we’ve worked with the PromptTemplate and LLM primitives by thems

A chain in LangChain is made up of links, which can be either primitives like LLMs or other chains.

#### 2.5.1 LLM Chain
The most core type of chain is an LLMChain, which consists of a PromptTemplate and an LLM.
```java
OpenAI llm = OpenAI.builder()
Expand All @@ -100,7 +101,22 @@ System.out.println(chain.run("colorful socks"));
```shell
\n\nSocktastic!
```
There we go! There’s the first chain - an LLM Chain. This is one of the simpler types of chains, but understanding how it works will set you up well for working with more complex chains.
#### 2.5.2 SQL Chain
This example demonstrates the use of the SQLDatabaseChain for answering questions over a database.
```java
SQLDatabase database = SQLDatabase.fromUri("jdbc:mysql://127.0.0.1:3306/demo", "root", "123456");

BaseLanguageModel llm = OpenAI.builder()
.temperature(0)
.build()
.init();

Chain chain = SQLDatabaseChain.fromLLM(llm, database);
System.out.println(chain.run("How many students are there?"));
```
```shell
There are 6 students.
```

## 3. Run Test Cases from Source
```
Expand Down
16 changes: 16 additions & 0 deletions langchain-core/src/test/java/com/hw/langchain/QuickStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

package com.hw.langchain;

import com.hw.langchain.base.language.BaseLanguageModel;
import com.hw.langchain.chains.base.Chain;
import com.hw.langchain.chains.llm.LLMChain;
import com.hw.langchain.chains.sql.database.base.SQLDatabaseChain;
import com.hw.langchain.llms.openai.OpenAI;
import com.hw.langchain.prompts.prompt.PromptTemplate;

import com.hw.langchain.sql.database.SQLDatabase;
import lombok.experimental.UtilityClass;

import java.util.List;
Expand Down Expand Up @@ -67,9 +70,22 @@ private void llmChain() {
System.out.println(chain.run("colorful socks"));
}

private void sqlChain() {
SQLDatabase database = SQLDatabase.fromUri("jdbc:mysql://127.0.0.1:3306/demo", "root", "123456");

BaseLanguageModel llm = OpenAI.builder()
.temperature(0)
.build()
.init();

Chain chain = SQLDatabaseChain.fromLLM(llm, database);
System.out.println(chain.run("How many students are there?"));
}

public static void main(String[] args) {
llm();
promptTemplate();
llmChain();
sqlChain();
}
}
12 changes: 12 additions & 0 deletions scripts/mysql/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO demo.students (id, name, score, teacher_note) VALUES (1, 'Alex', 100, 'Alex did perfectly every day in the class.');
INSERT INTO demo.students (id, name, score, teacher_note) VALUES (2, 'Alice', 70, 'Alice needs a lot of improvements.');
INSERT INTO demo.students (id, name, score, teacher_note) VALUES (3, 'Jack', 75, 'Event it is not the best, Jack has already improved.');
INSERT INTO demo.students (id, name, score, teacher_note) VALUES (4, 'Ophelia', 0, 'Unfortunately, Ophelia missed the test.');
INSERT INTO demo.students (id, name, score, teacher_note) VALUES (5, 'Zack', 60, 'Zack needs to do better.');
INSERT INTO demo.students (id, name, score, teacher_note) VALUES (6, 'HamaWhite', 95, 'Keep going.');

INSERT INTO demo.parents (id, student_name, parent_name, parent_mobile) VALUES (1, 'Alex', 'Barry', '088121');
INSERT INTO demo.parents (id, student_name, parent_name, parent_mobile) VALUES (2, 'Alice', 'Jessica', '088122');
INSERT INTO demo.parents (id, student_name, parent_name, parent_mobile) VALUES (3, 'Jack', 'Simon', '088123');
INSERT INTO demo.parents (id, student_name, parent_name, parent_mobile) VALUES (5, 'Ophelia', 'Tracy', '088124');
INSERT INTO demo.parents (id, student_name, parent_name, parent_mobile) VALUES (6, 'HamaWhite', 'Harper', '088125');
20 changes: 20 additions & 0 deletions scripts/mysql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE `students`
(
`id` int(11) DEFAULT NULL,
`name` varchar(64) DEFAULT NULL,
`score` int(11) DEFAULT NULL COMMENT 'math score',
`teacher_note` varchar(256) DEFAULT NULL
) COMMENT ='student score table';


CREATE TABLE `parents`
(
`id` int(11) DEFAULT NULL,
`student_name` varchar(64) DEFAULT NULL,
`parent_name` varchar(64) DEFAULT NULL,
`parent_mobile` varchar(16) DEFAULT NULL
);




0 comments on commit 3afea76

Please sign in to comment.