|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 Embabel Software, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.embabel.database.agent; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.beans.factory.annotation.Value; |
| 26 | + |
| 27 | +import com.embabel.agent.api.annotation.AchievesGoal; |
| 28 | +import com.embabel.agent.api.annotation.Action; |
| 29 | +import com.embabel.agent.api.annotation.Agent; |
| 30 | +import com.embabel.agent.api.common.OperationContext; |
| 31 | +import com.embabel.agent.domain.io.UserInput; |
| 32 | +import com.embabel.common.ai.model.PerTokenPricingModel; |
| 33 | +import com.embabel.common.ai.model.PricingModel; |
| 34 | +import com.embabel.database.agent.util.TagParser; |
| 35 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 36 | + |
| 37 | +record TagList(List<String> tags) { } |
| 38 | + |
| 39 | +@Agent(name="ModelSuggestionAgent", description = "Suggest models based on user criteria") |
| 40 | +public class ModelSuggestionAgent { |
| 41 | + |
| 42 | + private static Log logger = LogFactory.getLog(ModelSuggestionAgent.class); |
| 43 | + |
| 44 | + @Autowired |
| 45 | + TagParser tagParser; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + ObjectMapper objectMapper; |
| 49 | + |
| 50 | + @Value("${embabel.models.defaultLlm:llama3.1:8b}") |
| 51 | + String modelName; |
| 52 | + |
| 53 | + @AchievesGoal( |
| 54 | + description="Retrieves model suggestions based on the entered criteria" |
| 55 | + ) |
| 56 | + @Action |
| 57 | + public ListModelMetadata getModelSuggestions(TagList tags, OperationContext operationContext) { |
| 58 | + |
| 59 | + var prompt = """ |
| 60 | + Review the following request from the user and return a list of suggested models. |
| 61 | + """; |
| 62 | + |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @Action |
| 68 | + public TagList getSuggestedTagList(UserInput userInput, OperationContext operationContext) { |
| 69 | + //retrieves the tags available |
| 70 | + //uses an LLM to take the "criteria" from the user and build a tag option |
| 71 | + List<Map<String,Object>> tags = tagParser.getTasks(objectMapper,TagParser.RESOURCE_LOCATION); |
| 72 | + //convert to just a list of strings |
| 73 | + List<String> tagNames = tags.stream() |
| 74 | + .map(map -> (String) map.get("tag")) |
| 75 | + .collect(Collectors.toList()); |
| 76 | + //set up the prompt |
| 77 | + var prompt = """ |
| 78 | + Review the following request from the user and return a list of tag names that meet |
| 79 | + the users requested criteria. Respond only with a list of tags. |
| 80 | +
|
| 81 | + Criteria = %s |
| 82 | +
|
| 83 | + Tag Options = %s |
| 84 | + """.formatted(userInput.getContent(),tagNames); |
| 85 | + logger.debug(prompt);//quick dump of the prompot |
| 86 | + return operationContext.ai().withLlm(modelName).createObject(prompt, TagList.class); |
| 87 | + } |
| 88 | +} |
0 commit comments