Kann ich dieses Verhalten unter Verwendung von Interrupts in Langgraph implementieren oder gibt es eine weitere empfohlene Möglichkeit, diese Art von Fluss mit Langgraph -Funktionen zu erreichen? Außerdem möchte ich wissen, wie man dieses Diagramm mit der Logik des Menschen in der Schleife richtig aufgerufen. < /P>
Beispiel: < /p>
Code: Select all
from langgraph.graph import StateGraph, START, END
from langgraph.types import interrupt
from typing import TypedDict, List
# Define your state
class State(TypedDict):
question: str
documents:List
user_approval: bool
def get_vector_store():
pass
# Node to fetch documents
def fetch_documents(state: State) -> State:
# Logic to fetch documents from the vector store
documents = get_vector_store(state["question"])
return {"question": state["question"], "documents": documents}
# Node for human approval
def human_approval(state: State) -> State:
if not state["documents"]:
approval = interrupt("No relevant documents found. Do you want to use the LLM instead? (yes/no)")
return {"user_approval": approval}
return state
def direct_response():
pass
# Node to call the LLM
def call_llm(state: State) -> State:
if state["user_approval"] == "yes":
response = direct_response(state["question"])
return {"response": response}
return {"response": "User declined to use the LLM."}
# Build the graph
graph = StateGraph(State)
graph.add_node("fetch_documents", fetch_documents)
graph.add_node("human_approval", human_approval)
graph.add_node("call_llm", call_llm)
# Define edges
graph.add_edge(START, "fetch_documents")
graph.add_edge("fetch_documents", "human_approval")
graph.add_edge("human_approval", "call_llm")
graph.add_edge("call_llm", END)
# Compile the graph
compiled_graph = graph.compile()