1). AI Chat Bot
- Project Overview
- Goal
- Data Structure
- Tools
- How It Works
- Setup & Usage
- Limitations
- Possible Extensions
A Gradio chat application that plays the role of an Amazon customer assistant. It wraps a LangChain tool-calling agent, backed by an LLM served through OpenRouter, that answers questions about product price, specs, rating, and stock status, and recommends products from a small built-in catalog.
- Primary Objective: given a natural-language question about a product, return an accurate answer grounded in the catalog (via tool calls), not a hallucinated one.
- Secondary Objective: demonstrate a LangChain
create_tool_calling_agent+AgentExecutorwired to a GradioBlockschat UI, with two custom tools over a mocked dataset.
DEFAULT_AMAZON_CATALOG — a hardcoded list of 10 product dicts, each with:
asin,title,category,price,rating,in_stock,description
If a file named amazon_products.json exists in the working directory (same shape as the list above), load_amazon_data() loads that instead of the default catalog.
- Python — application logic
- Gradio (
gr.Blocks,gr.Chatbot) — chat UI - LangChain (
langchain-openai,langchain-core,langchain-classic) —create_tool_calling_agent+AgentExecutor - OpenRouter API — LLM backend (
ChatOpenAIpointed athttps://openrouter.ai/api/v1), modelanthropic/claude-3-haiku - python-dotenv — environment variable loading
Two LangChain tools exposed to the agent:
search_amazon_product(query)— matches by title, ASIN, category, or descriptionget_recommendations_by_criteria(category, max_price)— filters and sorts by stock + rating
agent_with_memory.py → loads catalog, defines the two tools, builds the system prompt and
prompt template, exposes AmazonChatSession.chat(user_message)
app.py → Gradio Blocks UI: textbox + Chatbot + Clear button, calls
agent.chat() on submit and appends the exchange to chat_history
Request flow: user types a message → app.py respond() → AmazonChatSession.chat() → AgentExecutor.invoke() (LLM decides whether to call a tool) → tool result (if any) fed back to the LLM → final answer returned → rendered in the Gradio chat window.
There's no requirements.txt in the repo, so install directly from the imports:
pip install gradio python-dotenv langchain-openai langchain-core langchain-classicCreate a .env file with:
OPEN_API_KEY=your_openrouter_api_key
Then run:
python app.pyThis launches a local Gradio server (default http://127.0.0.1:7860) and, because share=True is set, also prints a temporary public link.
- No
requirements.txtor.env.examplechecked in — dependencies and the required env var have to be reverse-engineered from the source. - The env var is named
OPEN_API_KEY, not the conventionalOPENAI_API_KEY, and the key it expects is an OpenRouter key, not an OpenAI one — easy to misconfigure. - Despite the filename
agent_with_memory.pyand the classAmazonChatSession, there is no real conversation memory:chat()callsexecutor.invoke({"input": user_message})fresh each turn with no prior turns passed in. The Gradiochat_historyis display-only; the agent never sees it. share=Trueis hardcoded, so every run ofpython app.pygenerates a public URL — convenient for a demo, not something to leave running.- No handling for a missing/invalid API key beyond letting the exception string surface as a chat message.
- The catalog is a static mock (10 hardcoded products, or an optional local JSON file) — not connected to any real Amazon data or API.
- Add a
requirements.txt(orpyproject.toml) and a.env.example. - Wire actual conversation memory: pass prior turns into
agent_scratchpador attach a memory/history object so follow-up questions work. - Rename
OPEN_API_KEYto something unambiguous, or add a comment noting it's an OpenRouter key. - Add tests for
search_amazon_productandget_recommendations_by_criteriaagainst the catalog. - Make
shareconfigurable via an env var instead of hardcodedTrue.