diff --git a/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMServiceType.java b/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMServiceType.java index 8b41cdf30fa8..e71589f300ca 100644 --- a/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMServiceType.java +++ b/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMServiceType.java @@ -1,6 +1,6 @@ package de.tum.cit.aet.artemis.core.domain; public enum LLMServiceType { - ATHENA, IRIS_CODE_FEEDBACK, IRIS_CHAT_COURSE_MESSAGE, IRIS_CHAT_EXERCISE_MESSAGE, IRIS_INTERACTION_SUGGESTION, IRIS_CHAT_LECTURE_MESSAGE, IRIS_COMPETENCY_GENERATION, - IRIS_CITATION_PIPELINE, NOT_SET + ATHENA_PRELIMINARY_FEEDBACK, ATHENA_FEEDBACK_SUGGESTION, IRIS_CODE_FEEDBACK, IRIS_CHAT_COURSE_MESSAGE, IRIS_CHAT_EXERCISE_MESSAGE, IRIS_INTERACTION_SUGGESTION, + IRIS_CHAT_LECTURE_MESSAGE, IRIS_COMPETENCY_GENERATION, IRIS_CITATION_PIPELINE, NOT_SET } diff --git a/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMTokenUsage.java b/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMTokenUsage.java index 6646810c2367..cd26f899113a 100644 --- a/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMTokenUsage.java +++ b/src/main/java/de/tum/cit/aet/artemis/core/domain/LLMTokenUsage.java @@ -38,15 +38,18 @@ public class LLMTokenUsage extends DomainObject { @Column(name = "model") private String model; - @Column(name = "cost_per_token") - private double cost_per_token; - @Column(name = "num_input_tokens") private int num_input_tokens; + @Column(name = "cost_per_input_token") + private float cost_per_input_token; + @Column(name = "num_output_tokens") private int num_output_tokens; + @Column(name = "cost_per_output_token") + private float cost_per_output_token; + @Nullable @ManyToOne @JsonIgnore @@ -66,6 +69,9 @@ public class LLMTokenUsage extends DomainObject { @Column(name = "timestamp") private ZonedDateTime timestamp = ZonedDateTime.now(); + @Column(name = "trace_id") + private Long traceId; + @Nullable @ManyToOne @JsonIgnore @@ -88,12 +94,20 @@ public void setModel(String model) { this.model = model; } - public double getCost_per_token() { - return cost_per_token; + public float getCost_per_input_token() { + return cost_per_input_token; + } + + public void setCost_per_input_token(float cost_per_input_token) { + this.cost_per_input_token = cost_per_input_token; + } + + public float getCost_per_output_token() { + return cost_per_output_token; } - public void setCost_per_token(double cost_per_token) { - this.cost_per_token = cost_per_token; + public void setCost_per_output_token(float cost_per_output_token) { + this.cost_per_output_token = cost_per_output_token; } public int getNum_input_tokens() { @@ -144,6 +158,14 @@ public void setTimestamp(ZonedDateTime timestamp) { this.timestamp = timestamp; } + public Long getTraceId() { + return traceId; + } + + public void setTraceId(Long traceId) { + this.traceId = traceId; + } + public IrisMessage getIrisMessage() { return irisMessage; } @@ -151,4 +173,11 @@ public IrisMessage getIrisMessage() { public void setIrisMessage(IrisMessage message) { this.irisMessage = message; } + + @Override + public String toString() { + return "LLMTokenUsage{" + "serviceType=" + serviceType + ", model=" + model + ", num_input_tokens=" + num_input_tokens + ", cost_per_input_token=" + cost_per_input_token + + ", num_output_tokens=" + num_output_tokens + ", cost_per_output_token=" + cost_per_output_token + ", course=" + course + ", exercise=" + exercise + ", userId=" + + userId + ", timestamp=" + timestamp + ", trace_id=" + traceId + ", irisMessage=" + irisMessage + '}'; + } } diff --git a/src/main/java/de/tum/cit/aet/artemis/core/service/LLMTokenUsageService.java b/src/main/java/de/tum/cit/aet/artemis/core/service/LLMTokenUsageService.java index 2a5b9204ed48..3446c61117e3 100644 --- a/src/main/java/de/tum/cit/aet/artemis/core/service/LLMTokenUsageService.java +++ b/src/main/java/de/tum/cit/aet/artemis/core/service/LLMTokenUsageService.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.UUID; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @@ -44,6 +45,12 @@ public LLMTokenUsageService(LLMTokenUsageRepository llmTokenUsageRepository) { public List saveTokenUsage(IrisMessage message, Exercise exercise, User user, Course course, List tokens) { List tokenUsages = new ArrayList<>(); + + // Combine current time and UUID to create a unique traceId + long timestamp = System.currentTimeMillis(); + long uuidComponent = UUID.randomUUID().getLeastSignificantBits() & Long.MAX_VALUE; + Long traceId = timestamp + uuidComponent; + for (PyrisLLMCostDTO cost : tokens) { LLMTokenUsage llmTokenUsage = new LLMTokenUsage(); if (message != null) { @@ -57,8 +64,11 @@ public List saveTokenUsage(IrisMessage message, Exercise exercise } llmTokenUsage.setCourse(course); llmTokenUsage.setNum_input_tokens(cost.num_input_tokens()); + llmTokenUsage.setCost_per_input_token(cost.cost_per_input_token()); llmTokenUsage.setNum_output_tokens(cost.num_output_tokens()); + llmTokenUsage.setCost_per_output_token(cost.cost_per_output_token()); llmTokenUsage.setModel(cost.model_info()); + llmTokenUsage.setTraceId(traceId); tokenUsages.add(llmTokenUsageRepository.save(llmTokenUsage)); } return tokenUsages; diff --git a/src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/dto/data/PyrisLLMCostDTO.java b/src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/dto/data/PyrisLLMCostDTO.java index 1fa344b3ebc1..13fd40d84bf1 100644 --- a/src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/dto/data/PyrisLLMCostDTO.java +++ b/src/main/java/de/tum/cit/aet/artemis/iris/service/pyris/dto/data/PyrisLLMCostDTO.java @@ -2,5 +2,5 @@ import de.tum.cit.aet.artemis.core.domain.LLMServiceType; -public record PyrisLLMCostDTO(String model_info, int num_input_tokens, int num_output_tokens, LLMServiceType pipeline) { +public record PyrisLLMCostDTO(String model_info, int num_input_tokens, float cost_per_input_token, int num_output_tokens, float cost_per_output_token, LLMServiceType pipeline) { } diff --git a/src/main/resources/config/liquibase/changelog/20241011140701_changelog.xml b/src/main/resources/config/liquibase/changelog/20241012125003_changelog.xml similarity index 89% rename from src/main/resources/config/liquibase/changelog/20241011140701_changelog.xml rename to src/main/resources/config/liquibase/changelog/20241012125003_changelog.xml index 4088f2b05a95..fcb5bb25ac22 100644 --- a/src/main/resources/config/liquibase/changelog/20241011140701_changelog.xml +++ b/src/main/resources/config/liquibase/changelog/20241012125003_changelog.xml @@ -5,20 +5,22 @@ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.29.xsd" objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS"> - + - + + + - +