X

redhat-developer-demos github io

Bing Rank
Average Position of Bing Search Engine Ranking of related query such as 'Sales AI Agent', 'Coding AI Agent', etc.

Last Updated: 2025-04-15

Information

Requirements Basics Cloud Native Reactive AI "extensions=mailpit,mailer" package import import import import import import @ApplicationScoped public class EmailService class EmailService @Inject @Tool "send the given content by email" public void sendAnEmail(String content) public void sendAnEmail (String content) "Sending an email: " "sendMeALetter@quarkus.io" "A poem for you" "origin@quarkus.io" package import import import @RegisterAiService .class)public interface AssistantWithContext class public interface AssistantWithContext /** * Ask the LLM to create a poem about the given topic. * * @param topic the topic of the poem * @param lines the number of line of the poem * @return the poem */ @param @param @return @SystemMessage "You are a professional poet" @UserMessage "Write a poem about {topic}. The poem should be {lines} lines long. Then send this poem by email." String writeAPoem(String topic, int lines) writeAPoem (String topic, int lines) int package import import @Path "/email-me-a-poem" public class EmailMeAPoemResource class EmailMeAPoemResource private final public EmailMeAPoemResource(AssistantWithContext service) public EmailMeAPoemResource (AssistantWithContext service) this @GET public String emailMeAPoem() public emailMeAPoem () return "Quarkus" 4 -a Chains and Memory Embedding Documents Things become more interesting when you can bring the AI LLM into your application and get it to interact with specific functions you build for it. This section will use AI to trigger an email service from our local application. To do this, we’ll use LangChain4j’s concept of Agents and Tools. Agents operate by utilizing a language model to decipher a series of actions, unlike chains where actions are pre-programmed. Ie. agents leverage a language model as a cognitive engine to decide on the actions (tools) and their order. You can read more about this in the Quarkus LangChain4j Documentation Open a new terminal window, and make sure you’re at the root of your quarkus-langchain4j-app project, then run the following command to add emailing capabilities to our application: Maven Quarkus CLI Let’s create a class for our email service. Create a new EmailService Java class in src/main/java in the com.redhat.developers package with the following contents: Let’s create an interface for our AI service, but with SystemMessage and UserMessage this time.SystemMessage gives context to the AI Model.In this case, we tell it that it should craft a message as if it is written by a professional poet.The UserMessage is the actual instruction/question we’re sending to the AI model.As you can see in the example below,you can format and parameterize the UserMessage, translating structured content to text and vice-versa. Create a new AssistantWithContext Java interface in src/main/java in the com.redhat.developers package with the following contents: Note that this assistant references the email service as a tool. Now we create a resource that builds the interaction and calls the service with the required parameters (topic and number of lines). Create a new EmailMeAPoemResource Java class in src/main/java in the com.redhat.developers package with the following contents: Tool calling is not supported with the OpenAI demo key so we will need toeither use a real API key, or use a local model that supports tools..If you want to use OpenAI’s ChatGPT, you can create and fund an account at OpenAI and then set the openai-api-key to your key. We will use a local (free) open source model served with Ollama instead.To do this, you will need to download and install Ollama.Once that’s done, you will need to download a model that supports tool calling, such as granite3.1-dense:2b. To do so, execute the command: Update the following properties in your application.properties Modify the application.properties as below: Make sure your Quarkus Dev mode is still running. It should have reloaded with the new configuration. Because we haven’t configured the local email service, Quarkus will also have started a Dev Service to instantiate and configure a local email service for you (in dev mode only!). You can check it running: And you should see something like this: Which means that you have an email service up and running. You can check your prompt implementation by pointing your browser to http://localhost:8080/email-me-a-poem You can also run the following command: An example of output (will vary on each prompt execution): If you have a tool calling model configured, you can check your inbox for the actual email: First, open the DevUI and click on the Mailpit arrow. Now you can see the email that was sent:

Prompts

Reviews

Tags

Write Your Review

Detailed Ratings

ALL
Correctness
Helpfulness
Interesting
Upload Pictures and Videos