Skip to content

Commit

Permalink
Merge pull request #141 from eldermoraes/master
Browse files Browse the repository at this point in the history
New demos: Prompts, Chains & Memory, Agents/Tools, Embedding Documents
  • Loading branch information
kdubois committed May 15, 2024
2 parents cf49d31 + 859f4b3 commit d9f6a81
Show file tree
Hide file tree
Showing 31 changed files with 10,692 additions and 157 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 25 additions & 16 deletions documentation/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
* Requirements
** xref:setup.adoc[Setup]
** xref:01_setup.adoc[Setup]
* Basics
** xref:basics.adoc[Basics and Fundamentals]
** xref:configuration.adoc[Configuration]
** xref:panache.adoc[Hibernate with Panache]
** xref:kubernetes.adoc[Deploy to Kubernetes]
** xref:dev-services.adoc[Dev Services]
** xref:spring.adoc[Spring Compatibility]
** xref:02_basics.adoc[Basics and Fundamentals]
** xref:03_configuration.adoc[Configuration]
** xref:04_panache.adoc[Hibernate with Panache]
** xref:05_kubernetes.adoc[Deploy to Kubernetes]
** xref:06_dev-services.adoc[Dev Services]
** xref:07_spring.adoc[Spring Compatibility]
* Cloud Native
** xref:rest-client.adoc[REST Client]
** xref:fault-tolerance.adoc[Fault Tolerance]
** xref:health.adoc[Health Check]
** xref:observability.adoc[Observability]
** xref:security.adoc[Security with JWT RBAC]
// ** xref:security-oidc.adoc[Security using OpenID Connect]
** xref:08_rest-client.adoc[REST Client]
** xref:09_fault-tolerance.adoc[Fault Tolerance]
** xref:10_health.adoc[Health Check]
** xref:11_observability.adoc[Observability]
** xref:12_security.adoc[Security with JWT RBAC]
// ** xref:13_security-oidc.adoc[Security using OpenID Connect]
* Reactive
** xref:reactive.adoc[Reactive with Mutiny]
** xref:reactive-messaging.adoc[Streaming reactive messages]
** xref:kafka-and-streams.adoc[Apache Kafka with Reactive Streams]
** xref:14_reactive.adoc[Reactive with Mutiny]
** xref:15_reactive-messaging.adoc[Streaming reactive messages]
** xref:15_kafka-and-streams.adoc[Apache Kafka with Reactive Streams]
* AI
** xref:17_prompts.adoc[Working with prompts]
** xref:18_chains_memory.adoc[Chains and Memory]
** xref:19_agents_tools.adoc[Agents/Tools]
** xref:20_embed_documents.adoc[Embedding Documents]
//** xref:21_podman_ai[Working with Podman Desktop AI]
** xref:22_local_models.adoc[Working with local models]
//** xref:23_kubernetes_kafka_ai.adoc[Bringing Kubernetes and Kafka to the party]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ quarkus extension add mutiny,rest-client-jsonb,rest-jsonb

== Create Beer POJO

Create a new `Beer` Java class in `src/main/java` in the `org.acme` package with the following contents:
Create a new `Beer` Java class in `src/main/java` in the `com.redhat.developers` package with the following contents:

