-
Notifications
You must be signed in to change notification settings - Fork 64
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 #504 from jmartisk/jbang-sample
JBang sample
- Loading branch information
Showing
2 changed files
with
43 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,30 @@ | ||
# JBang sample aka getting an AI-powered bot in 13 lines of Java | ||
|
||
To run the sample, you need JBang installed. If you don't have it, choose | ||
one of the installation options from the [JBang | ||
website](https://www.jbang.dev/download/). | ||
|
||
You also have to set your OpenAI API key: | ||
|
||
``` | ||
export QUARKUS_LANGCHAIN4J_OPENAI_API_KEY=<your-openai-api-key> | ||
``` | ||
|
||
Then, run the example with: | ||
|
||
``` | ||
jbang jokes.java | ||
``` | ||
|
||
To have it tell you a joke, call `http://localhost:8080/joke` with a GET | ||
request. | ||
|
||
Explanation: The code contains a single method which injects a | ||
`io.vertx.ext.web.Router`, which is a class from the `quarkus-vertx-http` | ||
extension responsible for routing requests to appropriate handlers. The | ||
method is called during application boot thanks to the `@Observes` | ||
annotation, and it uses the injected `Router` to define a single route on | ||
the `/joke` path. The route's handler (the lambda expression that accepts a `rc` - | ||
`RoutingContext` parameter) invokes the model and sets the HTTP response. | ||
See [Quarkus documentation](https://quarkus.io/guides/reactive-routes#using-the-vert-x-web-router) | ||
for more information. |
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,13 @@ | ||
//DEPS io.quarkus.platform:quarkus-bom:3.9.4@pom | ||
//DEPS io.quarkus:quarkus-vertx-http:3.9.4 | ||
//DEPS io.quarkiverse.langchain4j:quarkus-langchain4j-openai:0.12.0 | ||
|
||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import io.vertx.ext.web.Router; | ||
import jakarta.enterprise.event.Observes; | ||
|
||
public class jokes { | ||
void route(@Observes Router router, ChatLanguageModel ai) { | ||
router.get("/joke").blockingHandler(rc -> rc.end(ai.generate("tell me a joke"))); | ||
} | ||
} |