-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
78 lines (69 loc) · 1.99 KB
/
ContentView.swift
File metadata and controls
78 lines (69 loc) · 1.99 KB
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
//
// Created by Artem Novichkov on 17.08.2025.
//
import SwiftUI
import FoundationModels
import TranscriptDebugMenu
struct ContentView: View {
@State private var isLoading = false
@State private var text = ""
@State private var session = LanguageModelSession(tools: [MoodTool()]) {
"You're a helpful assistant that generates haikus. Always use `generateMood` tool to get a random mood for the haiku."
}
@State private var showTranscript = false
var body: some View {
NavigationStack {
VStack {
if isLoading {
ProgressView("Loading...")
} else {
Text(text)
}
}
.navigationTitle("Haiku")
.transcriptDebugMenu(session, isPresented: $showTranscript)
.toolbar {
ToolbarItem {
Button {
showTranscript.toggle()
} label: {
Label("Transcript", systemImage: "gear")
}
}
}
.onAppear {
Task {
do {
isLoading = true
let response = try await session.respond(to: "Generate a haiku about Swift")
text = response.content
isLoading = false
} catch {
isLoading = false
print("Error: \(error)")
}
}
}
}
}
}
@Generable
enum Mood: String, CaseIterable {
case happy, sad, thoughtful, excited, calm
}
final class MoodTool: Tool {
let name = "generateMood"
let description = "Generates a random mood for haiku"
@Generable
struct Arguments {}
func call(arguments: Arguments) async throws -> Mood? {
.allCases.randomElement()
}
}
@Generable
struct Haiku {
let text: String
}
#Preview {
ContentView()
}