Skip to content

Commit 57925ce

Browse files
committed
Update
1 parent a08cca8 commit 57925ce

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

Tool/Sources/LangChain/AgentExecutor.swift

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import Foundation
22

3-
public protocol AgentOutputParsable {
4-
static func parse(_ string: String) throws -> Self
5-
var botReadableContent: String { get }
6-
}
7-
8-
extension String: AgentOutputParsable {
9-
public static func parse(_ string: String) throws -> String { string }
10-
public var botReadableContent: String { self }
11-
}
12-
133
public actor AgentExecutor<InnerAgent: Agent>: Chain
144
where InnerAgent.Input == String, InnerAgent.Output: AgentOutputParsable
155
{
166
public typealias Input = String
177
public struct Output {
18-
typealias FinalOutput = AgentFinish<InnerAgent.Output>.ReturnValue
8+
public typealias FinalOutput = AgentFinish<InnerAgent.Output>.ReturnValue
199

20-
let finalOutput: FinalOutput
10+
public let finalOutput: FinalOutput
2111
let intermediateSteps: [AgentAction]
2212
}
2313

@@ -180,7 +170,7 @@ extension AgentExecutor {
180170
func getToolFinish(action: AgentAction) -> AgentFinish<InnerAgent.Output>? {
181171
guard let tool = tools[action.toolName] else { return nil }
182172
guard tool.returnDirectly else { return nil }
183-
173+
184174
do {
185175
let result = try InnerAgent.Output.parse(action.observation ?? "")
186176
return .init(returnValue: .success(result), log: action.observation ?? "")
@@ -193,3 +183,32 @@ extension AgentExecutor {
193183
}
194184
}
195185

186+
// MARK: - AgentOutputParsable
187+
188+
public protocol AgentOutputParsable {
189+
static func parse(_ string: String) throws -> Self
190+
var botReadableContent: String { get }
191+
}
192+
193+
extension String: AgentOutputParsable {
194+
public static func parse(_ string: String) throws -> String { string }
195+
public var botReadableContent: String { self }
196+
}
197+
198+
extension Int: AgentOutputParsable {
199+
public static func parse(_ string: String) throws -> Int {
200+
guard let int = Int(string) else { return 0 }
201+
return int
202+
}
203+
204+
public var botReadableContent: String { String(self) }
205+
}
206+
207+
extension Double: AgentOutputParsable {
208+
public static func parse(_ string: String) throws -> Double {
209+
guard let double = Double(string) else { return 0 }
210+
return double
211+
}
212+
213+
public var botReadableContent: String { String(self) }
214+
}

0 commit comments

Comments
 (0)