Request for converting LangGraph output to Chainlit UI tool - Help Needed #27724
Replies: 1 comment
-
To convert the LangGraph output to be displayed in the Chainlit UI, you need to adapt the LangGraph output to the format expected by Chainlit. Here's a step-by-step guide to achieve this:
Here's how you can modify your existing code to achieve this: from langgraph.prebuilt import create_react_agent
from langgraph.errors import GraphRecursionError
from langchain_core.tools import tool
from langgraph.checkpoint.memory import MemorySaver
import chainlit as cl
# Initialize your model and other variables
RECURSION_LIMIT = 2 * 15 + 1
model = llm
query = output_query_str
prompts = [prompt_repl_lg]
tools = [
repl,
retriever_tool_n,
add,
subtract,
multiply,
divide,
]
def _modify_state_messages(state: AgentState):
modified_messages = state["messages"]
for prompt in prompts:
modified_messages = prompt.invoke({"messages": modified_messages}).to_messages()
modified_messages.append(("user", "Also say 'Pandamonium!' after the answer."))
return modified_messages
langgraph_agent_executor = create_react_agent(
model, tools, state_modifier=_modify_state_messages
)
@cl.on_chat_start
def setup_langgraph():
cl.user_session.set("langgraph_agent_executor", langgraph_agent_executor)
@cl.on_message
async def handle_message(message: cl.Message):
langgraph_agent_executor = cl.user_session.get("langgraph_agent_executor")
try:
# Invoke the LangGraph agent executor
result = langgraph_agent_executor.invoke(
{
"messages": [("human", message.content)],
"recursion_limit": RECURSION_LIMIT,
}
)
# Extract the last message content
response_content = result["messages"][-1].content
except GraphRecursionError:
response_content = "Agent stopped due to max iterations."
# Send the response to Chainlit UI
await cl.Message(response_content).send()
# Run Chainlit
# chainlit run your_script.py -w --port 8000 Key Points:
This setup will allow you to convert the LangGraph output into a format that can be displayed in the Chainlit UI, similar to how you handle messages with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My objective here is to convert the LangGraph output (enclosed in my LangGraph react code below) into a Chainlit message and display it in the Chainlit UI. I have found multiple samples that mainly focus on llm_chain.acall (code enclosed below). My request convert my LangGraph output message and simulate a similar template code provided with LangChain. Please help. At the end I like to see my LangGraph ouput in chainlit (what I currently seeing via llm_chain).
My LangGraph code is below:
The sample code I got for Chainlit to conver llm_chain to Chainlit application is below:
Beta Was this translation helpful? Give feedback.
All reactions