跳到正文
DEV Community· Ramya Perumal·· 3 小時前AI 評分40

AI 智能體:工作流程與人類介入(HITL)

AI Agents - Workflows and Human In The Loop(HITL)

AI 導讀

LangChain 的 LCEL 透過 Runnable 組合序列及並行工作流程,Human-in-the-Loop 則讓人類審批 AI 智能體的高風險操作。文章列出鏈內審批、逐項審核工具呼叫,以及透過中介軟件暫停或恢復流程三種方式;人類可批准、拒絕、修改工具呼叫或回覆審批。

正文

LCEL - LangChain Common Expression Language

LCEL isn't specifically about using an Agent. LCEL is a way to compose LangChain components/Runnables into workflows.

In Python, everything is an object. Similarly, in LangChain, many components are implemented as Runnables, which means they can be executed and composed together.

LLM can be Runnable, or Prompt can be Runnable, or the output of the LLM can be Runnable.

Here, JSON data called "Topic" will be passed to the Prompt function that converts the JSON into messages, which will be passed to the LLM to convert the messages into AI messages, which will be passed to the parser to convert into a string. We will get the output as a string.

This line is called an assembly line or pipeline line. Runnable is not only able to take input, but also modify something and return the output.

The symbol for the pipeline is "|".

We can transform a normal function into a Runnable by using Runnable Lambda.

Runnable can have .invoke(), .batch(), .stream(), .ainvoke(), .abatch(), and .astream().

There are 2 types of workflows based on this Runnable.

1. Sequential Workflow

Follow one after another, which means Task A should be completed before Task B. So, it is a sequential order.

Topic -> Outline LLM -> Draft LLM -> Polish LLM -> Final

Two ways of calling Sequential Workflow:

# Way 1 — pipe operator (most common)
recipe_chain = prompt | model | parser

# Way 2 — explicit class (exactly the same)
recipe_chain_explicit = RunnableSequence(prompt, model, parser)

2. Parallel Workflow

Here, independent tasks will run together, and the time to finish all the tasks will be the time that the slowest task takes.

Two ways of calling Parallel Workflow:

# Way 1 — explicit class
review = RunnableParallel(pros=pros, cons=cons, verdict=verdict)

# Way 2 — a plain dict inside a chain is auto-converted to RunnableParallel
summary_chain = (
    {"pros": pros, "cons": cons, "verdict": verdict}
    | p("Write a 4-line summary.\nPros:{pros}\nCons:{cons}\nVerdict:{verdict}")
    | model
    | parser
)

Human in the Loop

A human will be involved in taking the final decision in an Agent operation. Risky operations will be sent for human approval.

Why do we need human approval? Some operations may be risky. If the Agent takes a decision without human approval, it might create side effects.

3 levels are available.

Level 1 - Approval Gateway Inside the Chain

query -> LLM -> output -> tool suggestion -> user approval -> result

Here, if the user approves, the action will be executed; otherwise, nothing will happen.

In the pipeline, if one of the parts is not executed, it will not proceed to the next part.

Here, every decision, even for a non-risky one, relies on human approval.

Level 2 - Review Tool Call by Hand

Every tool call suggested by the LLM will be reviewed by a human. Based on the human's decision, the workflow can either execute the tool, reject the tool call, or modify the tool call before execution. The workflow state or message can then be updated with the human's decision.

Level 3 - Middleware to Pause/Resume the Flow

Here, we will insert the middleware to determine which condition needs approval or can be processed without approval.

A human can approve, reject, edit, or respond to the approval.

If the human approves, the tool will be executed.

If the human rejects, a message will be sent with the ToolMessage.

If the human edits, the arguments passed to the tool will be changed according to the human-edited value.

If the human responds, the string typed by the human will be used.

來源:DEV Community · dev.to