Skip to content

Commit 4a702ab

Browse files
committed
Update
1 parent 12cd285 commit 4a702ab

5 files changed

Lines changed: 74 additions & 6 deletions

File tree

Pro

Submodule Pro updated from 9be768d to 9835eba

Tool/Sources/LangChain/Callback.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public protocol CallbackEvent {
88
public struct CallbackEvents {
99
public struct UnTypedEvent: CallbackEvent {
1010
public var info: String
11-
init(info: String) {
11+
public init(info: String) {
1212
self.info = info
1313
}
1414
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import Foundation
2+
import Logger
3+
import OpenAIService
4+
5+
public class CombineAnswersChain: Chain {
6+
public struct Input: Decodable {
7+
public var question: String
8+
public var answers: [String]
9+
public init(question: String, answers: [String]) {
10+
self.question = question
11+
self.answers = answers
12+
}
13+
}
14+
15+
public typealias Output = String
16+
public let chatModelChain: ChatModelChain<Input>
17+
18+
public init(
19+
configuration: ChatGPTConfiguration = UserPreferenceChatGPTConfiguration(),
20+
extraInstructions: String = ""
21+
) {
22+
chatModelChain = .init(
23+
chatModel: OpenAIChat(
24+
configuration: configuration.overriding {
25+
$0.runFunctionsAutomatically = false
26+
},
27+
memory: nil,
28+
stream: false
29+
),
30+
stops: ["Observation:"],
31+
promptTemplate: { input in
32+
[
33+
.init(
34+
role: .system,
35+
content: """
36+
You are a helpful assistant.
37+
Your job is to combine multiple answers from different sources to one question.
38+
\(extraInstructions)
39+
"""
40+
),
41+
.init(role: .user, content: """
42+
Question: \(input.question)
43+
44+
Answers:
45+
\(input.answers.joined(separator: "\n\(String(repeating: "-", count: 32))\n"))
46+
47+
What is the combined answer?
48+
"""),
49+
]
50+
}
51+
)
52+
}
53+
54+
public func callLogic(
55+
_ input: Input,
56+
callbackManagers: [CallbackManager]
57+
) async throws -> String {
58+
let output = try await chatModelChain.call(input, callbackManagers: callbackManagers)
59+
return await parseOutput(output)
60+
}
61+
62+
public func parseOutput(_ message: ChatMessage) async -> String {
63+
return message.content ?? "No answer."
64+
}
65+
66+
public func parseOutput(_ output: String) -> String {
67+
output
68+
}
69+
}
70+

Tool/Sources/LangChain/Chains/RetrievalQA.swift renamed to Tool/Sources/LangChain/Chains/QAInformationRetrievalChain.swift

File renamed without changes.

Tool/Sources/LangChain/Chains/StructuredOutputChatModelChain.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ public class StructuredOutputChatModelChain<Output: Decodable>: Chain {
6363

6464
public init(
6565
configuration: ChatGPTConfiguration = UserPreferenceChatGPTConfiguration(),
66-
tools: [AgentTool] = [],
6766
endFunction: EndFunction,
68-
extraSystemPrompt: String = ""
67+
promptTemplate: ((String) -> [ChatMessage])? = nil
6968
) {
7069
functionProvider = .init(
7170
endFunction: endFunction
@@ -80,15 +79,14 @@ public class StructuredOutputChatModelChain<Output: Decodable>: Chain {
8079
stream: false
8180
),
8281
stops: ["Observation:"],
83-
promptTemplate: { input in
82+
promptTemplate: promptTemplate ?? { input in
8483
[
8584
.init(
8685
role: .system,
8786
content: """
8887
You are a helpful assistant
8988
Generate a final answer to my query as concisely, helpfully and accurately as possible.
9089
You don't ask me for additional information.
91-
\(extraSystemPrompt)
9290
"""
9391
),
9492
.init(role: .user, content: input),

0 commit comments

Comments
 (0)