Skip to content

Commit

Permalink
Added support for Metrics and Telemetry tracing using Microprofile Me…
Browse files Browse the repository at this point in the history
…trics & Microprofile Telemetry respectively.
  • Loading branch information
Buhake Sindi committed Dec 19, 2024
1 parent 95d65d2 commit 18d4262
Show file tree
Hide file tree
Showing 14 changed files with 743 additions and 80 deletions.
138 changes: 101 additions & 37 deletions examples/liberty-car-booking/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,104 @@
<scope>provided</scope>
</dependency>

<!--
<dependency>
<groupId>io.smallrye.llm.liberty-bundle</groupId>
<artifactId>smallrye-llm-langchain4j-features-bom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
-->
<!--
<dependency>
<groupId>io.smallrye.llm</groupId>
<artifactId>smallrye-llm-langchain4j-buildcompatible-extension</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
-->
<!-- https://mvnrepository.com/artifact/ai.djl.huggingface/tokenizers -->
<dependency>
<groupId>ai.djl.huggingface</groupId>
<artifactId>tokenizers</artifactId>
<version>0.30.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-api -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>1.44.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
<version>2.10.0</version>
<scope>provided</scope>
</dependency>

<dependencies>
<!-- https://mvnrepository.com/artifact/jakarta.platform/jakarta.jakartaee-api -->
<!--
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<scope>provided</scope>
</dependency>
-->
<dependency>
<groupId>io.smallrye.llm</groupId>
<artifactId>smallrye-llm-langchain4j-config-mpconfig</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.smallrye.llm</groupId>
<artifactId>smallrye-llm-langchain4j-portable-extension</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>io.smallrye.llm</groupId>
<artifactId>smallrye-llm-langchain4j-telemetry</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>${dev.langchain4j.version}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-hugging-face</artifactId>
<version>${dev.langchain4j.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/dev.langchain4j/langchain4j-azure-open-ai -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-azure-open-ai</artifactId>
<version>${dev.langchain4j.version}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>${dev.langchain4j.version}</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId>
<version>${dev.langchain4j.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/ai.djl.huggingface/tokenizers -->
<dependency>
<groupId>ai.djl.huggingface</groupId>
<artifactId>tokenizers</artifactId>
<version>0.30.0</version>
</dependency>

<!-- SLF4J messages from langchain4j and Azure OpenAI SDK backed by JUL -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<scope>runtime</scope>
<version>2.0.9</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<type>pom</type>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/io.opentelemetry.instrumentation/opentelemetry-instrumentation-annotations -->
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
</dependency>

<dependency>
<groupId>io.smallrye.llm</groupId>
Expand Down Expand Up @@ -113,6 +172,11 @@
</dependency>
-->

<dependency>
<groupId>io.smallrye.llm</groupId>
<artifactId>smallrye-llm-langchain4j-telemetry</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.jefrajames.booking;

import java.time.LocalDate;
import java.util.Objects;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
//@Data
//@NoArgsConstructor
//@AllArgsConstructor
public class Booking {

private String bookingNumber;
Expand All @@ -18,4 +15,155 @@ public class Booking {
private boolean canceled = false;
private String carModel;

/**
*
*/
public Booking() {
super();
//TODO Auto-generated constructor stub
}

/**
* @param bookingNumber
* @param start
* @param end
* @param customer
* @param canceled
* @param carModel
*/
public Booking(String bookingNumber, LocalDate start, LocalDate end, Customer customer, boolean canceled,
String carModel) {
super();
this.bookingNumber = bookingNumber;
this.start = start;
this.end = end;
this.customer = customer;
this.canceled = canceled;
this.carModel = carModel;
}

/**
* @return the bookingNumber
*/
public String getBookingNumber() {
return bookingNumber;
}

/**
* @param bookingNumber the bookingNumber to set
*/
public void setBookingNumber(String bookingNumber) {
this.bookingNumber = bookingNumber;
}

/**
* @return the start
*/
public LocalDate getStart() {
return start;
}

/**
* @param start the start to set
*/
public void setStart(LocalDate start) {
this.start = start;
}

/**
* @return the end
*/
public LocalDate getEnd() {
return end;
}

/**
* @param end the end to set
*/
public void setEnd(LocalDate end) {
this.end = end;
}

/**
* @return the customer
*/
public Customer getCustomer() {
return customer;
}

/**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}

/**
* @return the canceled
*/
public boolean isCanceled() {
return canceled;
}

/**
* @param canceled the canceled to set
*/
public void setCanceled(boolean canceled) {
this.canceled = canceled;
}

/**
* @return the carModel
*/
public String getCarModel() {
return carModel;
}

/**
* @param carModel the carModel to set
*/
public void setCarModel(String carModel) {
this.carModel = carModel;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(bookingNumber, canceled, carModel, customer, end, start);
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Booking other = (Booking) obj;
return Objects.equals(bookingNumber, other.bookingNumber) && canceled == other.canceled
&& Objects.equals(carModel, other.carModel) && Objects.equals(customer, other.customer)
&& Objects.equals(end, other.end) && Objects.equals(start, other.start);
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Booking [bookingNumber=" + bookingNumber + ", start=" + start + ", end=" + end + ", customer="
+ customer + ", canceled=" + canceled + ", carModel=" + carModel + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import jakarta.enterprise.context.ApplicationScoped;

import org.jboss.logging.Logger;

import dev.langchain4j.agent.tool.Tool;
import lombok.extern.java.Log;

@ApplicationScoped
@Log
//@Log
public class BookingService {

private static final Logger LOGGER = Logger.getLogger(BookingService.class.getName());

// Pseudo database
private static final Map<String, Booking> BOOKINGS = new HashMap<>();
static {
Expand Down Expand Up @@ -44,14 +47,14 @@ private Booking checkBookingExists(String bookingNumber, String name, String sur

@Tool("Get booking details given a booking number and customer name and surname")
public Booking getBookingDetails(String bookingNumber, String name, String surname) {
log.info("DEMO: Calling Tool-getBookingDetails: " + bookingNumber + " and customer: "
LOGGER.info("DEMO: Calling Tool-getBookingDetails: " + bookingNumber + " and customer: "
+ name + " " + surname);
return checkBookingExists(bookingNumber, name, surname);
}

@Tool("Get all booking ids for a customer given his name and surname")
public List<String> getBookingsForCustomer(String name, String surname) {
log.info("DEMO: Calling Tool-getBookingsForCustomer: " + name + " " + surname);
LOGGER.info("DEMO: Calling Tool-getBookingsForCustomer: " + name + " " + surname);
Customer customer = new Customer(name, surname);
return BOOKINGS.values()
.stream()
Expand All @@ -77,7 +80,7 @@ public void checkCancelPolicy(Booking booking) {

@Tool("Cancel a booking given its booking number and customer name and surname")
public Booking cancelBooking(String bookingNumber, String name, String surname) {
log.info("DEMO: Calling Tool-cancelBooking " + bookingNumber + " for customer: " + name
LOGGER.info("DEMO: Calling Tool-cancelBooking " + bookingNumber + " for customer: " + name
+ " " + surname);

Booking booking = checkBookingExists(bookingNumber, name, surname);
Expand Down
Loading

0 comments on commit 18d4262

Please sign in to comment.