|
| 1 | +package tools; |
| 2 | + |
| 3 | +import dev.langchain4j.data.message.SystemMessage; |
| 4 | +import dev.langchain4j.data.message.UserMessage; |
| 5 | +import dev.langchain4j.model.anthropic.AnthropicChatModel; |
| 6 | +import dev.langchain4j.model.chat.ChatLanguageModel; |
| 7 | +import dev.langchain4j.model.chat.response.ChatResponse; |
| 8 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import java.time.Duration; |
| 13 | + |
| 14 | +/** |
| 15 | + * Access Anthropic Claude models with LangChain4j |
| 16 | + * <br> |
| 17 | + * Doc: |
| 18 | + * https://docs.anthropic.com/claude/reference/getting-started-with-the-api |
| 19 | + * https://github.com/langchain4j/langchain4j |
| 20 | + */ |
| 21 | +public class AnthropicCompletions { |
| 22 | + private static final Logger LOGGER = LoggerFactory.getLogger(AnthropicCompletions.class); |
| 23 | + public static final String API_KEY = "***"; |
| 24 | + public static final String MODEL_NAME = "claude-3-7-sonnet-20250219"; |
| 25 | + |
| 26 | + private final ChatLanguageModel model; |
| 27 | + private final String moviePlotContext; |
| 28 | + |
| 29 | + public AnthropicCompletions() { |
| 30 | + this(CompletionsUtil.DEFAULT_TRANSLATOR_SYSTEM_MESSAGE); |
| 31 | + } |
| 32 | + |
| 33 | + private AnthropicCompletions(String moviePlotContext) { |
| 34 | + this.model = AnthropicChatModel.builder() |
| 35 | + .apiKey(API_KEY) |
| 36 | + .modelName(MODEL_NAME) |
| 37 | + .temperature(0.2) |
| 38 | + .cacheSystemMessages(true) |
| 39 | + .cacheTools(true) |
| 40 | + .maxTokens(8192) |
| 41 | + .maxRetries(10) |
| 42 | + .timeout(Duration.ofMinutes(5)) |
| 43 | + .build(); |
| 44 | + this.moviePlotContext = moviePlotContext; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new AnthropicCompletions instance with movie context information. |
| 49 | + * |
| 50 | + * @param movieTitle The title of the movie |
| 51 | + * @param movieReleaseYear The release year of the movie |
| 52 | + * @return A new AnthropicCompletions instance with the resolved movie plot context |
| 53 | + */ |
| 54 | + public static AnthropicCompletions withContext(String movieTitle, int movieReleaseYear) { |
| 55 | + ChatLanguageModel contextModel = AnthropicChatModel.builder() |
| 56 | + .apiKey(API_KEY) |
| 57 | + .modelName(MODEL_NAME) |
| 58 | + .temperature(0.1) |
| 59 | + .maxTokens(1024) |
| 60 | + .build(); |
| 61 | + |
| 62 | + String contextPrompt = CompletionsUtil.createMovieContextPrompt(movieTitle, movieReleaseYear); |
| 63 | + ChatResponse response = contextModel.chat( |
| 64 | + UserMessage.from(contextPrompt) |
| 65 | + ); |
| 66 | + |
| 67 | + String movieContext = response.aiMessage().text(); |
| 68 | + LOGGER.info("Generated movie context with: {} for: {} ({}):\n{}", MODEL_NAME, movieTitle, movieReleaseYear, movieContext); |
| 69 | + |
| 70 | + return new AnthropicCompletions(movieContext); |
| 71 | + } |
| 72 | + |
| 73 | + public ImmutablePair<String, Integer> runCompletions(String prompt) { |
| 74 | + ChatResponse response = model.chat( |
| 75 | + SystemMessage.systemMessage(moviePlotContext), |
| 76 | + UserMessage.from(prompt) |
| 77 | + ); |
| 78 | + return new ImmutablePair<>(response.aiMessage().text(), response.tokenUsage().totalTokenCount()); |
| 79 | + } |
| 80 | + |
| 81 | + public static void main(String[] args) { |
| 82 | + String toTranslate = CompletionsUtil.createTranslationPrompt("This is fun.", "English", "German"); |
| 83 | + |
| 84 | + ImmutablePair<String, Integer> result = new AnthropicCompletions().runCompletions(toTranslate); |
| 85 | + CompletionsUtil.logCompletionResult(result.getLeft(), result.getRight(), "Translation"); |
| 86 | + |
| 87 | + ImmutablePair<String, Integer> resultWithContext = AnthropicCompletions.withContext("The Hangover", 2009).runCompletions(toTranslate); |
| 88 | + CompletionsUtil.logCompletionResult(resultWithContext.getLeft(), resultWithContext.getRight(), "Translation with context"); |
| 89 | + } |
| 90 | + |
| 91 | + public String toString() { |
| 92 | + return "Anthropic: " + MODEL_NAME; |
| 93 | + } |
| 94 | +} |
0 commit comments