Skip to content

Commit ff01a98

Browse files
committed
Add ReActFunctionCallingChatAgent
1 parent 67c200e commit ff01a98

6 files changed

Lines changed: 26 additions & 26 deletions

File tree

Pro

Submodule Pro updated from 0515904 to 36828ea

Tool/Sources/LangChain/Agent.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ public struct AgentFinish<Output: AgentOutputParsable> {
6464
public enum AgentNextStep<Output: AgentOutputParsable> {
6565
case actions([AgentAction])
6666
case finish(AgentFinish<Output>)
67-
case thought(String)
6867
}
6968

7069
public struct AgentScratchPad<Content: Equatable>: Equatable {
71-
var content: Content
70+
public var content: Content
71+
72+
public init(content: Content) {
73+
self.content = content
74+
}
7275
}
7376

7477
public struct AgentInput<T, ScratchPadContent: Equatable> {
75-
var input: T
76-
var thoughts: AgentScratchPad<ScratchPadContent>
78+
public var input: T
79+
public var thoughts: AgentScratchPad<ScratchPadContent>
7780

7881
public init(input: T, thoughts: AgentScratchPad<ScratchPadContent>) {
7982
self.input = input
@@ -146,8 +149,6 @@ public extension Agent {
146149
return finish
147150
case .actions:
148151
return .init(returnValue: .unstructured(output.content ?? ""), log: output.content ?? "")
149-
case let .thought(content):
150-
return .init(returnValue: .unstructured(content), log: content)
151152
}
152153
}
153154
}

Tool/Sources/LangChain/AgentExecutor.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ public actor AgentExecutor<InnerAgent: Agent>: Chain
1818
var earlyStopHandleType: AgentEarlyStopHandleType
1919
var now: () -> Date = { Date() }
2020
var isCancelled = false
21+
var initialSteps: [AgentAction]
2122

2223
public init(
2324
agent: InnerAgent,
2425
tools: [AgentTool],
2526
maxIteration: Int? = 10,
2627
maxExecutionTime: Double? = nil,
27-
earlyStopHandleType: AgentEarlyStopHandleType = .force
28+
earlyStopHandleType: AgentEarlyStopHandleType = .force,
29+
initialSteps: [AgentAction] = []
2830
) {
2931
self.agent = agent
3032
self.tools = tools.reduce(into: [:]) { $0[$1.name] = $1 }
3133
self.maxIteration = maxIteration
3234
self.maxExecutionTime = maxExecutionTime
3335
self.earlyStopHandleType = earlyStopHandleType
36+
self.initialSteps = initialSteps
3437
}
3538

3639
public func callLogic(
@@ -41,7 +44,7 @@ public actor AgentExecutor<InnerAgent: Agent>: Chain
4144

4245
let startTime = now().timeIntervalSince1970
4346
var iterations = 0
44-
var intermediateSteps: [AgentAction] = []
47+
var intermediateSteps: [AgentAction] = initialSteps
4548

4649
func shouldContinue() -> Bool {
4750
if isCancelled { return false }
@@ -84,8 +87,6 @@ public actor AgentExecutor<InnerAgent: Agent>: Chain
8487
callbackManagers: callbackManagers
8588
)
8689
}
87-
case .thought:
88-
break
8990
}
9091
iterations += 1
9192
}
@@ -152,6 +153,10 @@ extension AgentExecutor {
152153
for action in actions {
153154
callbackManagers
154155
.forEach { $0.send(CallbackEvents.AgentActionDidStart(info: action)) }
156+
if action.observation != nil {
157+
taskGroup.addTask { action }
158+
continue
159+
}
155160
guard let tool = tools[action.toolName] else { throw InvalidToolError() }
156161
taskGroup.addTask {
157162
let observation = try await tool.run(input: action.toolInput)
@@ -169,15 +174,6 @@ extension AgentExecutor {
169174
}
170175

171176
return .actions(completedActions)
172-
case let .thought(content):
173-
return .actions([
174-
.init(
175-
toolName: "Thought",
176-
toolInput: content,
177-
log: "Thought: \(content)",
178-
observation: nil
179-
),
180-
])
181177
}
182178
}
183179

Tool/Sources/LangChain/Agents/FunctionCallingChatAgent.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ public class FunctionCallingChatAgent<Output: AgentOutputParsable & Decodable>:
213213
case let .unstructured(x), let .structured(x):
214214
return .finish(.init(returnValue: .unstructured(x), log: finish.log))
215215
}
216-
case let .thought(content):
217-
return .finish(.init(returnValue: .unstructured(content), log: content))
218216
}
219217
}
220218
}

Tool/Sources/LangChain/Chains/LLMChain.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import Foundation
33
public class ChatModelChain<Input>: Chain {
44
public typealias Output = ChatMessage
55

6-
var chatModel: ChatModel
7-
var promptTemplate: (Input) -> [ChatMessage]
8-
var stops: [String]
6+
public internal(set) var chatModel: ChatModel
7+
public internal(set) var promptTemplate: (Input) -> [ChatMessage]
8+
public internal(set) var stops: [String]
99

1010
public init(
1111
chatModel: ChatModel,

Tool/Sources/Logger/Logger.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum LogLevel: String {
1010
public final class Logger {
1111
private let subsystem: String
1212
private let category: String
13+
private let osLog: OSLog
1314

1415
public static let service = Logger(category: "Service")
1516
public static let ui = Logger(category: "UI")
@@ -25,6 +26,7 @@ public final class Logger {
2526
public init(subsystem: String = "com.intii.CopilotForXcode", category: String) {
2627
self.subsystem = subsystem
2728
self.category = category
29+
osLog = OSLog(subsystem: subsystem, category: category)
2830
}
2931

3032
func log(level: LogLevel, message: String) {
@@ -38,7 +40,6 @@ public final class Logger {
3840
osLogType = .error
3941
}
4042

41-
let osLog = OSLog(subsystem: subsystem, category: category)
4243
os_log("%{public}@", log: osLog, type: osLogType, message as CVarArg)
4344
}
4445

@@ -57,4 +58,8 @@ public final class Logger {
5758
public func error(_ error: Error) {
5859
log(level: .error, message: error.localizedDescription)
5960
}
61+
62+
public func signpost(_ type: OSSignpostType, name: StaticString) {
63+
os_signpost(type, log: osLog, name: name)
64+
}
6065
}

0 commit comments

Comments
 (0)