You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in the langgraph, along with the streaming chat response, want to send some additional information to be displayed.
for eg use case is tool calls created files, and the list of files to be displayed seperately.
how to do that?
`from typing import Dict, TypedDict, Annotated
from langchain_core.messages import HumanMessage
from langgraph.graph import END, Graph
import operator
from langchain_openai import ChatOpenAI
import json
from dotenv import load_dotenv
import os
from langgraph.config import get_stream_writer
Load environment variables
load_dotenv('./be/.env')
Define our state
class State(TypedDict):
messages: list
current_step: str
Create processing nodes
def process_input(state: State):
"""First node: Process the input message"""
current_message = state["messages"][-1].content
return {"messages": state["messages"], "current_step": f"Processing: {current_message}"}
def analyze_text(state: State):
"""Second node: Analyze the text"""
# Use API key from environment variables
llm = ChatOpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0,
)
messages = state["messages"]
response = llm.invoke([HumanMessage(content=f"Analyze this text briefly: {messages[-1].content}")])
writer = get_stream_writer()
writer({"custom_key": "Generating custom data inside node"})
response.additional_kwargs = {"custom_key": "Generating custom data inside node"}
for message_chunk, metadata in graph.stream(input_state,
stream_mode="messages"
):
if message_chunk.content:
print(message_chunk.content, end="|", flush=True)
print(message_chunk.additional_kwargs)
if name == "main":
run_graph("The quick brown fox jumps over the lazy dog.")
`
in the above code snippet, tried to do with response.additional_kwargs, but looks like that is not the way.
any suggestions
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
in the langgraph, along with the streaming chat response, want to send some additional information to be displayed.
for eg use case is tool calls created files, and the list of files to be displayed seperately.
how to do that?
`from typing import Dict, TypedDict, Annotated
from langchain_core.messages import HumanMessage
from langgraph.graph import END, Graph
import operator
from langchain_openai import ChatOpenAI
import json
from dotenv import load_dotenv
import os
from langgraph.config import get_stream_writer
Load environment variables
load_dotenv('./be/.env')
Define our state
class State(TypedDict):
messages: list
current_step: str
Create processing nodes
def process_input(state: State):
"""First node: Process the input message"""
current_message = state["messages"][-1].content
return {"messages": state["messages"], "current_step": f"Processing: {current_message}"}
def analyze_text(state: State):
"""Second node: Analyze the text"""
# Use API key from environment variables
llm = ChatOpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
temperature=0,
)
messages = state["messages"]
response = llm.invoke([HumanMessage(content=f"Analyze this text briefly: {messages[-1].content}")])
writer = get_stream_writer()
writer({"custom_key": "Generating custom data inside node"})
response.additional_kwargs = {"custom_key": "Generating custom data inside node"}
Build the graph
def build_graph():
# Create workflow
workflow = Graph()
Create the app
graph = build_graph().compile()
Example usage
def run_graph(input_text: str):
input_state = State(
messages=[HumanMessage(content=input_text)],
current_step=""
)
if name == "main":
run_graph("The quick brown fox jumps over the lazy dog.")
`
in the above code snippet, tried to do with response.additional_kwargs, but looks like that is not the way.
any suggestions
Beta Was this translation helpful? Give feedback.
All reactions