-
Notifications
You must be signed in to change notification settings - Fork 182
/
OtherServiceExamples.java
368 lines (251 loc) · 12.9 KB
/
OtherServiceExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.input.structured.StructuredPrompt;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.structured.Description;
import dev.langchain4j.service.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.function.Function;
import static java.util.Arrays.asList;
public class OtherServiceExamples {
static ChatLanguageModel chatLanguageModel = OpenAiChatModel.withApiKey(ApiKeys.OPENAI_API_KEY);
static class Sentiment_Extracting_AI_Service_Example {
enum Sentiment {
POSITIVE, NEUTRAL, NEGATIVE;
}
interface SentimentAnalyzer {
@UserMessage("Analyze sentiment of {{it}}")
Sentiment analyzeSentimentOf(String text);
@UserMessage("Does {{it}} have a positive sentiment?")
boolean isPositive(String text);
}
public static void main(String[] args) {
SentimentAnalyzer sentimentAnalyzer = AiServices.create(SentimentAnalyzer.class, chatLanguageModel);
Sentiment sentiment = sentimentAnalyzer.analyzeSentimentOf("It is good!");
System.out.println(sentiment); // POSITIVE
boolean positive = sentimentAnalyzer.isPositive("It is bad!");
System.out.println(positive); // false
}
}
static class Number_Extracting_AI_Service_Example {
interface NumberExtractor {
@UserMessage("Extract number from {{it}}")
int extractInt(String text);
@UserMessage("Extract number from {{it}}")
long extractLong(String text);
@UserMessage("Extract number from {{it}}")
BigInteger extractBigInteger(String text);
@UserMessage("Extract number from {{it}}")
float extractFloat(String text);
@UserMessage("Extract number from {{it}}")
double extractDouble(String text);
@UserMessage("Extract number from {{it}}")
BigDecimal extractBigDecimal(String text);
}
public static void main(String[] args) {
NumberExtractor extractor = AiServices.create(NumberExtractor.class, chatLanguageModel);
String text = "After countless millennia of computation, the supercomputer Deep Thought finally announced " +
"that the answer to the ultimate question of life, the universe, and everything was forty two.";
int intNumber = extractor.extractInt(text);
System.out.println(intNumber); // 42
long longNumber = extractor.extractLong(text);
System.out.println(longNumber); // 42
BigInteger bigIntegerNumber = extractor.extractBigInteger(text);
System.out.println(bigIntegerNumber); // 42
float floatNumber = extractor.extractFloat(text);
System.out.println(floatNumber); // 42.0
double doubleNumber = extractor.extractDouble(text);
System.out.println(doubleNumber); // 42.0
BigDecimal bigDecimalNumber = extractor.extractBigDecimal(text);
System.out.println(bigDecimalNumber); // 42.0
}
}
static class Date_and_Time_Extracting_AI_Service_Example {
interface DateTimeExtractor {
@UserMessage("Extract date from {{it}}")
LocalDate extractDateFrom(String text);
@UserMessage("Extract time from {{it}}")
LocalTime extractTimeFrom(String text);
@UserMessage("Extract date and time from {{it}}")
LocalDateTime extractDateTimeFrom(String text);
}
public static void main(String[] args) {
DateTimeExtractor extractor = AiServices.create(DateTimeExtractor.class, chatLanguageModel);
String text = "The tranquility pervaded the evening of 1968, just fifteen minutes shy of midnight," +
" following the celebrations of Independence Day.";
LocalDate date = extractor.extractDateFrom(text);
System.out.println(date); // 1968-07-04
LocalTime time = extractor.extractTimeFrom(text);
System.out.println(time); // 23:45
LocalDateTime dateTime = extractor.extractDateTimeFrom(text);
System.out.println(dateTime); // 1968-07-04T23:45
}
}
static class POJO_Extracting_AI_Service_Example {
static class Person {
private String firstName;
private String lastName;
private LocalDate birthDate;
@Override
public String toString() {
return "Person {" +
" firstName = \"" + firstName + "\"" +
", lastName = \"" + lastName + "\"" +
", birthDate = " + birthDate +
" }";
}
}
interface PersonExtractor {
@UserMessage("Extract information about a person from {{it}}")
Person extractPersonFrom(String text);
}
public static void main(String[] args) {
ChatLanguageModel chatLanguageModel = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
// When extracting POJOs with the LLM that supports the "json mode" feature
// (e.g., OpenAI, Azure OpenAI, Ollama, etc.), it is advisable to use it to get more reliable results.
// When using this feature, LLM will be forced to output a valid JSON.
// Please note that this feature is not (yet) supported when using "demo" key.
.responseFormat("json_object")
.build();
PersonExtractor extractor = AiServices.create(PersonExtractor.class, chatLanguageModel);
String text = "In 1968, amidst the fading echoes of Independence Day, "
+ "a child named John arrived under the calm evening sky. "
+ "This newborn, bearing the surname Doe, marked the start of a new journey.";
Person person = extractor.extractPersonFrom(text);
System.out.println(person); // Person { firstName = "John", lastName = "Doe", birthDate = 1968-07-04 }
}
}
static class POJO_With_Descriptions_Extracting_AI_Service_Example {
static class Recipe {
@Description("short title, 3 words maximum")
private String title;
@Description("short description, 2 sentences maximum")
private String description;
@Description("each step should be described in 4 words, steps should rhyme")
private List<String> steps;
private Integer preparationTimeMinutes;
@Override
public String toString() {
return "Recipe {" +
" title = \"" + title + "\"" +
", description = \"" + description + "\"" +
", steps = " + steps +
", preparationTimeMinutes = " + preparationTimeMinutes +
" }";
}
}
@StructuredPrompt("Create a recipe of a {{dish}} that can be prepared using only {{ingredients}}")
static class CreateRecipePrompt {
private String dish;
private List<String> ingredients;
}
interface Chef {
Recipe createRecipeFrom(String... ingredients);
Recipe createRecipe(CreateRecipePrompt prompt);
}
public static void main(String[] args) {
Chef chef = AiServices.create(Chef.class, chatLanguageModel);
Recipe recipe = chef.createRecipeFrom("cucumber", "tomato", "feta", "onion", "olives");
System.out.println(recipe);
// Recipe {
// title = "Greek Salad",
// description = "A refreshing mix of veggies and feta cheese in a zesty dressing.",
// steps = [
// "Chop cucumber and tomato",
// "Add onion and olives",
// "Crumble feta on top",
// "Drizzle with dressing and enjoy!"
// ],
// preparationTimeMinutes = 10
// }
CreateRecipePrompt prompt = new CreateRecipePrompt();
prompt.dish = "salad";
prompt.ingredients = asList("cucumber", "tomato", "feta", "onion", "olives");
Recipe anotherRecipe = chef.createRecipe(prompt);
System.out.println(anotherRecipe);
// Recipe ...
}
}
static class AI_Service_with_System_Message_Example {
interface Chef {
@SystemMessage("You are a professional chef. You are friendly, polite and concise.")
String answer(String question);
}
public static void main(String[] args) {
Chef chef = AiServices.create(Chef.class, chatLanguageModel);
String answer = chef.answer("How long should I grill chicken?");
System.out.println(answer); // Grilling chicken usually takes around 10-15 minutes per side, depending on ...
}
}
static class AI_Service_with_System_and_User_Messages_Example {
interface TextUtils {
@SystemMessage("You are a professional translator into {{language}}")
@UserMessage("Translate the following text: {{text}}")
String translate(@V("text") String text, @V("language") String language);
@SystemMessage("Summarize every message from user in {{n}} bullet points. Provide only bullet points.")
List<String> summarize(@UserMessage String text, @V("n") int n);
}
public static void main(String[] args) {
TextUtils utils = AiServices.create(TextUtils.class, chatLanguageModel);
String translation = utils.translate("Hello, how are you?", "italian");
System.out.println(translation); // Ciao, come stai?
String text = "AI, or artificial intelligence, is a branch of computer science that aims to create " +
"machines that mimic human intelligence. This can range from simple tasks such as recognizing " +
"patterns or speech to more complex tasks like making decisions or predictions.";
List<String> bulletPoints = utils.summarize(text, 3);
System.out.println(bulletPoints);
// [
// "- AI is a branch of computer science",
// "- It aims to create machines that mimic human intelligence",
// "- It can perform simple or complex tasks"
// ]
}
}
static class AI_Service_with_System_and_User_Messages_loaded_from_resources_Example {
interface TextUtils {
@SystemMessage(fromResource = "/translator-system-prompt-template.txt")
@UserMessage(fromResource = "/translator-user-prompt-template.txt")
String translate(@V("text") String text, @V("language") String language);
}
public static void main(String[] args) {
TextUtils utils = AiServices.create(TextUtils.class, chatLanguageModel);
String translation = utils.translate("Hello, how are you?", "italian");
System.out.println(translation); // Ciao, come stai?
}
}
static class AI_Service_with_UserName_Example {
interface Assistant {
String chat(@UserName String name, @UserMessage String message);
}
public static void main(String[] args) {
Assistant assistant = AiServices.create(Assistant.class, chatLanguageModel);
String answer = assistant.chat("Klaus", "Hi, tell me my name if you see it.");
System.out.println(answer); // Hello! Your name is Klaus. How can I assist you today?
}
}
static class AI_Service_with_Dynamic_System_Message_Example {
interface Assistant {
String chat(@MemoryId String memoryId, @UserMessage String userMessage);
}
public static void main(String[] args) {
Function<Object, String> systemMessageProvider = (memoryId) -> {
if (memoryId.equals("1")) {
return "You are a helpful assistant. The user prefers to be called 'Your Majesty'.";
} else {
return "You are a helpful assistant.";
}
};
Assistant assistant = AiServices.builder(Assistant.class)
.chatLanguageModel(chatLanguageModel)
.systemMessageProvider(systemMessageProvider)
.build();
System.out.println(assistant.chat("1", "Hi")); // Hello, Your Majesty! How may I assist you today?
System.out.println(assistant.chat("2", "Hi")); // Hello! How can I assist you today?
}
}
}