Skip to content

Commit 596374d

Browse files
committed
feat: iOS sample integration
1 parent 9310e86 commit 596374d

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

sample/ios/EnvConfig.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
// https://help.apple.com/xcode/#/dev745c5c974
1111

1212

13-
API_KEY = // YOUR API KEY
13+
API_KEY = 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

sample/ios/YChatApp/Features/Completion/CompletionView.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ private extension CompletionView {
7272
}
7373
case .bot:
7474
HStack {
75-
botChatBubble(message: chatMessage.message)
76-
Spacer().frame(width: 60)
77-
Spacer()
75+
if let imageUrl = chatMessage.url {
76+
botImageBubble(imageUrl)
77+
Spacer().frame(width: 60)
78+
Spacer()
79+
} else {
80+
botChatBubble(message: chatMessage.message)
81+
Spacer().frame(width: 60)
82+
Spacer()
83+
}
7884
}
7985
case .loading:
8086
HStack {
@@ -120,6 +126,28 @@ private extension CompletionView {
120126
.cornerRadius(16, corners: [.bottomLeft, .bottomLeft, .topRight])
121127
}
122128
}
129+
130+
@ViewBuilder
131+
private func botImageBubble(_ url: String) -> some View {
132+
HStack(alignment: .top, spacing: 4) {
133+
Circle()
134+
.fill(.green)
135+
.frame(width: 40, height: 40)
136+
.overlay {
137+
Image(uiImage: Icon.bot.uiImage)
138+
.renderingMode(.template)
139+
.foregroundColor(.white)
140+
}
141+
ZStack {
142+
AsyncImage(url: URL(string: url))
143+
.foregroundColor(.grayDark)
144+
}
145+
.padding(.horizontal, 16)
146+
.padding(.vertical, 8)
147+
.background(Color.grayLight)
148+
.cornerRadius(16, corners: [.bottomLeft, .bottomLeft, .topRight])
149+
}
150+
}
123151

124152
@ViewBuilder
125153
private func sendMessageSection() -> some View {

sample/ios/YChatApp/Features/Completion/Model/ChatMessage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct ChatMessage: Identifiable, Equatable {
1212
let id: String
1313
var message: String = ""
1414
var type: MessageType = .human(error: false)
15+
var url: String?
1516

1617
enum MessageType: Equatable {
1718
case human(error: Bool), bot, loading

sample/ios/YChatApp/Features/Completion/ViewModel/CompletionViewModel.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ internal final class CompletionViewModel: ObservableObject {
1919
content: "You are a helpful assistant."
2020
)
2121

22+
private var imageGenerations: ImageGenerations =
23+
YChatCompanion.shared.create(apiKey: Config.apiKey)
24+
.imageGenerations()
25+
.setResults(results: 2)
26+
2227
@Published
2328
var message: String = ""
2429

@@ -37,9 +42,15 @@ internal final class CompletionViewModel: ObservableObject {
3742
cleanLastMessage()
3843
addLoading()
3944
do {
40-
let result = try await chatCompletions.execute(content: input)[0].content
41-
removeLoading()
42-
addAIMessage(message: result)
45+
if input.contains("/image ") {
46+
let result = try await imageGenerations.execute(prompt: input)[0].url
47+
removeLoading()
48+
addAIImage(url: result)
49+
} else {
50+
let result = try await chatCompletions.execute(content: input)[0].content
51+
removeLoading()
52+
addAIMessage(message: result)
53+
}
4354
} catch {
4455
removeLoading()
4556
setError()
@@ -64,6 +75,15 @@ internal final class CompletionViewModel: ObservableObject {
6475
)
6576
chatMessageList.append(chatMessage)
6677
}
78+
79+
private func addAIImage(url: String) {
80+
let chatMessage = ChatMessage(
81+
id: UUID().uuidString,
82+
type: .bot,
83+
url: url
84+
)
85+
chatMessageList.append(chatMessage)
86+
}
6787

6888
private func addLoading() {
6989
let chatMessage = ChatMessage(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public ResponseEntity<String> chatCompletions(
3232
return ResponseEntity.ok(result);
3333
}
3434

35+
@GetMapping("image-generations")
36+
public ResponseEntity<String> imageGenerations(
37+
@RequestParam(value = "prompt", defaultValue = Defaults.CHAT_COMPLETION_INPUT) String input
38+
) throws Exception {
39+
String result = YChatService.getImageGenerationsAnswer(input);
40+
return ResponseEntity.ok(result);
41+
}
42+
3543
private static class Defaults {
3644
static final String COMPLETION_INPUT = "Say this is a test.";
3745
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.stereotype.Service;
88
import java.util.concurrent.CompletableFuture;
99
import co.yml.ychat.YChat;
10+
import co.yml.ychat.domain.model.ImageGenerated;
1011

1112
@Service
1213
public class YChatService {
@@ -35,6 +36,14 @@ public String getChatCompletionsAnswer(String input, String topic) throws Except
3536
return future.get().get(0).getContent();
3637
}
3738

39+
public String getImageGenerationsAnswer(String prompt) throws Exception {
40+
final CompletableFuture<List<String>> future = new CompletableFuture<>();
41+
ychat.imageGenerations()
42+
.setResults(2)
43+
.execute(prompt, new CompletionCallbackResult<>(future));
44+
return future.get().get(0);
45+
}
46+
3847
private static class CompletionCallbackResult<T> implements YChat.Callback<T> {
3948

4049
private final CompletableFuture<T> future;

0 commit comments

Comments
 (0)