-
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #3 from hbelmiro/langchain4j
intelligent-java-blog-reader
- Loading branch information
Showing
11 changed files
with
366 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: "maven" | ||
directory: "/intelligent-java-blog-reader" | ||
schedule: | ||
interval: "daily" | ||
ignore: | ||
- dependency-name: "org.apache.maven.plugins:maven-compiler-plugin" | ||
- package-ecosystem: "github-actions" | ||
directory: "/" | ||
schedule: | ||
interval: "weekly" |
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,23 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up JDK 21 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '21' | ||
distribution: 'temurin' | ||
cache: maven | ||
- name: Build with Maven - intelligent-java-blog-reader | ||
run: mvn -B package --file intelligent-java-blog-reader/pom.xml |
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,7 @@ | ||
# first-quarkus-intelligent-app | ||
|
||
```bash | ||
curl -X 'POST' \ | ||
'http://localhost:8080/read' \ | ||
-d 'https://thegreatapi.com/blog/a-beginners-guide-to-contributing-to-open-source/' | ||
``` |
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,134 @@ | ||
<?xml version="1.0"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.hbelmiro.demos</groupId> | ||
<artifactId>intelligent-java-blog-reader</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<properties> | ||
<compiler-plugin.version>3.11.0</compiler-plugin.version> | ||
<maven.compiler.release>17</maven.compiler.release> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id> | ||
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id> | ||
<quarkus.platform.version>3.6.4</quarkus.platform.version> | ||
<quarkus.langchain4j.version>0.6.2</quarkus.langchain4j.version> | ||
<com.knuddels.jtokkit.version>0.6.1</com.knuddels.jtokkit.version> | ||
<skipITs>true</skipITs> | ||
<surefire-plugin.version>3.1.2</surefire-plugin.version> | ||
</properties> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>${quarkus.platform.group-id}</groupId> | ||
<artifactId>${quarkus.platform.artifact-id}</artifactId> | ||
<version>${quarkus.platform.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-reactive</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-smallrye-openapi</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-arc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkiverse.langchain4j</groupId> | ||
<artifactId>quarkus-langchain4j-openai</artifactId> | ||
<version>${quarkus.langchain4j.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.knuddels</groupId> | ||
<artifactId>jtokkit</artifactId> | ||
<version>${com.knuddels.jtokkit.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>${quarkus.platform.group-id}</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<version>${quarkus.platform.version}</version> | ||
<extensions>true</extensions> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>build</goal> | ||
<goal>generate-code</goal> | ||
<goal>generate-code-tests</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${compiler-plugin.version}</version> | ||
<configuration> | ||
<compilerArgs> | ||
<arg>-parameters</arg> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>${surefire-plugin.version}</version> | ||
<configuration> | ||
<systemPropertyVariables> | ||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> | ||
<maven.home>${maven.home}</maven.home> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<version>${surefire-plugin.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>integration-test</goal> | ||
<goal>verify</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<systemPropertyVariables> | ||
<native.image.path>${project.build.directory}/${project.build.finalName}-runner | ||
</native.image.path> | ||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> | ||
<maven.home>${maven.home}</maven.home> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<profiles> | ||
<profile> | ||
<id>native</id> | ||
<activation> | ||
<property> | ||
<name>native</name> | ||
</property> | ||
</activation> | ||
<properties> | ||
<skipITs>false</skipITs> | ||
<quarkus.package.type>native</quarkus.package.type> | ||
</properties> | ||
</profile> | ||
</profiles> | ||
</project> |
56 changes: 56 additions & 0 deletions
56
...reader/src/main/java/com/hbelmiro/demos/intelligentjavablogreader/BlogReaderResource.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,56 @@ | ||
package com.hbelmiro.demos.intelligentjavablogreader; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.List; | ||
|
||
@Path("/") | ||
public class BlogReaderResource { | ||
|
||
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(BlogReaderResource.class); | ||
|
||
private final BlogReaderService blogReaderService; | ||
|
||
private final WebCrawler webCrawler; | ||
|
||
private final RequestSplitter requestSplitter; | ||
|
||
@Inject | ||
public BlogReaderResource(BlogReaderService blogReaderService, WebCrawler webCrawler, RequestSplitter requestSplitter) { | ||
this.blogReaderService = blogReaderService; | ||
this.webCrawler = webCrawler; | ||
this.requestSplitter = requestSplitter; | ||
} | ||
|
||
|
||
@Path("/read") | ||
@POST | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String read(String url) { | ||
String content = webCrawler.crawl(url); | ||
|
||
LOGGER.info("\uD83D\uDD1C Preparing analysis of {}...", url); | ||
|
||
blogReaderService.prepare(); | ||
|
||
List<String> split = requestSplitter.split(content); | ||
|
||
for (int i = 0; i < split.size(); i++) { | ||
blogReaderService.sendBody(split.get(i)); | ||
LOGGER.info("\uD83E\uDDD0 Analyzing article... Part {} out of {}.", (i + 1), split.size()); | ||
} | ||
|
||
LOGGER.info("\uD83D\uDCDD Preparing response..."); | ||
|
||
String sumUp = blogReaderService.sumUp(); | ||
|
||
LOGGER.info("✅ Response for {} ready", url); | ||
|
||
return sumUp; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...-reader/src/main/java/com/hbelmiro/demos/intelligentjavablogreader/BlogReaderService.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,29 @@ | ||
package com.hbelmiro.demos.intelligentjavablogreader; | ||
|
||
import dev.langchain4j.service.SystemMessage; | ||
import dev.langchain4j.service.UserMessage; | ||
import io.quarkiverse.langchain4j.RegisterAiService; | ||
|
||
@RegisterAiService | ||
public interface BlogReaderService { | ||
|
||
@SystemMessage("You are an assistant that receives the body of an html page and sum up the article in that page. Add key takeaways to the end of the sum up.") | ||
@UserMessage(""" | ||
The body will be sent in parts in the next requests. Don't return anything. | ||
""") | ||
String prepare(); | ||
|
||
@UserMessage(""" | ||
Here's the next part of the body page: | ||
```html | ||
{html}. | ||
``` | ||
Wait for the next parts. Don't answer anything else. | ||
""") | ||
String sendBody(String html); | ||
|
||
@UserMessage(""" | ||
That's it. You can sum up the article now. | ||
""") | ||
String sumUp(); | ||
} |
36 changes: 36 additions & 0 deletions
36
...og-reader/src/main/java/com/hbelmiro/demos/intelligentjavablogreader/RequestSplitter.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,36 @@ | ||
package com.hbelmiro.demos.intelligentjavablogreader; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@ApplicationScoped | ||
class RequestSplitter { | ||
|
||
private static final int MAX_CHARACTERS = 2000; | ||
|
||
List<String> split(String text) { | ||
List<String> pieces = new ArrayList<>(); | ||
|
||
if (text != null && !text.isEmpty() && MAX_CHARACTERS > 0) { | ||
int length = text.length(); | ||
|
||
if (length <= MAX_CHARACTERS) { | ||
return List.of(text); | ||
} | ||
|
||
int startIndex = 0; | ||
int endIndex = MAX_CHARACTERS; | ||
|
||
while (startIndex < length) { | ||
String piece = text.substring(startIndex, endIndex); | ||
pieces.add(piece); | ||
startIndex = endIndex; | ||
endIndex = Math.min(startIndex + MAX_CHARACTERS, length); | ||
} | ||
} | ||
|
||
return pieces; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...va-blog-reader/src/main/java/com/hbelmiro/demos/intelligentjavablogreader/WebCrawler.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,23 @@ | ||
package com.hbelmiro.demos.intelligentjavablogreader; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
@ApplicationScoped | ||
class WebCrawler { | ||
|
||
String crawl(String url) { | ||
Document doc; | ||
try { | ||
doc = Jsoup.connect(url).get(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
|
||
return doc.body().getElementsByClass("nv-content-wrap").first().html(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
intelligent-java-blog-reader/src/main/resources/application.properties
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,5 @@ | ||
quarkus.log.level=INFO | ||
quarkus.http.read-timeout=120s | ||
quarkus.langchain4j.openai.timeout=1m | ||
|
||
%test.quarkus.langchain4j.openai.api-key=a-key |
23 changes: 23 additions & 0 deletions
23
...er/src/test/java/com/hbelmiro/demos/intelligentjavablogreader/BlogReaderResourceTest.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,23 @@ | ||
package com.hbelmiro.demos.intelligentjavablogreader; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@QuarkusTest | ||
class BlogReaderResourceTest { | ||
|
||
@Inject | ||
WebCrawler webCrawler; | ||
|
||
@Inject | ||
RequestSplitter requestSplitter; | ||
|
||
@Test | ||
void test() { | ||
var blogReaderResource = new BlogReaderResource(new FakeBlogReaderService(), webCrawler, requestSplitter); | ||
assertEquals("MockGPT", blogReaderResource.read("https://thegreatapi.com/blog/a-beginners-guide-to-contributing-to-open-source/")); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...der/src/test/java/com/hbelmiro/demos/intelligentjavablogreader/FakeBlogReaderService.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,18 @@ | ||
package com.hbelmiro.demos.intelligentjavablogreader; | ||
|
||
public class FakeBlogReaderService implements BlogReaderService { | ||
@Override | ||
public String prepare() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String sendBody(String html) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String sumUp() { | ||
return "MockGPT"; | ||
} | ||
} |