Skip to content

Commit 144e4e6

Browse files
committed
ReActFunctionCallingChatAgent
1 parent ff01a98 commit 144e4e6

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

Pro

Submodule Pro updated from 36828ea to 8dfae2b

Tool/Sources/LangChain/Agent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public extension CallbackEvents {
2525
public let info: AgentFinish<Output>
2626
}
2727

28-
func agentDidFinish<Output: AgentOutputParsable>() -> AgentDidFinish<Output>.Type {
28+
static func agentDidFinish<Output: AgentOutputParsable>() -> AgentDidFinish<Output>.Type {
2929
AgentDidFinish<Output>.self
3030
}
3131

Tool/Sources/LangChain/AgentExecutor.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public actor AgentExecutor<InnerAgent: Agent>: Chain
2525
tools: [AgentTool],
2626
maxIteration: Int? = 10,
2727
maxExecutionTime: Double? = nil,
28-
earlyStopHandleType: AgentEarlyStopHandleType = .force,
28+
earlyStopHandleType: AgentEarlyStopHandleType = .generate,
2929
initialSteps: [AgentAction] = []
3030
) {
3131
self.agent = agent
@@ -151,8 +151,7 @@ extension AgentExecutor {
151151
let completedActions = try await withThrowingTaskGroup(of: AgentAction.self) {
152152
taskGroup in
153153
for action in actions {
154-
callbackManagers
155-
.forEach { $0.send(CallbackEvents.AgentActionDidStart(info: action)) }
154+
callbackManagers.send(CallbackEvents.AgentActionDidStart(info: action))
156155
if action.observation != nil {
157156
taskGroup.addTask { action }
158157
continue
@@ -167,8 +166,7 @@ extension AgentExecutor {
167166
for try await action in taskGroup {
168167
try Task.checkCancellation()
169168
completedActions.append(action)
170-
callbackManagers
171-
.forEach { $0.send(CallbackEvents.AgentActionDidEnd(info: action)) }
169+
callbackManagers.send(CallbackEvents.AgentActionDidEnd(info: action))
172170
}
173171
return completedActions
174172
}

Tool/Sources/LangChain/Callback.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public struct CallbackEvents {
1010
}
1111

1212
public struct CallbackManager {
13+
struct Observer<Event: CallbackEvent> {
14+
let handler: (Event.Info) -> Void
15+
}
16+
1317
fileprivate var observers = [Any]()
1418

1519
public init() {}
@@ -24,28 +28,28 @@ public struct CallbackManager {
2428
_: Event.Type = Event.self,
2529
_ handler: @escaping (Event.Info) -> Void
2630
) {
27-
observers.append(handler)
31+
observers.append(Observer<Event>(handler: handler))
2832
}
2933

3034
public mutating func on<Event: CallbackEvent>(
3135
_: KeyPath<CallbackEvents, Event.Type>,
3236
_ handler: @escaping (Event.Info) -> Void
3337
) {
34-
observers.append(handler)
38+
observers.append(Observer<Event>(handler: handler))
3539
}
3640

3741
public func send<Event: CallbackEvent>(_ event: Event) {
38-
for case let observer as ((Event.Info) -> Void) in observers {
39-
observer(event.info)
42+
for case let observer as Observer<Event> in observers {
43+
observer.handler(event.info)
4044
}
4145
}
4246

4347
func send<Event: CallbackEvent>(
4448
_: KeyPath<CallbackEvents, Event.Type>,
4549
_ info: Event.Info
4650
) {
47-
for case let observer as ((Event.Info) -> Void) in observers {
48-
observer(info)
51+
for case let observer as Observer<Event> in observers {
52+
observer.handler(info)
4953
}
5054
}
5155
}

0 commit comments

Comments
 (0)