Repository navigation
Expand file tree
/
Copy pathContentView.swift
More file actions
73 lines (65 loc) · 1.8 KB
/
Copy pathContentView.swift
File metadata and controls
73 lines (65 loc) · 1.8 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
//
// 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 haiku."
}
@State private var showTranscript = false
var body: some View {
VStack {
if isLoading {
ProgressView("Loading...")
} else {
Text(text)
}
}
.navigationTitle("Haiku")
.toolbar {
ToolbarItem {
Button {
showTranscript.toggle()
} label: {
Label("Transcript", systemImage: "gear")
}
}
}
.transcriptDebugMenu(session, isPresented: $showTranscript)
.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 mood for haiku"
@Generable
struct Arguments {}
func call(arguments: Arguments) async throws -> GeneratedContent {
GeneratedContent(properties: ["mood": Mood.allCases.randomElement()])
}
}
#Preview {
NavigationStack {
ContentView()
}
}