# app.py (or your main Gradio script) import gradio as gr from backend import chatbot from langchain_core.messages import HumanMessage, BaseMessage, AIMessage def respond_stream(message, history): # Convert history to LangChain format messages = [] for human, ai in history: messages.append(HumanMessage(content=human)) messages.append(ai) # Add the current message messages.append(HumanMessage(content=message)) # Stream the response config = {"configurable": {"thread_id": "1"}} full_response = "" # Stream mode 'messages' returns tuples of (message, metadata) for chunk, metadata in chatbot.stream( {"messages": messages}, config=config, stream_mode="messages" ): # Extract content from the chunk if hasattr(chunk, 'content'): full_response += chunk.content yield full_response elif isinstance(chunk, dict) and 'content' in chunk: full_response += chunk['content'] yield full_response def main(): # Create the Gradio ChatInterface using the streaming function demo = gr.ChatInterface( fn=respond_stream, # Use the streaming function # Note: The 'type' parameter for ChatInterface history format is usually handled internally # based on the function signature or defaults. Specifying it might be unnecessary or outdated. title="Streaming ChatBot", description="Chatbot using langgraph backend with streaming output.", examples=[["Hello"], ["How are you?"], ["Tell me a joke."]] # Add examples ) # Launch with share=True to get a public link if needed demo.launch(share=True) if __name__ == "__main__": main()