From ab11233e58f86d6efedb7ec26b81d1dc03b55f59 Mon Sep 17 00:00:00 2001 From: HamaWhiteGG Date: Sat, 30 Sep 2023 21:58:05 +0800 Subject: [PATCH 1/4] optimize code --- .../spark/sql/toolkit/SparkSqlToolkitTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/langchain-bigdata/langchain-spark/src/test/java/com/hw/langchain/agents/toolkits/spark/sql/toolkit/SparkSqlToolkitTest.java b/langchain-bigdata/langchain-spark/src/test/java/com/hw/langchain/agents/toolkits/spark/sql/toolkit/SparkSqlToolkitTest.java index 6fb9c7a87..8eeccaa2b 100644 --- a/langchain-bigdata/langchain-spark/src/test/java/com/hw/langchain/agents/toolkits/spark/sql/toolkit/SparkSqlToolkitTest.java +++ b/langchain-bigdata/langchain-spark/src/test/java/com/hw/langchain/agents/toolkits/spark/sql/toolkit/SparkSqlToolkitTest.java @@ -93,11 +93,12 @@ static void setup() { void testDescribeTable() { var actual = agentExecutor.run("Describe the titanic table"); - var expected = """ - The titanic table has the following columns: PassengerId (INT), Survived (INT), Pclass (INT), Name (STRING), Sex (STRING), Age (DOUBLE), SibSp (INT), Parch (INT), Ticket (STRING), Fare (DOUBLE), Cabin (STRING), Embarked (STRING). Here are some sample rows from the table: - 1. PassengerId: 1, Survived: 0, Pclass: 3, Name: Braund, Mr. Owen Harris, Sex: male, Age: 22.0, SibSp: 1, Parch: 0, Ticket: A/5 21171, Fare: 7.25, Cabin: null, Embarked: S - 2. PassengerId: 2, Survived: 1, Pclass: 1, Name: Cumings, Mrs. John Bradley (Florence Briggs Thayer), Sex: female, Age: 38.0, SibSp: 1, Parch: 0, Ticket: PC 17599, Fare: 71.2833, Cabin: C85, Embarked: C - 3. PassengerId: 3, Survived: 1, Pclass: 3, Name: Heikkinen, Miss. Laina, Sex: female, Age: 26.0, SibSp: 0, Parch: 0, Ticket: STON/O2. 3101282, Fare: 7.925, Cabin: null, Embarked: S"""; + var expected = + """ + The titanic table has the following columns: PassengerId (INT), Survived (INT), Pclass (INT), Name (STRING), Sex (STRING), Age (DOUBLE), SibSp (INT), Parch (INT), Ticket (STRING), Fare (DOUBLE), Cabin (STRING), Embarked (STRING). Here are some sample rows from the table: + 1. PassengerId: 1, Survived: 0, Pclass: 3, Name: Braund, Mr. Owen Harris, Sex: male, Age: 22.0, SibSp: 1, Parch: 0, Ticket: A/5 21171, Fare: 7.25, Cabin: null, Embarked: S + 2. PassengerId: 2, Survived: 1, Pclass: 1, Name: Cumings, Mrs. John Bradley (Florence Briggs Thayer), Sex: female, Age: 38.0, SibSp: 1, Parch: 0, Ticket: PC 17599, Fare: 71.2833, Cabin: C85, Embarked: C + 3. PassengerId: 3, Survived: 1, Pclass: 3, Name: Heikkinen, Miss. Laina, Sex: female, Age: 26.0, SibSp: 0, Parch: 0, Ticket: STON/O2. 3101282, Fare: 7.925, Cabin: null, Embarked: S"""; assertEquals(expected, actual); } From bc5e5cd7ffd31d1afc410762cb05bd30553e0368 Mon Sep 17 00:00:00 2001 From: HamaWhiteGG Date: Sun, 1 Oct 2023 01:46:03 +0800 Subject: [PATCH 2/4] optimize test cases --- .../agents/toolkits/flink/sql/toolkit/FlinkSqlToolkitTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/langchain-bigdata/langchain-flink/src/test/java/com/hw/langchain/agents/toolkits/flink/sql/toolkit/FlinkSqlToolkitTest.java b/langchain-bigdata/langchain-flink/src/test/java/com/hw/langchain/agents/toolkits/flink/sql/toolkit/FlinkSqlToolkitTest.java index e99482e2d..c4e167989 100644 --- a/langchain-bigdata/langchain-flink/src/test/java/com/hw/langchain/agents/toolkits/flink/sql/toolkit/FlinkSqlToolkitTest.java +++ b/langchain-bigdata/langchain-flink/src/test/java/com/hw/langchain/agents/toolkits/flink/sql/toolkit/FlinkSqlToolkitTest.java @@ -96,6 +96,7 @@ void testRunQuery() { .init(); var llm = ChatOpenAI.builder() + .model("gpt-4") .temperature(0) .build().init(); @@ -105,7 +106,7 @@ void testRunQuery() { // SELECT SQRT(AVG(Age)) FROM titanic var actual = agentExecutor.run("whats the square root of the average age?"); // sometimes it's 'The square root of the average age is approximately 5.07.' - assertEquals("5.07", actual); + assertEquals("The square root of the average age is approximately 5.07.", actual); // TODO: It should be DESC here, not ASC. // SELECT Name FROM titanic WHERE Survived = 1 ORDER BY Age ASC LIMIT 1 From 4aec20c7ed979f2c72da3447cd5096a3a9c5553a Mon Sep 17 00:00:00 2001 From: HamaWhiteGG Date: Wed, 4 Oct 2023 10:10:52 +0800 Subject: [PATCH 3/4] enhance code --- .../chains/query/constructor/base/BaseUtils.java | 3 ++- .../prompts/few/shot/FewShotPromptTemplate.java | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/langchain-core/src/main/java/com/hw/langchain/chains/query/constructor/base/BaseUtils.java b/langchain-core/src/main/java/com/hw/langchain/chains/query/constructor/base/BaseUtils.java index 3e28ada4f..63aaf0283 100644 --- a/langchain-core/src/main/java/com/hw/langchain/chains/query/constructor/base/BaseUtils.java +++ b/langchain-core/src/main/java/com/hw/langchain/chains/query/constructor/base/BaseUtils.java @@ -77,7 +77,8 @@ private static BasePromptTemplate getPrompt(String documentContents, List createTemplateArguments(List allowedComparators, diff --git a/langchain-core/src/main/java/com/hw/langchain/prompts/few/shot/FewShotPromptTemplate.java b/langchain-core/src/main/java/com/hw/langchain/prompts/few/shot/FewShotPromptTemplate.java index 82438fac8..51dc6d28e 100644 --- a/langchain-core/src/main/java/com/hw/langchain/prompts/few/shot/FewShotPromptTemplate.java +++ b/langchain-core/src/main/java/com/hw/langchain/prompts/few/shot/FewShotPromptTemplate.java @@ -63,15 +63,26 @@ public class FewShotPromptTemplate extends StringPromptTemplate { /** * String separator used to join the prefix, the examples, and suffix. */ - private String exampleSeparator = "\n\n"; + private String exampleSeparator; public FewShotPromptTemplate(List> examples, PromptTemplate examplePrompt, String prefix, - String suffix, List inputVariables, BaseOutputParser outputParser) { + String suffix, List inputVariables) { + this(examples, examplePrompt, prefix, suffix, inputVariables, "\n\n"); + } + + public FewShotPromptTemplate(List> examples, PromptTemplate examplePrompt, String prefix, + String suffix, List inputVariables, String exampleSeparator) { + this(examples, examplePrompt, prefix, suffix, inputVariables, exampleSeparator, null); + } + + public FewShotPromptTemplate(List> examples, PromptTemplate examplePrompt, String prefix, + String suffix, List inputVariables, String exampleSeparator, BaseOutputParser outputParser) { super(inputVariables, outputParser); this.examples = examples; this.examplePrompt = examplePrompt; this.prefix = prefix; this.suffix = suffix; + this.exampleSeparator = exampleSeparator; } public List> getExamples() { From e6cbcd06c2ebd85c32e54bc68387d1ff39f9a25c Mon Sep 17 00:00:00 2001 From: HamaWhiteGG Date: Thu, 5 Oct 2023 11:31:55 +0800 Subject: [PATCH 4/4] optimize code --- .../com/hw/langchain/utilities/GoogleSearchAPIWrapper.java | 6 +++++- .../hw/langchain/utilities/GoogleSearchAPIWrapperTest.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/langchain-core/src/main/java/com/hw/langchain/utilities/GoogleSearchAPIWrapper.java b/langchain-core/src/main/java/com/hw/langchain/utilities/GoogleSearchAPIWrapper.java index 938b6137e..d24e070fe 100644 --- a/langchain-core/src/main/java/com/hw/langchain/utilities/GoogleSearchAPIWrapper.java +++ b/langchain-core/src/main/java/com/hw/langchain/utilities/GoogleSearchAPIWrapper.java @@ -136,6 +136,10 @@ public List> results(String query, int num) { }).toList(); } + public static Builder builder() { + return new Builder(); + } + public static final class Builder { /** @@ -163,7 +167,7 @@ public static final class Builder { */ private int readTimeout = 20 * 1000; - public Builder() { + private Builder() { } public Builder googleApiKey(String googleApiKey) { diff --git a/langchain-core/src/test/java/com/hw/langchain/utilities/GoogleSearchAPIWrapperTest.java b/langchain-core/src/test/java/com/hw/langchain/utilities/GoogleSearchAPIWrapperTest.java index cea7ee2e2..a1b1c0c85 100644 --- a/langchain-core/src/test/java/com/hw/langchain/utilities/GoogleSearchAPIWrapperTest.java +++ b/langchain-core/src/test/java/com/hw/langchain/utilities/GoogleSearchAPIWrapperTest.java @@ -38,7 +38,7 @@ class GoogleSearchAPIWrapperTest { void testGoogleSearch() { String query = "2022 USA national auto sales by brand"; - GoogleSearchAPIWrapper searchWrapper = new GoogleSearchAPIWrapper.Builder() + GoogleSearchAPIWrapper searchWrapper = GoogleSearchAPIWrapper.builder() .connectTimeout(10 * 1000) .build();