forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombineAnswersChain.swift
More file actions
72 lines (63 loc) · 2.17 KB
/
Copy pathCombineAnswersChain.swift
File metadata and controls
72 lines (63 loc) · 2.17 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
import Foundation
import Logger
import OpenAIService
import Preferences
public class CombineAnswersChain: Chain {
public struct Input: Decodable {
public var question: String
public var answers: [String]
public init(question: String, answers: [String]) {
self.question = question
self.answers = answers
}
}
public typealias Output = String
public let chatModelChain: ChatModelChain<Input>
public init(
configuration: ChatGPTConfiguration =
UserPreferenceChatGPTConfiguration(chatModelKey: \.preferredChatModelIdForUtilities),
extraInstructions: String = ""
) {
chatModelChain = .init(
chatModel: OpenAIChat(
configuration: configuration.overriding {
$0.runFunctionsAutomatically = false
},
memory: nil,
stream: false
),
stops: ["Observation:"],
promptTemplate: { input in
[
.init(
role: .system,
content: """
You are a helpful assistant.
Your job is to combine multiple answers from different sources to one question.
\(extraInstructions)
"""
),
.init(role: .user, content: """
Question: \(input.question)
Answers:
\(input.answers.joined(separator: "\n\(String(repeating: "-", count: 32))\n"))
What is the combined answer?
"""),
]
}
)
}
public func callLogic(
_ input: Input,
callbackManagers: [CallbackManager]
) async throws -> String {
let output = try await chatModelChain.call(input, callbackManagers: callbackManagers)
return await parseOutput(output)
}
public func parseOutput(_ message: ChatMessage) async -> String {
return message.content ?? "No answer."
}
public func parseOutput(_ output: String) -> String {
output
}
}