Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import osimport gradio as grfrom gradio import ChatMessageimport requestsfrom typing import Dict, Listfrom langchain_core.messages import HumanMessagefrom langchain_core.tools import toolfrom langchain_openai import ChatOpenAIfrom langgraph.checkpoint.memory import MemorySaverfrom langgraph.prebuilt import create_react_agent# Weather and location tools@tooldef get_lat_lng(location_description: str) -> dict[str, float]:"""Get the latitude and longitude of a location."""return {"lat": 51.1, "lng": -0.1} # London coordinates as dummy response@tooldef get_weather(lat: float, lng: float) -> dict[str, str]:"""Get the weather at a location."""return {"temperature": "21°C", "description": "Sunny"} # Dummy responsedef stream_from_agent(message: str, history: List[Dict[str, str]]) -> gr.ChatMessage:"""Process messages through the LangChain agent with visible reasoning."""# Initialize the agentllm = ChatOpenAI(temperature=0, model="gpt-4")memory = MemorySaver()tools = [get_lat_lng, get_weather]agent_executor = create_react_agent(llm, tools, checkpointer=memory)# Add message to historypast_messages = [HumanMessage(content=message)]for h in history:if h["role"] == "user":past_messages.append(HumanMessage(content=h["content"]))messages_to_display = []final_response = Nonefor chunk in agent_executor.stream({"messages": past_messages},config={"configurable": {"thread_id": "abc123"}}):# Handle agent's actions and tool usageif chunk.get("agent"):for msg in chunk["agent"]["messages"]:if msg.content:final_response = msg.content# Handle tool callsfor tool_call in msg.tool_calls:tool_message = ChatMessage(content=f"Parameters: {tool_call['args']}",metadata={"title": f"🛠️ Using {tool_call['name']}","id": tool_call["id"],"status": "pending",})messages_to_display.append(tool_message)yield messages_to_displaytool_message.metadata["status"] = "done"# Handle tool responsesif chunk.get("tools"):for tool_response in chunk["tools"]["messages"]:# Find the corresponding tool messagefor msg in messages_to_display:if msg.metadata.get("id") == tool_response.tool_call_id:msg.content += f"\nResult: {tool_response.content}"yield messages_to_display# Add the final response as a regular messageif final_response:messages_to_display.append(ChatMessage(content=final_response))yield messages_to_display# Create the Gradio interfacedemo = gr.ChatInterface(fn=stream_from_agent,type="messages",title="🌤️ Weather Assistant",description="Ask about the weather anywhere! Watch as I gather the information step by step.",examples=["What's the weather like in Tokyo?","Is it sunny in Paris right now?","Should I bring an umbrella in New York today?"],example_icons=["https://cdn3.iconfinder.com/data/icons/landmark-outline/432/japan_tower_tokyo_landmark_travel_architecture_tourism_view-256.png","https://cdn2.iconfinder.com/data/icons/city-building-1/200/ArcdeTriomphe-256.png","https://cdn2.iconfinder.com/data/icons/city-icons-for-offscreen-magazine/80/new-york-256.png"],save_history=True,editable=True)if __name__ == "__main__":# Load environment variablestry:from dotenv import load_dotenvload_dotenv()except ImportError:passdemo.launch(debug=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import osimport gradio as gr# Keep using gradio.ChatMessage for type hints if needed, but not for yielding complex structures directly to ChatInterface# from gradio import ChatMessage # Maybe remove this import if not used elsewhereimport requestsfrom typing import Dict, List, AsyncGenerator, Union, Tuplefrom langchain_core.messages import HumanMessage, AIMessage, ToolMessage # Use LangChain messages internallyfrom langchain_core.tools import toolfrom langchain_openai import ChatOpenAIfrom langgraph.checkpoint.memory import MemorySaverfrom langgraph.prebuilt import create_react_agent# --- Tools remain the same ---@tooldef get_lat_lng(location_description: str) -> dict[str, float]:"""Get the latitude and longitude of a location."""print(f"Tool: Getting lat/lng for {location_description}")# Replace with actual API call in a real appif "tokyo" in location_description.lower():return {"lat": 35.6895, "lng": 139.6917}elif "paris" in location_description.lower():return {"lat": 48.8566, "lng": 2.3522}elif "new york" in location_description.lower():return {"lat": 40.7128, "lng": -74.0060}else:return {"lat": 51.5072, "lng": -0.1276} # Default London@tooldef get_weather(lat: float, lng: float) -> dict[str, str]:"""Get the weather at a location."""print(f"Tool: Getting weather for lat={lat}, lng={lng}")# Replace with actual API call in a real app# Dummy logic based on latif lat > 45: # Northern locationsreturn {"temperature": "15°C", "description": "Cloudy"}elif lat > 30: # Mid locationsreturn {"temperature": "25°C", "description": "Sunny"}else: # Southern locationsreturn {"temperature": "30°C", "description": "Very Sunny"}# --- Modified Agent Function ---# Change return type hint for clarity if desired, e.g., AsyncGenerator[str, None]# Or keep it simple, Gradio infers based on yieldsasync def stream_from_agent(message: str, history: List[List[str]]) -> AsyncGenerator[str, None]:"""Processes message through LangChain agent, yielding intermediate steps as strings."""# Convert Gradio history to LangChain messageslc_messages = []for user_msg, ai_msg in history:if user_msg:lc_messages.append(HumanMessage(content=user_msg))if ai_msg:# Important: Handle potential previous intermediate strings from AI# If the ai_msg contains markers like "🛠️ Using", it was an intermediate step.# For simplicity here, we assume full AI responses were stored previously.# A more robust solution might involve storing message types in history.if not ai_msg.startswith("🛠️ Using") and not ai_msg.startswith("Result:"):lc_messages.append(AIMessage(content=ai_msg))lc_messages.append(HumanMessage(content=message))# Initialize the agent (consider initializing outside the function if stateful across calls)llm = ChatOpenAI(temperature=0, model="gpt-4")memory = MemorySaver() # Be mindful of memory state if agent is re-initialized every timetools = [get_lat_lng, get_weather]agent_executor = create_react_agent(llm, tools, checkpointer=memory)# Use a unique thread_id per session if needed, or manage state differently# Using a fixed one like "abc123" means all users share the same memory if server restarts aren't frequentthread_id = "user_session_" + str(os.urandom(4).hex()) # Example: generate unique IDfull_response = "" # Accumulate the response partsasync for chunk in agent_executor.astream_events({"messages": lc_messages},config={"configurable": {"thread_id": thread_id}},version="v1" # Use v1 for events streaming):event = chunk["event"]data = chunk["data"]if event == "on_chat_model_stream":# Stream content from the LLM (final answer parts)content = data["chunk"].contentif content:full_response += contentyield full_response # Yield the accumulating final responseelif event == "on_tool_start":# Show tool usage starttool_input_str = str(data.get('input', '')) # Get tool input safelyyield f"🛠️ Using tool: **{data['name']}** with input: `{tool_input_str}`"elif event == "on_tool_end":# Show tool result (optional, can make chat verbose)tool_output_str = str(data.get('output', '')) # Get tool output safely# Find the corresponding start message to potentially update, or just yield new message# For simplicity, just yield the result as a new message lineyield f"Tool **{data['name']}** finished.\nResult: `{tool_output_str}`"# Yield the accumulated response again after tool use in case LLM continuesif full_response:yield full_response# Ensure the final accumulated response is yielded if not already done by the last LLM chunk# (stream might end on tool end sometimes)if full_response and (not chunk or chunk["event"] != "on_chat_model_stream"):yield full_response# --- Gradio Interface (mostly unchanged) ---demo = gr.ChatInterface(fn=stream_from_agent,# No type="messages" needed when yielding strings; ChatInterface handles it.title="🌤️ Weather Assistant",description="Ask about the weather anywhere! Watch as I gather the information step by step.",examples=[["What's the weather like in Tokyo?"],["Is it sunny in Paris right now?"],["Should I bring an umbrella in New York today?"]],# Example icons removed for simplicity, ensure they are accessible if added backcache_examples=False, # Turn off caching initially to ensure it's not the issuesave_history=True,editable=True,)if __name__ == "__main__":# Load environment variablestry:from dotenv import load_dotenvprint("Attempting to load .env file...")loaded = load_dotenv()if loaded:print(".env file loaded successfully.")else:print(".env file not found or empty.")# Check if the key is loadedopenai_api_key = os.getenv("OPENAI_API_KEY")if openai_api_key:print("OPENAI_API_KEY found.")else:print("Warning: OPENAI_API_KEY not found in environment variables.")except ImportError:print("dotenv not installed, skipping .env load.")pass# Add server_name="0.0.0.0" if running in Docker or need external accessdemo.launch(debug=True, server_name="0.0.0.0")
app.py+112-72