Skip to content

Commit d58093b

Browse files
committed
docs: Add an example in the JVM sample
1 parent cfc9aee commit d58093b

File tree

4 files changed

+86
-48
lines changed

4 files changed

+86
-48
lines changed

sample/jvm/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,23 @@ This endpoint generates text based on the provided prompt.
2323

2424
##### Parameters:
2525

26-
- input: The prompt for generating text.
26+
- `input`: The prompt for generating text.
2727

2828
##### Example:
2929

3030
`GET http://localhost:8080/api/ychat/completion?input="What is 1 + 1?"`
31+
32+
### Chat Completions Endpoint
33+
34+
This endpoint generates text based on the provided prompt and a specified topic. The generated text will be related to the topic provided.
35+
36+
##### Endpoint: http://localhost:[port_number]/api/ychat/chat-completions
37+
38+
##### Parameters:
39+
40+
- `input`: The prompt for generating text.
41+
- `topic`: The topic to limit the response to.
42+
43+
##### Example:
44+
45+
`GET http://localhost:8080/api/ychat/chat-completions?input="Tell me an exercise plan"&topic=fitness`

sample/jvm/src/main/java/co/yml/ychat/jvm/controller/YChatController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,35 @@
66
import org.springframework.web.bind.annotation.RequestMapping;
77
import org.springframework.web.bind.annotation.RequestParam;
88
import org.springframework.web.bind.annotation.RestController;
9-
import co.yml.ychat.jvm.services.CompletionService;
9+
import co.yml.ychat.jvm.services.YChatService;
1010

1111
@RestController
1212
@RequestMapping("api/ychat")
1313
public class YChatController {
1414

1515
@Autowired
16-
private CompletionService completionService;
16+
private YChatService YChatService;
1717

1818
@GetMapping("completion")
1919
public ResponseEntity<String> completion(
2020
@RequestParam(value = "input", defaultValue = Defaults.COMPLETION_INPUT) String input
2121
) throws Exception {
22-
String result = completionService.getCompletionAnswer(input);
22+
String result = YChatService.getCompletionAnswer(input);
23+
return ResponseEntity.ok(result);
24+
}
25+
26+
@GetMapping("chat-completions")
27+
public ResponseEntity<String> chatCompletions(
28+
@RequestParam(value = "input", defaultValue = Defaults.CHAT_COMPLETION_INPUT) String input,
29+
@RequestParam(value = "topic", defaultValue = Defaults.CHAT_COMPLETION_TOPIC) String topic
30+
) throws Exception {
31+
String result = YChatService.getChatCompletionsAnswer(input, topic);
2332
return ResponseEntity.ok(result);
2433
}
2534

2635
private static class Defaults {
2736
static final String COMPLETION_INPUT = "Say this is a test.";
37+
static final String CHAT_COMPLETION_INPUT = "Tell me one strength exercise";
38+
static final String CHAT_COMPLETION_TOPIC = "fitness";
2839
}
2940
}

sample/jvm/src/main/java/co/yml/ychat/jvm/services/CompletionService.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package co.yml.ychat.jvm.services;
2+
3+
import co.yml.ychat.domain.model.ChatMessage;
4+
import java.util.List;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
import java.util.concurrent.CompletableFuture;
9+
import co.yml.ychat.YChat;
10+
11+
@Service
12+
public class YChatService {
13+
14+
@Autowired
15+
private YChat ychat;
16+
17+
private static final int MAX_TOKENS = 512;
18+
19+
public String getCompletionAnswer(String input) throws Exception {
20+
final CompletableFuture<String> future = new CompletableFuture<>();
21+
ychat.completion()
22+
.setMaxTokens(MAX_TOKENS)
23+
.setInput(input)
24+
.execute(new CompletionCallbackResult<>(future));
25+
return future.get();
26+
}
27+
28+
public String getChatCompletionsAnswer(String input, String topic) throws Exception {
29+
final CompletableFuture<List<ChatMessage>> future = new CompletableFuture<>();
30+
String content = "You are a helpful assistant the only answer questions related to " + topic;
31+
ychat.chatCompletions()
32+
.setMaxTokens(MAX_TOKENS)
33+
.addMessage("system", content)
34+
.execute(input, new CompletionCallbackResult<>(future));
35+
return future.get().get(0).getContent();
36+
}
37+
38+
private static class CompletionCallbackResult<T> implements YChat.Callback<T> {
39+
40+
private final CompletableFuture<T> future;
41+
42+
CompletionCallbackResult(CompletableFuture<T> future) {
43+
this.future = future;
44+
}
45+
46+
@Override
47+
public void onSuccess(T result) {
48+
future.complete(result);
49+
}
50+
51+
@Override
52+
public void onError(@NotNull Throwable throwable) {
53+
future.completeExceptionally(throwable);
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)