@@ -4,12 +4,14 @@ import LangChain
44import OpenAIService
55import PlaygroundSupport
66import SwiftUI
7+ import TokenEncoder
78
89struct QAForm : View {
9- @State var intermediateAnswers = [ RefineDocumentChain . IntermediateAnswer ] ( )
10+ @State var relevantInformation = [ String ] ( )
1011 @State var relevantDocuments = [ ( document: Document, distance: Float) ] ( )
1112 @State var duration : TimeInterval = 0
1213 @State var answer : String = " "
14+ @State var tokenCount : Int = 0
1315 @State var question : String = " What is Swift macros? "
1416 @State var isProcessing : Bool = false
1517 @State var url : String = " https://developer.apple.com/documentation/swift/applying-macros "
@@ -36,23 +38,14 @@ struct QAForm: View {
3638 Text ( " \( duration) seconds " )
3739 }
3840 }
39- Section ( header: Text ( " Answer " ) ) {
41+ Section ( header: Text ( " All Relevant Information ( \( tokenCount ) words) " ) ) {
4042 Text ( answer)
4143 }
42- Section ( header: Text ( " Intermediate Answers " ) ) {
43- ForEach ( 0 ..< intermediateAnswers . endIndex, id: \. self) { index in
44- let answer = intermediateAnswers [ index]
44+ Section ( header: Text ( " Relevant Information " ) ) {
45+ ForEach ( 0 ..< relevantInformation . endIndex, id: \. self) { index in
46+ let information = relevantInformation [ index]
4547 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- }
48+ Text ( information)
5649 Divider ( )
5750 }
5851 . textSelection ( . enabled)
@@ -84,8 +77,9 @@ struct QAForm: View {
8477 let start = Date ( ) . timeIntervalSince1970
8578 answer = " "
8679 relevantDocuments = [ ]
87- intermediateAnswers = [ ]
80+ relevantInformation = [ ]
8881 duration = 0
82+ tokenCount = 0
8983 isProcessing = true
9084 defer { isProcessing = false }
9185 guard let url = URL ( string: url) else {
@@ -112,23 +106,24 @@ struct QAForm: View {
112106 }
113107 } ( )
114108
115- let qa = RetrievalQAChain (
109+ let qa = QAInformationRetrievalChain (
116110 vectorStore: store,
117111 embedding: embedding
118112 )
119113 answer = try await qa. run (
120114 question,
121115 callbackManagers: [
122116 . init {
123- $0. on ( CallbackEvents . RetrievalQADidGenerateIntermediateAnswer . self ) {
124- intermediateAnswers . append ( $0)
117+ $0. on ( \ . relevantInformationExtractionChainDidExtractPartialRelevantContent ) {
118+ relevantInformation . append ( $0)
125119 }
126- $0. on ( CallbackEvents . RetrievalQADidExtractRelevantContent . self ) {
120+ $0. on ( \ . retrievalQADidExtractRelevantContent ) {
127121 relevantDocuments = $0
128122 }
129123 } ,
130124 ]
131125 )
126+ tokenCount = answer. split ( separator: " " ) . count
132127 duration = Date ( ) . timeIntervalSince1970 - start
133128 }
134129}
0 commit comments