Skip to content

Commit 5138584

Browse files
committed
Merge branch 'release/2.0.0'
2 parents 3f15fdc + d870c29 commit 5138584

File tree

4 files changed

+100
-82
lines changed

4 files changed

+100
-82
lines changed

LangChainDemo/LangChainDemo/AgentExecutor.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct AgentExecutorState : AgentState {
2020

2121
init() {
2222
self.init([
23-
"intermediate_steps": AppendableValue(),
23+
"intermediate_steps": AppendableValue<(AgentAction, String)>(),
2424
"chat_history": AppendableValue()
2525
])
2626
}
@@ -58,7 +58,7 @@ struct AgentExecutorState : AgentState {
5858
struct ToolOutputParser: BaseOutputParser {
5959
public init() {}
6060
public func parse(text: String) -> Parsed {
61-
print(text.uppercased())
61+
print("\n-------\n\(text.uppercased())\n-------\n")
6262
let pattern = "Action\\s*:[\\s]*(.*)[\\s]*Action\\s*Input\\s*:[\\s]*(.*)"
6363
let regex = try! NSRegularExpression(pattern: pattern)
6464

@@ -99,7 +99,7 @@ public func runAgent( input: String, llm: LLM, tools: [BaseTool], callbacks: [Ba
9999

100100
let toolExecutor = { (action: AgentAction) in
101101
guard let tool = tools.filter({$0.name() == action.action}).first else {
102-
throw GraphRunnerError.executionError("tool \(action.action) not found!")
102+
throw CompiledGraphError.executionError("tool \(action.action) not found!")
103103
}
104104

105105
do {
@@ -151,7 +151,7 @@ public func runAgent( input: String, llm: LLM, tools: [BaseTool], callbacks: [Ba
151151
}
152152

153153

154-
let workflow = GraphState {
154+
let workflow = StateGraph {
155155
AgentExecutorState()
156156
}
157157

@@ -175,10 +175,10 @@ public func runAgent( input: String, llm: LLM, tools: [BaseTool], callbacks: [Ba
175175
try workflow.addNode("call_agent" ) { state in
176176

177177
guard let input = state.input else {
178-
throw GraphRunnerError.executionError("'input' argument not found in state!")
178+
throw CompiledGraphError.executionError("'input' argument not found in state!")
179179
}
180180
guard let intermediate_steps = state.intermediate_steps else {
181-
throw GraphRunnerError.executionError("'intermediate_steps' property not found in state!")
181+
throw CompiledGraphError.executionError("'intermediate_steps' property not found in state!")
182182
}
183183

184184
onAgentStart( input )
@@ -191,18 +191,18 @@ public func runAgent( input: String, llm: LLM, tools: [BaseTool], callbacks: [Ba
191191
onAgentAction( action )
192192
return [ "agent_outcome": AgentOutcome.action(action) ]
193193
default:
194-
throw GraphRunnerError.executionError( "Parsed.error" )
194+
throw CompiledGraphError.executionError( "Parsed.error" )
195195
}
196196
}
197197

198198
try workflow.addNode("call_action" ) { state in
199199

200200
guard let agentOutcome = state.agentOutcome else {
201-
throw GraphRunnerError.executionError("'agent_outcome' property not found in state!")
201+
throw CompiledGraphError.executionError("'agent_outcome' property not found in state!")
202202
}
203203

204204
guard case .action(let action) = agentOutcome else {
205-
throw GraphRunnerError.executionError("'agent_outcome' is not an action!")
205+
throw CompiledGraphError.executionError("'agent_outcome' is not an action!")
206206
}
207207

208208
let result = try await toolExecutor( action )
@@ -218,7 +218,7 @@ public func runAgent( input: String, llm: LLM, tools: [BaseTool], callbacks: [Ba
218218
try workflow.addConditionalEdge( sourceId: "call_agent", condition: { state in
219219

220220
guard let agentOutcome = state.agentOutcome else {
221-
throw GraphRunnerError.executionError("'agent_outcome' property not found in state!")
221+
throw CompiledGraphError.executionError("'agent_outcome' property not found in state!")
222222
}
223223

224224
return switch agentOutcome {

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
9191
}
9292

9393

94-
let workflow = GraphState {
94+
let workflow = StateGraph {
9595
AgentExecutorState()
9696
}
9797

9898
try workflow.addNode("call_agent" ) { state in
9999

100100
guard let input = state.input else {
101-
throw GraphRunnerError.executionError("'input' argument not found in state!")
101+
throw CompiledGraphError.executionError("'input' argument not found in state!")
102102
}
103103
guard let intermediate_steps = state.intermediate_steps else {
104-
throw GraphRunnerError.executionError("'intermediate_steps' property not found in state!")
104+
throw CompiledGraphError.executionError("'intermediate_steps' property not found in state!")
105105
}
106106

107107
let step = await agent.plan(input: input, intermediate_steps: intermediate_steps)
@@ -111,17 +111,17 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
111111
case .action( let action ):
112112
return [ "agent_outcome": AgentOutcome.action(action) ]
113113
default:
114-
throw GraphRunnerError.executionError( "Parsed.error" )
114+
throw CompiledGraphError.executionError( "Parsed.error" )
115115
}
116116
}
117117

118118
try workflow.addNode("call_action" ) { state in
119119

120120
guard let agentOutcome = state.agentOutcome else {
121-
throw GraphRunnerError.executionError("'agent_outcome' property not found in state!")
121+
throw CompiledGraphError.executionError("'agent_outcome' property not found in state!")
122122
}
123123
guard case .action(let action) = agentOutcome else {
124-
throw GraphRunnerError.executionError("'agent_outcome' is not an action!")
124+
throw CompiledGraphError.executionError("'agent_outcome' is not an action!")
125125
}
126126
let result = try await toolExecutor( action )
127127
return [ "intermediate_steps" : (action, result) ]
@@ -132,7 +132,7 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
132132
try workflow.addConditionalEdge( sourceId: "call_agent", condition: { state in
133133

134134
guard let agentOutcome = state.agentOutcome else {
135-
throw GraphRunnerError.executionError("'agent_outcome' property not found in state!")
135+
throw CompiledGraphError.executionError("'agent_outcome' property not found in state!")
136136
}
137137

138138
switch agentOutcome {
@@ -148,9 +148,9 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
148148

149149
try workflow.addEdge(sourceId: "call_action", targetId: "call_agent")
150150

151-
let runner = try workflow.compile()
151+
let app = try workflow.compile()
152152

153-
let result = try await runner.invoke(inputs: [ "input": input, "chat_history": [] ])
153+
let result = try await app.invoke(inputs: [ "input": input, "chat_history": [] ])
154154

155155
print( result )
156156

@@ -166,4 +166,4 @@ In the [LangChainDemo](LangChainDemo) project, you can find the porting of [Agen
166166
[langchain-swift]: https://github.com/buhe/langchain-swift.git
167167
[langchain.ai]: https://github.com/langchain-ai
168168
[langgraph]: https://github.com/langchain-ai/langgraph
169-
[AgentExecutor]: https://github.com/buhe/langchain-swift/blob/main/Sources/LangChain/agents/Agent.swift
169+
[AgentExecutor]: https://github.com/buhe/langchain-swift/blob/main/Sources/LangChain/agents/Agent.swift

0 commit comments

Comments
 (0)