Skip to content

Commit eb6821f

Browse files
committed
Tweak auto stop in QA chain
1 parent 5f602f6 commit eb6821f

4 files changed

Lines changed: 153 additions & 133 deletions

File tree

Playground.playground/Pages/RetrievalQAChain.xcplaygroundpage/Contents.swift

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,91 @@
11
import AppKit
2+
import Foundation
23
import LangChain
34
import OpenAIService
45
import PlaygroundSupport
56
import SwiftUI
67

78
struct QAForm: View {
89
@State var intermediateAnswers = [RefineDocumentChain.IntermediateAnswer]()
10+
@State var relevantDocuments = [(document: Document, distance: Float)]()
11+
@State var duration: TimeInterval = 0
912
@State var answer: String = ""
1013
@State var question: String = "What is Swift macros?"
1114
@State var isProcessing: Bool = false
1215
@State var url: String = "https://developer.apple.com/documentation/swift/applying-macros"
1316

1417
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)
2559
}
2660
}
2761
}
28-
.disabled(isProcessing)
29-
}
30-
Section(header: Text("Answer")) {
31-
Text(answer)
62+
.formStyle(.grouped)
3263
}
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+
}
4177
}
42-
}
78+
}.formStyle(.grouped)
4379
}
4480
}
45-
.formStyle(.grouped)
4681
}
4782

4883
func ask() async throws {
84+
let start = Date().timeIntervalSince1970
85+
answer = ""
86+
relevantDocuments = []
4987
intermediateAnswers = []
88+
duration = 0
5089
isProcessing = true
5190
defer { isProcessing = false }
5291
guard let url = URL(string: url) else {
@@ -84,15 +123,19 @@ struct QAForm: View {
84123
$0.on(CallbackEvents.RetrievalQADidGenerateIntermediateAnswer.self) {
85124
intermediateAnswers.append($0)
86125
}
126+
$0.on(CallbackEvents.RetrievalQADidExtractRelevantContent.self) {
127+
relevantDocuments = $0
128+
}
87129
},
88130
]
89131
)
132+
duration = Date().timeIntervalSince1970 - start
90133
}
91134
}
92135

93136
let hostingView = NSHostingController(
94137
rootView: QAForm()
95-
.frame(width: 600, height: 800)
138+
.frame(width: 800, height: 800)
96139
)
97140

98141
PlaygroundPage.current.needsIndefiniteExecution = true

Playground.playground/Pages/RetrievalQAChain.xcplaygroundpage/timeline.xctimeline

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)