Skip to content

Commit cf8a52b

Browse files
committed
feature: add jvm sample
1 parent 8e105ce commit cf8a52b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import org.springframework.http.ResponseEntity;
88
import org.springframework.web.bind.annotation.GetMapping;
99
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.PostMapping;
1011
import org.springframework.web.bind.annotation.RequestMapping;
1112
import org.springframework.web.bind.annotation.RequestParam;
1213
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.multipart.MultipartFile;
1315

1416
@RestController
1517
@RequestMapping("api/ychat")
@@ -64,6 +66,14 @@ public ResponseEntity<AIModel> model(@PathVariable String id) throws Exception {
6466
return ResponseEntity.ok(result);
6567
}
6668

69+
@PostMapping("audio/transcriptions")
70+
public ResponseEntity<String> audioTranscriptions(
71+
@RequestParam("file") MultipartFile multipartFile
72+
) throws Exception {
73+
String result = YChatService.getAudioTranscription(multipartFile);
74+
return ResponseEntity.ok(result);
75+
}
76+
6777
private static class Defaults {
6878
static final String COMPLETION_INPUT = "Say this is a test.";
6979
static final String CHAT_COMPLETION_INPUT = "Tell me one strength exercise";

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import co.yml.ychat.domain.model.AIModel;
55
import co.yml.ychat.domain.model.ChatMessage;
66
import java.util.List;
7+
import java.util.Optional;
78
import java.util.concurrent.CompletableFuture;
89
import org.jetbrains.annotations.NotNull;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.stereotype.Service;
12+
import org.springframework.web.multipart.MultipartFile;
1113

1214
@Service
1315
public class YChatService {
@@ -65,6 +67,14 @@ public AIModel getModel(String id) throws Exception {
6567
return future.get();
6668
}
6769

70+
public String getAudioTranscription(MultipartFile multipartFile) throws Exception {
71+
final CompletableFuture<String> future = new CompletableFuture<>();
72+
String filename = Optional.ofNullable(multipartFile.getOriginalFilename()).orElse("");
73+
byte[] bytes = multipartFile.getBytes();
74+
ychat.audioTranscriptions().execute(filename, bytes, new CompletionCallbackResult<>(future));
75+
return future.get();
76+
}
77+
6878
private static class CompletionCallbackResult<T> implements YChat.Callback<T> {
6979

7080
private final CompletableFuture<T> future;

0 commit comments

Comments
 (0)