Skip to content

Commit 8560aee

Browse files
committed
Move CallbackEvents to the right places
1 parent 821a2d2 commit 8560aee

4 files changed

Lines changed: 43 additions & 27 deletions

File tree

Tool/Sources/LangChain/Agent.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ public struct AgentAction: Equatable {
2020
}
2121
}
2222

23+
public extension CallbackEvents {
24+
struct AgentDidFinish: CallbackEvent {
25+
public let info: AgentFinish
26+
}
27+
28+
struct AgentActionDidStart: CallbackEvent {
29+
public let info: AgentAction
30+
}
31+
32+
struct AgentActionDidEnd: CallbackEvent {
33+
public let info: AgentAction
34+
}
35+
}
36+
2337
public struct AgentFinish: Equatable {
2438
public var returnValue: String
2539
public var log: String
@@ -38,12 +52,12 @@ public enum AgentNextStep: Equatable {
3852
public enum AgentScratchPad: Equatable {
3953
case text(String)
4054
case messages([String])
41-
55+
4256
var isEmpty: Bool {
4357
switch self {
44-
case .text(let text):
58+
case let .text(text):
4559
return text.isEmpty
46-
case .messages(let messages):
60+
case let .messages(messages):
4761
return messages.isEmpty
4862
}
4963
}
@@ -108,7 +122,7 @@ public extension Agent {
108122
case .generate:
109123
var thoughts = constructBaseScratchpad(intermediateSteps: intermediateSteps)
110124
thoughts += """
111-
125+
112126
\(llmPrefix)I now need to return a final answer based on the previous steps:
113127
(Please continue with `Final Answer:`)
114128
"""

Tool/Sources/LangChain/Callback.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,3 @@ public struct CallbackManager {
3131
}
3232
}
3333
}
34-
35-
public extension CallbackEvents {
36-
struct AgentDidFinish: CallbackEvent {
37-
public let info: AgentFinish
38-
}
39-
40-
struct AgentActionDidStart: CallbackEvent {
41-
public let info: AgentAction
42-
}
43-
44-
struct AgentActionDidEnd: CallbackEvent {
45-
public let info: AgentAction
46-
}
47-
48-
struct LLMDidProduceNewToken: CallbackEvent {
49-
public let info: String
50-
}
51-
52-
struct ChainDidStart<T: Chain>: CallbackEvent {
53-
public let info: (type: T.Type, input: T.Input)
54-
}
55-
}
56-

Tool/Sources/LangChain/Chain.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public protocol Chain {
88
}
99

1010
public extension Chain {
11+
typealias ChainDidStart = CallbackEvents.ChainDidStart<Self>
12+
typealias ChainDidEnd = CallbackEvents.ChainDidEnd<Self>
13+
1114
func run(_ input: Input, callbackManagers: [CallbackManager] = []) async throws -> String {
1215
let output = try await call(input, callbackManagers: callbackManagers)
1316
return parseOutput(output)
@@ -18,10 +21,26 @@ public extension Chain {
1821
callbackManager
1922
.send(CallbackEvents.ChainDidStart(info: (type: Self.self, input: input)))
2023
}
24+
defer {
25+
for callbackManager in callbackManagers {
26+
callbackManager
27+
.send(CallbackEvents.ChainDidEnd(info: (type: Self.self, input: input)))
28+
}
29+
}
2130
return try await callLogic(input, callbackManagers: callbackManagers)
2231
}
2332
}
2433

34+
public extension CallbackEvents {
35+
struct ChainDidStart<T: Chain>: CallbackEvent {
36+
public let info: (type: T.Type, input: T.Input)
37+
}
38+
39+
struct ChainDidEnd<T: Chain>: CallbackEvent {
40+
public let info: (type: T.Type, input: T.Input)
41+
}
42+
}
43+
2544
public struct SimpleChain<Input, Output>: Chain {
2645
let block: (Input) async throws -> Output
2746
let parseOutputBlock: (Output) -> String

Tool/Sources/LangChain/ChatModel/ChatModel.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ public struct ChatMessage {
2323
self.content = content
2424
}
2525
}
26+
27+
public extension CallbackEvents {
28+
struct LLMDidProduceNewToken: CallbackEvent {
29+
public let info: String
30+
}
31+
}

0 commit comments

Comments
 (0)