forked from HamaWhiteGG/langchain-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request HamaWhiteGG#97 from HamaWhiteGG/dev
Support Summarization(Stuff)
- Loading branch information
Showing
10 changed files
with
232 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
langchain-core/src/main/java/com/hw/langchain/document/loaders/WebBaseLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.langchain.document.loaders; | ||
|
||
import com.google.common.collect.Maps; | ||
import com.hw.langchain.document.loaders.base.BaseLoader; | ||
import com.hw.langchain.exception.LangChainException; | ||
import com.hw.langchain.schema.Document; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Element; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author HamaWhite | ||
*/ | ||
public class WebBaseLoader extends BaseLoader { | ||
|
||
private final List<String> webUrls; | ||
|
||
public WebBaseLoader(List<String> webUrls) { | ||
this.webUrls = webUrls; | ||
} | ||
|
||
@Override | ||
public List<Document> load() { | ||
List<Document> documents = new ArrayList<>(webUrls.size()); | ||
for (String url : webUrls) { | ||
try { | ||
org.jsoup.nodes.Document doc = Jsoup.connect(url).get(); | ||
Map<String, Object> metadata = buildMetadata(doc, url); | ||
|
||
documents.add(new Document(doc.wholeText(), metadata)); | ||
} catch (IOException e) { | ||
throw new LangChainException(errorMessage(url), e); | ||
} | ||
} | ||
return documents; | ||
} | ||
|
||
private Map<String, Object> buildMetadata(org.jsoup.nodes.Document doc, String url) { | ||
Map<String, Object> metadata = Maps.newHashMap(); | ||
metadata.put("source", url); | ||
|
||
Element title = doc.select("title").first(); | ||
if (title != null) { | ||
metadata.put("title", title.text()); | ||
} | ||
Element description = doc.select("meta[name=description]").first(); | ||
metadata.put("description", description != null ? description.attr("content") : "No description found."); | ||
|
||
Element html = doc.select("html").first(); | ||
metadata.put("language", html != null ? html.attr("lang") : "No language found."); | ||
return metadata; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
langchain-core/src/test/java/com/hw/langchain/chains/summarize/SummarizeUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.langchain.chains.summarize; | ||
|
||
import com.hw.langchain.base.language.BaseLanguageModel; | ||
import com.hw.langchain.document.loaders.WebBaseLoader; | ||
import com.hw.langchain.llms.openai.OpenAIChat; | ||
import com.hw.langchain.schema.Document; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* <a href="https://python.langchain.com/docs/use_cases/summarization">Summarization use cases</a> | ||
* | ||
* @author HamaWhite | ||
*/ | ||
@Disabled("Test requires costly OpenAI calls, can be run manually.") | ||
class SummarizeUtilsTest { | ||
|
||
private static BaseLanguageModel llm; | ||
|
||
private static List<Document> docs; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
llm = OpenAIChat.builder() | ||
.temperature(0) | ||
.model("gpt-3.5-turbo-16k") | ||
.build() | ||
.init(); | ||
|
||
var loader = new WebBaseLoader(List.of("https://lilianweng.github.io/posts/2023-06-23-agent/")); | ||
docs = loader.load(); | ||
} | ||
|
||
@Test | ||
void testLoadStuffChain() { | ||
var chain = SummarizeUtils.loadStuffChain(llm); | ||
var actual = chain.run(docs); | ||
|
||
var expected = | ||
"The article discusses the concept of building autonomous agents powered by large language models " + | ||
"(LLMs). It explores the components of such agents, including planning, memory, and tool " + | ||
"use. The article provides case studies and proof-of-concept examples of LLM-powered agents, " + | ||
"as well as challenges and limitations associated with their development."; | ||
assertEquals(expected, actual); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
langchain-examples/src/main/java/com/hw/langchain/examples/chains/SummarizationExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hw.langchain.examples.chains; | ||
|
||
import com.hw.langchain.chains.summarize.SummarizeUtils; | ||
import com.hw.langchain.document.loaders.WebBaseLoader; | ||
import com.hw.langchain.llms.openai.OpenAIChat; | ||
|
||
import java.util.List; | ||
|
||
import static com.hw.langchain.examples.utils.PrintUtils.println; | ||
|
||
/** | ||
* <a href="https://python.langchain.com/docs/use_cases/summarization">Summarization use cases</a> | ||
* | ||
* @author HamaWhite | ||
*/ | ||
public class SummarizationExample { | ||
|
||
public static void main(String[] args) { | ||
var llm = OpenAIChat.builder() | ||
.temperature(0) | ||
.model("gpt-3.5-turbo-16k") | ||
.build() | ||
.init(); | ||
|
||
var loader = new WebBaseLoader(List.of("https://lilianweng.github.io/posts/2023-06-23-agent/")); | ||
var docs = loader.load(); | ||
|
||
var chain = SummarizeUtils.loadStuffChain(llm); | ||
var result = chain.run(docs); | ||
|
||
println(result); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters