|
| 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.example.wikipedia; |
| 17 | + |
| 18 | +import com.embabel.agent.api.annotation.*; |
| 19 | +import com.embabel.agent.api.common.OperationContext; |
| 20 | +import com.embabel.agent.core.CoreToolGroups; |
| 21 | +import com.embabel.common.ai.model.BuildableLlmOptions; |
| 22 | +import com.embabel.common.ai.model.LlmOptions; |
| 23 | +import org.springframework.beans.factory.annotation.Value; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +record ResearchSubject(String name) { |
| 29 | +} |
| 30 | + |
| 31 | +record FocusAreas(List<FocusArea> focusAreas) {} |
| 32 | + |
| 33 | +record FocusArea(String topic) { |
| 34 | +} |
| 35 | + |
| 36 | +record ResearchReport(String subject, String summary) { |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Demonstrates an agent that interacts with the user |
| 41 | + */ |
| 42 | +@Agent(description = "Find information from wikipedia according to the user's request.") |
| 43 | +public class WikiAgent { |
| 44 | + |
| 45 | + private final int wordCount; |
| 46 | + private final BuildableLlmOptions llm; |
| 47 | + |
| 48 | + public WikiAgent( |
| 49 | + @Value("${wiki.wordcount:200}") int wordCount, |
| 50 | + @Value("${wiki.llm:gpt-4.1-nano}") String model) { |
| 51 | + this.wordCount = wordCount; |
| 52 | + this.llm = LlmOptions.fromModel(model); |
| 53 | + } |
| 54 | + |
| 55 | + @Action |
| 56 | + FocusAreas focusAreas(ResearchSubject researchSubject, OperationContext context) { |
| 57 | + return context.promptRunner() |
| 58 | + .withLlm(llm.withTemperature(.6)) |
| 59 | + .withToolGroup(CoreToolGroups.WEB) |
| 60 | + .createObject(""" |
| 61 | + What are some interesting topics to explore about %s? |
| 62 | + Please provide a list of focus areas, each as a separate line. |
| 63 | + Go beyond the obvious: emphasize quirky and lesser-known aspects. |
| 64 | + Use Wikipedia as the source of information. |
| 65 | + """.formatted(researchSubject.name()), FocusAreas.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Action |
| 69 | + FocusArea findFocusArea(ResearchSubject researchSubject, FocusAreas focusAreas) { |
| 70 | + return WaitFor.formSubmission( |
| 71 | + """ |
| 72 | + Great, there are lots of interesting things to explore about %s. |
| 73 | + Choices: |
| 74 | + %s |
| 75 | + Please select one of the topics above to focus on. |
| 76 | + """.formatted(researchSubject.name(), |
| 77 | + focusAreas.focusAreas().stream().map(FocusArea::topic) |
| 78 | + .collect(Collectors.joining("\n"))), |
| 79 | + FocusArea.class); |
| 80 | + } |
| 81 | + |
| 82 | + @Action |
| 83 | + @AchievesGoal(description = "Find information from wikipedia according to the user's request.", |
| 84 | + export = @Export(remote = true, name="wikiSearch", startingInputTypes = {ResearchSubject.class}) |
| 85 | + ) |
| 86 | + ResearchReport performResearch(ResearchSubject researchSubject, FocusArea focusArea, OperationContext context) { |
| 87 | + return context.promptRunner() |
| 88 | + .withLlm(llm.withTemperature(.8)) |
| 89 | + .withToolGroup(CoreToolGroups.WEB) |
| 90 | + .createObject(""" |
| 91 | + Research the following subject on Wikipedia, with the given focus area, and summarize it |
| 92 | + in %d words: |
| 93 | + Subject: %s |
| 94 | + Focus Area: %s |
| 95 | + """.formatted(wordCount, researchSubject.name(), focusArea.topic()), |
| 96 | + ResearchReport.class); |
| 97 | + } |
| 98 | +} |
0 commit comments