Skip to content

Commit

Permalink
feat: iOS sample integration
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoarg committed Mar 13, 2023
1 parent 9310e86 commit 596374d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sample/ios/EnvConfig.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
// https://help.apple.com/xcode/#/dev745c5c974


API_KEY = // YOUR API KEY
API_KEY = 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
34 changes: 31 additions & 3 deletions sample/ios/YChatApp/Features/Completion/CompletionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ private extension CompletionView {
}
case .bot:
HStack {
botChatBubble(message: chatMessage.message)
Spacer().frame(width: 60)
Spacer()
if let imageUrl = chatMessage.url {
botImageBubble(imageUrl)
Spacer().frame(width: 60)
Spacer()
} else {
botChatBubble(message: chatMessage.message)
Spacer().frame(width: 60)
Spacer()
}
}
case .loading:
HStack {
Expand Down Expand Up @@ -120,6 +126,28 @@ private extension CompletionView {
.cornerRadius(16, corners: [.bottomLeft, .bottomLeft, .topRight])
}
}

@ViewBuilder
private func botImageBubble(_ url: String) -> some View {
HStack(alignment: .top, spacing: 4) {
Circle()
.fill(.green)
.frame(width: 40, height: 40)
.overlay {
Image(uiImage: Icon.bot.uiImage)
.renderingMode(.template)
.foregroundColor(.white)
}
ZStack {
AsyncImage(url: URL(string: url))
.foregroundColor(.grayDark)
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(Color.grayLight)
.cornerRadius(16, corners: [.bottomLeft, .bottomLeft, .topRight])
}
}

@ViewBuilder
private func sendMessageSection() -> some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct ChatMessage: Identifiable, Equatable {
let id: String
var message: String = ""
var type: MessageType = .human(error: false)
var url: String?

enum MessageType: Equatable {
case human(error: Bool), bot, loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ internal final class CompletionViewModel: ObservableObject {
content: "You are a helpful assistant."
)

private var imageGenerations: ImageGenerations =
YChatCompanion.shared.create(apiKey: Config.apiKey)
.imageGenerations()
.setResults(results: 2)

@Published
var message: String = ""

Expand All @@ -37,9 +42,15 @@ internal final class CompletionViewModel: ObservableObject {
cleanLastMessage()
addLoading()
do {
let result = try await chatCompletions.execute(content: input)[0].content
removeLoading()
addAIMessage(message: result)
if input.contains("/image ") {
let result = try await imageGenerations.execute(prompt: input)[0].url
removeLoading()
addAIImage(url: result)
} else {
let result = try await chatCompletions.execute(content: input)[0].content
removeLoading()
addAIMessage(message: result)
}
} catch {
removeLoading()
setError()
Expand All @@ -64,6 +75,15 @@ internal final class CompletionViewModel: ObservableObject {
)
chatMessageList.append(chatMessage)
}

private func addAIImage(url: String) {
let chatMessage = ChatMessage(
id: UUID().uuidString,
type: .bot,
url: url
)
chatMessageList.append(chatMessage)
}

private func addLoading() {
let chatMessage = ChatMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public ResponseEntity<String> chatCompletions(
return ResponseEntity.ok(result);
}

@GetMapping("image-generations")
public ResponseEntity<String> imageGenerations(
@RequestParam(value = "prompt", defaultValue = Defaults.CHAT_COMPLETION_INPUT) String input
) throws Exception {
String result = YChatService.getImageGenerationsAnswer(input);
return ResponseEntity.ok(result);
}

private static class Defaults {
static final String COMPLETION_INPUT = "Say this is a test.";
static final String CHAT_COMPLETION_INPUT = "Tell me one strength exercise";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
import co.yml.ychat.YChat;
import co.yml.ychat.domain.model.ImageGenerated;

@Service
public class YChatService {
Expand Down Expand Up @@ -35,6 +36,14 @@ public String getChatCompletionsAnswer(String input, String topic) throws Except
return future.get().get(0).getContent();
}

public String getImageGenerationsAnswer(String prompt) throws Exception {
final CompletableFuture<List<String>> future = new CompletableFuture<>();
ychat.imageGenerations()
.setResults(2)
.execute(prompt, new CompletionCallbackResult<>(future));
return future.get().get(0);
}

private static class CompletionCallbackResult<T> implements YChat.Callback<T> {

private final CompletableFuture<T> future;
Expand Down

0 comments on commit 596374d

Please sign in to comment.