[.console-input]
[source,java]
----
package org.acme;
package com.redhat.developers;
import jakarta.json.bind.annotation.JsonbCreator;
Expand Down Expand Up @@ -83,12 +83,12 @@ public class Beer {

Now we're going to implement a Java interface that mimics the remote REST endpoint.

Create a new `BeerService` Java interface in `src/main/java` in the `org.acme` package with the following contents:
Create a new `BeerService` Java interface in `src/main/java` in the `com.redhat.developers` package with the following contents:

[.console-input]
[source,java]
----
package org.acme;
package com.redhat.developers;
import java.util.List;
Expand Down Expand Up @@ -122,7 +122,7 @@ Add the following properties to your `application.properties` in `src/main/resou
[.console-input]
[source,properties]
----
org.acme.BeerService/mp-rest/url=https://api.punkapi.com
com.redhat.developers.BeerService/mp-rest/url=https://api.punkapi.com
----

== Pagination + Filtering
Expand All @@ -133,12 +133,12 @@ image::pagination.png[]

=== Create BeerResource

Create a new `BeerResource` Java class in `src/main/java` in the `org.acme` package with the following contents:
Create a new `BeerResource` Java class in `src/main/java` in the `com.redhat.developers` package with the following contents:

[.console-input]
[source,java]
----
package org.acme;
package com.redhat.developers;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down
146 changes: 146 additions & 0 deletions documentation/modules/ROOT/pages/17_prompts.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
= Working with prompts

:project-ai-name: quarkus-langchain-app

The Quarkus LangChain4j extension seamlessly integrates Large Language Models (LLMs) into Quarkus applications. LLMs are AI-based systems designed to understand, generate, and manipulate human language, showcasing advanced natural language processing capabilities. Thanks to this extensions we can enable the harnessing of LLM capabilities for the development of more intelligent applications.

In this first chapter we'll explore the simplest of interactions with an LLM: Prompting. It essentially means just asking questions to an LLM and receiving an answer in natural language from a given Model, such as OpenAI, Mistral, Hugging Face, Ollama, etc.


== Creating a Quarkus & LangChain Application

[tabs%sync]
====
Maven::
+
--
[.console-input]
[source,bash,subs="+macros,+attributes"]
----
./mvnw "io.quarkus.platform:quarkus-maven-plugin:create" -DprojectGroupId="com.redhat.developers" -DprojectArtifactId="{project-ai-name}" -DprojectVersion="1.0-SNAPSHOT" -Dextensions=rest,langchain4j-core,langchain4j-openai
cd {project-ai-name}
----
--
Quarkus CLI::
+
--
[.console-input]
[source,bash,subs="+macros,+attributes"]
----
quarkus create app -x rest -x langchain4j-openai -x langchain4j-core com.redhat.developers:{project-ai-name}:1.0-SNAPSHOT
cd {project-ai-name}
----
--
====

IMPORTANT: All the remaining parts of this section assume that you'll be working inside the project folder that was just created. In this case, `{project-ai-name}`.

== Connect to OpenAI

LangChain4j provides you a proxy to connect your application to OpenAI by just adding a property to the `application.properties` file available in `src/main/resources`:

[.console-input]
[source,properties]
----
quarkus.langchain4j.openai.api-key=demo
----

== Create the AI service

First we need to create an interface for our AI service.

Create a new `Assistant` Java interface in `src/main/java` in the `com.redhat.developers` package with the following contents:

[.console-input]
[source,java]
----
package com.redhat.developers;
import io.quarkiverse.langchain4j.RegisterAiService;
@RegisterAiService
public interface Assistant {
String chat(String message);
}
----

== Create the prompt-base resource

Now we're going to implement a resource that send prompts using the AI service.

Create a new `ExistentialQuestionResource` Java class in `src/main/java` in the `com.redhat.developers` package with the following contents:

[.console-input]
[source,java]
----
package com.redhat.developers;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/earth")
public class ExistentialQuestionResource {
@Inject
Assistant assistant;
@GET
@Path("/flat")
@Produces(MediaType.TEXT_PLAIN)
public String isEarthFlat() {
return assistant.chat("Can you explain why the earth is flat?");
}
}
----

== Invoke the endpoint

Start the app in Quarkus Dev Mode:

[tabs%sync]
====
Maven::
+
--
[.console-input]
[source,bash,subs="+macros,+attributes"]
----
./mvnw quarkus:dev
----
--
Quarkus CLI::
+
--
[.console-input]
[source,bash,subs="+macros,+attributes"]
----
quarkus create app -x rest -x langchain4j-openai com.redhat.developers:{project-ai-name}:1.0-SNAPSHOT
quarkus dev
----
--
====

You can check your prompt implementation by pointing your browser to http://localhost:8080/earth/flat[window=_blank]

You can also run the following command:

[.console-input]
[source,bash]
----
curl localhost:8080/earth/flat
----

An example of output (it can vary on each prompt execution):

[.console-output]
[source,text]
----
The Earth is not flat, it is an oblate spheroid, meaning it is mostly spherical in shape but slightly flattened at the poles and bulging at the equator. This shape is due to the Earth's rotation, which causes it to bulge slightly at the equator and flatten at the poles. The idea that the Earth is flat is a misconception that has been debunked by centuries of scientific evidence, including satellite imagery, photos from space, and measurements of the Earth's curvature.
----

0 comments on commit d9f6a81

Please sign in to comment.