Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaWhiteGG committed Jun 6, 2023
1 parent 3afea76 commit 40055a4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ System.out.println(chain.run("colorful socks"));
#### 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");
SQLDatabase database = SQLDatabase.fromUri("jdbc:mysql://127.0.0.1:3306/demo", "xxx", "xxx");

BaseLanguageModel llm = OpenAI.builder()
.temperature(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package com.hw.langchain.chains.base;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,7 +110,7 @@ private Map<String, String> prepInputs(String inputs) {
* Validate and prep outputs.
*/
private Map<String, String> prepOutputs(Map<String, ?> inputs, Map<String, String> outputs,
boolean returnOnlyOutputs) {
boolean returnOnlyOutputs) {
if (returnOnlyOutputs) {
return outputs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,60 +76,62 @@ void testLLMChainWithMultipleInputVariables() {

@Test
void testLLMChainForNLP2SQL() {
String template = """
You are a H2 expert. Given an input question, first create a syntactically correct H2 query to run, then look at the results of the query and return the answer to the input question.
Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per H2. You can order the results to return the most informative data in the database.
Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers.
Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
Pay attention to use CURDATE() function to get the current date, if the question involves "today".
Use the following format:
Question: Question here
SQLQuery: SQL Query to run
SQLResult: Result of the SQLQuery
Answer: Final answer here
Only use the following tables:
CREATE TABLE parents (
id INTEGER(32),
student_name CHARACTER VARYING(64),
parent_name CHARACTER VARYING(64),
parent_mobile CHARACTER VARYING(16)
)
/*
3 rows from parents table:
id student_name parent_name parent_mobile
1 Alex Barry 088121
2 Alice Jessica 088122
3 Jack Simon 088123
*/
CREATE TABLE students (
id INTEGER(32),
name CHARACTER VARYING(64),
score INTEGER(32) COMMENT 'math score',
teacher_note CHARACTER VARYING(256)
) COMMENT 'student score table'
/*
3 rows from students table:
id name score teacher_note
1 Alex 100 Alex did perfectly every day in the class.
2 Alice 70 Alice needs a lot of improvements.
3 Jack 75 Event it is not the best, Jack has already improved.
*/
Question: Who got zero score? Show me her parent's contact information.
SQLQuery:""";
String template =
"""
You are a H2 expert. Given an input question, first create a syntactically correct H2 query to run, then look at the results of the query and return the answer to the input question.
Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per H2. You can order the results to return the most informative data in the database.
Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as delimited identifiers.
Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
Pay attention to use CURDATE() function to get the current date, if the question involves "today".
Use the following format:
Question: Question here
SQLQuery: SQL Query to run
SQLResult: Result of the SQLQuery
Answer: Final answer here
Only use the following tables:
CREATE TABLE parents (
id INTEGER(32),
student_name CHARACTER VARYING(64),
parent_name CHARACTER VARYING(64),
parent_mobile CHARACTER VARYING(16)
)
/*
3 rows from parents table:
id student_name parent_name parent_mobile
1 Alex Barry 088121
2 Alice Jessica 088122
3 Jack Simon 088123
*/
CREATE TABLE students (
id INTEGER(32),
name CHARACTER VARYING(64),
score INTEGER(32) COMMENT 'math score',
teacher_note CHARACTER VARYING(256)
) COMMENT 'student score table'
/*
3 rows from students table:
id name score teacher_note
1 Alex 100 Alex did perfectly every day in the class.
2 Alice 70 Alice needs a lot of improvements.
3 Jack 75 Event it is not the best, Jack has already improved.
*/
Question: Who got zero score? Show me her parent's contact information.
SQLQuery:""";
PromptTemplate prompt = new PromptTemplate(List.of(), template);

Chain chain = new LLMChain(llm, prompt);
String actual = chain.run(Map.of("stop", List.of("\nSQLResult:")));
String expected = " SELECT `parent_name`, `parent_mobile` FROM `parents` WHERE `student_name` IN (SELECT `name` FROM `students` WHERE `score` = 0) LIMIT 5;";
String expected =
" SELECT `parent_name`, `parent_mobile` FROM `parents` WHERE `student_name` IN (SELECT `name` FROM `students` WHERE `score` = 0) LIMIT 5;";
assertEquals(expected, actual);
}
}

0 comments on commit 40055a4

Please sign in to comment.