@@ -15,44 +15,74 @@ public final class RelevantInformationExtractionChain: Chain {
1515 public typealias Output = String
1616
1717 class FunctionProvider : ChatGPTFunctionProvider {
18- var functionCallStrategy : FunctionCallStrategy ? = . auto
19- var functions : [ any ChatGPTFunction ] = [ NoneFunction ( ) ]
18+ var functionCallStrategy : FunctionCallStrategy ? = . name ( " saveFinalAnswer " )
19+ var functions : [ any ChatGPTFunction ] = [ FinalAnswer ( ) ]
2020 }
2121
22- struct NoneFunction : ChatGPTArgumentsCollectingFunction {
23- typealias Arguments = NoArguments
24- var name : String = " noInformationFound "
25- var description : String = " Call when you can't find any relevant information from the document, or the question was not mentioned in the document "
22+ struct FinalAnswer : ChatGPTArgumentsCollectingFunction {
23+ struct Arguments : Decodable {
24+ var relevantInformation : String
25+ var noRelevantInformationFound : Bool ?
26+ }
27+
28+ var name : String = " saveFinalAnswer "
29+ var description : String =
30+ " save the relevant information "
31+ var argumentSchema : JSONSchemaValue {
32+ [
33+ . type: " object " ,
34+ . properties: [
35+ " relevantInformation " : [ . type: " string " ] ,
36+ " noRelevantInformationFound " : [ . type: " boolean " ] ,
37+ ] ,
38+ . required: [ " relevantInformation " , " noRelevantInformationFound " ] ,
39+ ]
40+ }
41+ }
42+
43+ let filterMetadata : ( String ) -> Bool
44+ let hint : String
45+
46+ init ( filterMetadata: @escaping ( String ) -> Bool = { _ in true } , hint: String ) {
47+ self . filterMetadata = filterMetadata
48+ self . hint = hint
2649 }
2750
2851 func buildChatModel( ) -> ChatModelChain < TaskInput > {
2952 . init(
3053 chatModel: OpenAIChat (
3154 configuration: UserPreferenceChatGPTConfiguration ( ) . overriding {
32- $0. temperature = 0
55+ $0. temperature = 0.5
3356 $0. runFunctionsAutomatically = false
3457 } ,
3558 memory: EmptyChatGPTMemory ( ) ,
3659 functionProvider: FunctionProvider ( ) ,
3760 stream: false
3861 )
39- ) { input in [
62+ ) { [ filterMetadata , hint ] input in [
4063 . init(
4164 role: . system,
4265 content: """
4366 Extract the relevant information from the Document according to the Question.
67+ The information may not directly answer the question, but it should be relevant to the question, \
68+ please think carefully and make you decision.
4469 Make the information clear, concise and short.
4570 If found code, wrap it in markdown code block.
71+ \( hint)
4672 """
4773 ) ,
4874 . init(
4975 role: . user,
5076 content: """
5177 Question:###
78+ (how, when, what or why)
5279 \( input. question)
5380 ###
5481 Document:###
55- \( input. document)
82+ \( input. document. metadata. filter { key, _ in
83+ filterMetadata ( key)
84+ } )
85+ \( input. document. pageContent)
5686 ###
5787 """
5888 ) ,
@@ -73,6 +103,22 @@ public final class RelevantInformationExtractionChain: Chain {
73103 taskInput,
74104 callbackManagers: callbackManagers
75105 )
106+
107+ if let functionCall = output. functionCall {
108+ do {
109+ let arguments = try JSONDecoder ( ) . decode (
110+ FinalAnswer . Arguments. self,
111+ from: functionCall. arguments. data ( using: . utf8) ?? Data ( )
112+ )
113+ if arguments. noRelevantInformationFound ?? false {
114+ return " "
115+ }
116+ return arguments. relevantInformation
117+ } catch {
118+ return output. content ?? " "
119+ }
120+ }
121+
76122 return output. content ?? " "
77123 }
78124
@@ -119,3 +165,4 @@ public extension CallbackEvents {
119165 RelevantInformationExtractionChainDidExtractPartialRelevantContent . self
120166 }
121167}
168+
0 commit comments