Spaces:
Running
Running
""" | |
FoodWise Inventory Agent using LangGraph's prebuilt React Agent. | |
This module creates and configures the inventory management agent using | |
LangGraph's create_react_agent() function with Notion-based tools. | |
""" | |
import os | |
from typing import Dict, Any | |
from langchain_openai import ChatOpenAI | |
from langgraph.prebuilt import create_react_agent | |
from langsmith import Client | |
from .tools import get_inventory_tools | |
from .prompts import INVENTORY_AGENT_PROMPT | |
def create_inventory_agent( | |
model_name: str = "gpt-4.1", | |
temperature: float = 0.1, | |
enable_tracing: bool = True | |
) -> Any: | |
""" | |
Creates and configures the FoodWise inventory agent using LangGraph's prebuilt React agent. | |
This function initializes the language model, loads the inventory management tools, | |
and sets up the agent for use with the FoodWise Notion database. Optionally enables | |
LangSmith tracing for debugging and monitoring. | |
Args: | |
model_name: The LLM model to use (default: gpt-4o-mini) | |
temperature: LLM temperature setting (default: 0.1 for consistency) | |
enable_tracing: Whether to enable LangSmith tracing | |
Returns: | |
Any: A configured LangGraph React agent instance ready for execution. | |
""" | |
# Initialize LLM | |
llm = ChatOpenAI( | |
model=model_name, | |
temperature=temperature | |
) | |
# Load inventory management tools | |
tools = get_inventory_tools() | |
# Create the React agent with tools (system prompt handled in run_agent_query) | |
agent = create_react_agent( | |
model=llm, | |
tools=tools | |
) | |
# Optionally enable LangSmith tracing for observability | |
if enable_tracing and os.getenv("LANGCHAIN_API_KEY"): | |
os.environ["LANGCHAIN_TRACING_V2"] = "true" | |
os.environ["LANGCHAIN_PROJECT"] = "FoodWise-Inventory-Agent" | |
return agent | |
def run_agent_query(query: str, agent: Any = None) -> Dict[str, Any]: | |
""" | |
Execute a single query against the inventory agent. | |
Args: | |
query: User query/message | |
agent: Pre-configured agent (will create one if None) | |
Returns: | |
Agent response including messages and any tool results | |
""" | |
if agent is None: | |
agent = create_inventory_agent() | |
# Execute the agent with system prompt and user query | |
result = agent.invoke({ | |
"messages": [ | |
("system", INVENTORY_AGENT_PROMPT), | |
("user", query) | |
] | |
}) | |
return result | |
if __name__ == "__main__": | |
# Quick test of the agent | |
test_agent = create_inventory_agent() | |
response = run_agent_query("What items are expiring soon?", test_agent) | |
print("Agent Response:", response) | |