From 40055a4ade3505cafcd7a479d34979954bff0dbf Mon Sep 17 00:00:00 2001 From: HamaWhite Date: Tue, 6 Jun 2023 22:54:05 +0800 Subject: [PATCH] optimize code --- README.md | 2 +- .../com/hw/langchain/chains/base/Chain.java | 4 +- .../java/com/hw/langchain/QuickStart.java | 2 +- .../hw/langchain/chains/llm/LLMChainTest.java | 102 +++++++++--------- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index a40210451..ed48251dd 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/langchain-core/src/main/java/com/hw/langchain/chains/base/Chain.java b/langchain-core/src/main/java/com/hw/langchain/chains/base/Chain.java index 4f0ba6905..65243c469 100644 --- a/langchain-core/src/main/java/com/hw/langchain/chains/base/Chain.java +++ b/langchain-core/src/main/java/com/hw/langchain/chains/base/Chain.java @@ -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; @@ -112,7 +110,7 @@ private Map prepInputs(String inputs) { * Validate and prep outputs. */ private Map prepOutputs(Map inputs, Map outputs, - boolean returnOnlyOutputs) { + boolean returnOnlyOutputs) { if (returnOnlyOutputs) { return outputs; } diff --git a/langchain-core/src/test/java/com/hw/langchain/QuickStart.java b/langchain-core/src/test/java/com/hw/langchain/QuickStart.java index 4fc147a95..83d4e1277 100644 --- a/langchain-core/src/test/java/com/hw/langchain/QuickStart.java +++ b/langchain-core/src/test/java/com/hw/langchain/QuickStart.java @@ -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; diff --git a/langchain-core/src/test/java/com/hw/langchain/chains/llm/LLMChainTest.java b/langchain-core/src/test/java/com/hw/langchain/chains/llm/LLMChainTest.java index 3e77c68ea..5347ecdbb 100644 --- a/langchain-core/src/test/java/com/hw/langchain/chains/llm/LLMChainTest.java +++ b/langchain-core/src/test/java/com/hw/langchain/chains/llm/LLMChainTest.java @@ -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); } } \ No newline at end of file