|
1 | 1 | import AppKit |
| 2 | +import Foundation |
2 | 3 | import LangChain |
3 | 4 | import OpenAIService |
4 | 5 | import PlaygroundSupport |
5 | 6 | import SwiftUI |
6 | 7 |
|
7 | 8 | struct QAForm: View { |
8 | 9 | @State var intermediateAnswers = [RefineDocumentChain.IntermediateAnswer]() |
| 10 | + @State var relevantDocuments = [(document: Document, distance: Float)]() |
| 11 | + @State var duration: TimeInterval = 0 |
9 | 12 | @State var answer: String = "" |
10 | 13 | @State var question: String = "What is Swift macros?" |
11 | 14 | @State var isProcessing: Bool = false |
12 | 15 | @State var url: String = "https://developer.apple.com/documentation/swift/applying-macros" |
13 | 16 |
|
14 | 17 | var body: some View { |
15 | | - Form { |
16 | | - Section(header: Text("Input")) { |
17 | | - TextField("URL", text: $url) |
18 | | - TextField("Question", text: $question) |
19 | | - Button("Ask") { |
20 | | - Task { |
21 | | - do { |
22 | | - try await ask() |
23 | | - } catch { |
24 | | - answer = error.localizedDescription |
| 18 | + HStack(spacing: 0) { |
| 19 | + ScrollView { |
| 20 | + Form { |
| 21 | + Section(header: Text("Input")) { |
| 22 | + TextField("URL", text: $url) |
| 23 | + TextField("Question", text: $question) |
| 24 | + HStack { |
| 25 | + Button("Ask") { |
| 26 | + Task { |
| 27 | + do { |
| 28 | + try await ask() |
| 29 | + } catch { |
| 30 | + answer = error.localizedDescription |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + .disabled(isProcessing) |
| 35 | + |
| 36 | + Text("\(duration) seconds") |
| 37 | + } |
| 38 | + } |
| 39 | + Section(header: Text("Answer")) { |
| 40 | + Text(answer) |
| 41 | + } |
| 42 | + Section(header: Text("Intermediate Answers")) { |
| 43 | + ForEach(0..<intermediateAnswers.endIndex, id: \.self) { index in |
| 44 | + let answer = intermediateAnswers[index] |
| 45 | + VStack(alignment: .leading) { |
| 46 | + Text(answer.answer) |
| 47 | + VStack(alignment: .leading) { |
| 48 | + Text("Usefulness: \(answer.usefulness)") |
| 49 | + Text("Needs more context: \(answer.more ? "Yes" : "No")") |
| 50 | + } |
| 51 | + .padding() |
| 52 | + .background { |
| 53 | + RoundedRectangle(cornerRadius: 8) |
| 54 | + .fill(Color(NSColor.textBackgroundColor)) |
| 55 | + } |
| 56 | + Divider() |
| 57 | + } |
| 58 | + .textSelection(.enabled) |
25 | 59 | } |
26 | 60 | } |
27 | 61 | } |
28 | | - .disabled(isProcessing) |
29 | | - } |
30 | | - Section(header: Text("Answer")) { |
31 | | - Text(answer) |
| 62 | + .formStyle(.grouped) |
32 | 63 | } |
33 | | - Section(header: Text("Intermediate Answers")) { |
34 | | - ForEach(0..<intermediateAnswers.endIndex, id: \.self) { index in |
35 | | - let answer = intermediateAnswers[index] |
36 | | - VStack { |
37 | | - Text(answer.answer) |
38 | | - Text("Score: \(answer.score)") |
39 | | - Text("Needs more context: \(answer.more ? "Yes" : "No")") |
40 | | - Divider() |
| 64 | + |
| 65 | + ScrollView { |
| 66 | + Form { |
| 67 | + Section(header: Text("Relevant Documents")) { |
| 68 | + ForEach(0..<relevantDocuments.endIndex, id: \.self) { index in |
| 69 | + let document = relevantDocuments[index] |
| 70 | + VStack(alignment: .leading) { |
| 71 | + Text("\(document.distance)") |
| 72 | + Text(document.document.pageContent) |
| 73 | + Divider() |
| 74 | + } |
| 75 | + .textSelection(.enabled) |
| 76 | + } |
41 | 77 | } |
42 | | - } |
| 78 | + }.formStyle(.grouped) |
43 | 79 | } |
44 | 80 | } |
45 | | - .formStyle(.grouped) |
46 | 81 | } |
47 | 82 |
|
48 | 83 | func ask() async throws { |
| 84 | + let start = Date().timeIntervalSince1970 |
| 85 | + answer = "" |
| 86 | + relevantDocuments = [] |
49 | 87 | intermediateAnswers = [] |
| 88 | + duration = 0 |
50 | 89 | isProcessing = true |
51 | 90 | defer { isProcessing = false } |
52 | 91 | guard let url = URL(string: url) else { |
@@ -84,15 +123,19 @@ struct QAForm: View { |
84 | 123 | $0.on(CallbackEvents.RetrievalQADidGenerateIntermediateAnswer.self) { |
85 | 124 | intermediateAnswers.append($0) |
86 | 125 | } |
| 126 | + $0.on(CallbackEvents.RetrievalQADidExtractRelevantContent.self) { |
| 127 | + relevantDocuments = $0 |
| 128 | + } |
87 | 129 | }, |
88 | 130 | ] |
89 | 131 | ) |
| 132 | + duration = Date().timeIntervalSince1970 - start |
90 | 133 | } |
91 | 134 | } |
92 | 135 |
|
93 | 136 | let hostingView = NSHostingController( |
94 | 137 | rootView: QAForm() |
95 | | - .frame(width: 600, height: 800) |
| 138 | + .frame(width: 800, height: 800) |
96 | 139 | ) |
97 | 140 |
|
98 | 141 | PlaygroundPage.current.needsIndefiniteExecution = true |
|
0 commit comments