LangChain LangGraph
Module 1 Module 2 Module 3
Module 1 · Basic abilities
1 1.1a Foundation Model
2 1.1b Prompting
3 1.2a Tools
4 1.2b Web Search
5 1.3 Memory
6 1.4 Multimodal
7 1.5 Personal Chef
SUM Module Summary
HomeLangChainModule 1 · Basic abilities1.5 Personal Chef
📓 Notebook 📖 Explained
LANGCHAIN ​​MODULE 1 · LESSON 1.5 · Practical Project

Practical Project: Personal Chef Agent
Web Search + Memory + System Prompt Comprehensive Application

The final project of Module 1 - integrating all skills together: using Tavily to search recipes, using InMemorySaver to remember user ingredients, using System Prompt to define chef roles, and build a real and usable personal chef Agent.

1Project goal: Integrate all capabilities of Module 1

Personal Chef Agent is a comprehensive practical project of Module 1, which integrates the core capabilities of all previous courses:

🧠
Model initialization (1.1a)
Initialize DeepSeek with init_chat_model
💬
System Prompt(1.1b)
Define chef roles and behavioral boundaries
🔍
Web Search (1.2b)
Tavily Search Live Recipes
💾
Memory (1.3)
InMemorySaver remembers conversation history
Why this project is valuable

Personal Chef Agent is aReal and usable applications: The user tells the Agent what ingredients are available at home, and the Agent searches for the corresponding recipes, remembers the user's ingredients in subsequent conversations, and can continue to provide suggestions and ask for specific recipes. This is the typical usage scenario of LangChain Agent - combining tool invocation, memory and role setting.

2Complete code: a combination of 5 key components

from dotenv import load_dotenv
load_dotenv()

import os
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent
from langchain.messages import HumanMessage
from langchain.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from tavily import TavilyClient

# ────────────────────────────────────
# 组件 1:模型工厂函数
# ────────────────────────────────────
def deepseek_model(**kwargs):
    return init_chat_model(
        model="deepseek-chat",
        model_provider="openai",
        api_key=os.environ["DEEPSEEK_API_KEY"],
        base_url="https://api.deepseek.com",
        max_tokens=1000,
        **kwargs
    )

# ────────────────────────────────────
# 组件 2:Web 搜索工具
# ────────────────────────────────────
tavily_client = TavilyClient()

@tool
def web_search(query: str) -> dict:
    """Search the web for information"""
    return tavily_client.search(query)

# ────────────────────────────────────
# 组件 3:System Prompt(厨师角色定义)
# ────────────────────────────────────
system_prompt = """

You are a personal chef. The user will give you a list of ingredients they have left over in their house.

Using the web search tool, search the web for recipes that can be made with the ingredients they have.

Return recipe suggestions and eventually the recipe instructions to the user, if requested.

"""

# ────────────────────────────────────
# 组件 4 + 5:Agent = 模型 + 工具 + System Prompt + 记忆
# ────────────────────────────────────
agent = create_agent(
    model=deepseek_model(),
    tools=[web_search],
    system_prompt=system_prompt,
    checkpointer=InMemorySaver()
)

3System Prompt: Define the behavior of the Chef Agent

System Prompt is the "soul" of this Agent - it defines the Agent's role, capability boundaries and response methods:

system_prompt = """

You are a personal chef. The user will give you a list of ingredients they have left over in their house.

Using the web search tool, search the web for recipes that can be made with the ingredients they have.

Return recipe suggestions and eventually the recipe instructions to the user, if requested.

"""

This prompt does three things:

  1. Define roles: "You are a personal chef" - the model knows who it is and its behavioral style will be different
  2. Define tool usage strategies:"Using the web search tool, search the web..."——Explicit instructionsModels use search tools instead of making up recipes based on LLM knowledge
  3. Define response format:"Return recipe suggestions and eventually the recipe instructions..."——Give suggestions first, and then give detailed steps after the user requests them.
Why explicit instructions to use a tool?

LLM has a lot of cooking knowledge,When not instructed to use tools, it may answer directly based on its own knowledge.- This may seem reasonable for a recipe, but may not match the user's existing ingredients, or give inaccurate instructions. Explicitly request "search the web" through the system prompt to ensure that the Agent always gives suggestions based on search results rather than generating them out of thin air.

4web_search tool: Search recipes in real time

Here'sweb_searchThe tool is slightly different than the previous one - using@toolSimplified writing method (no name specified, function name automatically used), and description comes from docstring:

@tool                    # 简化写法:name 自动为 "web_search"
def web_search(query: str) -> dict:
    """Search the web for information"""   # docstring 作为 description
    return tavily_client.search(query)
Comparison of two ways of writing @tool

Explicit writing(recommend):@tool("web_search", description="Search the web for...")
Simplified writing(this example):@tool, name = function name, description = docstring

Simplified writing is more concise, but make sure the function name is clear (web_searchinstead ofsearch), the docstring description is sufficient.

5Multi-turn dialogue demonstration: remember the ingredients and continue the dialogue

config = {"configurable": {"thread_id": "1"}}

# 轮次 1:告诉 Agent 有什么食材
response = agent.invoke(
    {"messages": [HumanMessage(content="I have some leftover chicken and rice. What can I make?")]},
    config
)
print(response['messages'][-1].content)
Chef Agent Response (Round 1)
Based on my search, here are some delicious recipes you can make with chicken and rice:

1. **Chicken Fried Rice** — A classic dish using day-old rice, diced chicken, eggs, soy sauce, and vegetables. Quick to make in under 20 minutes.

2. **Chicken and Rice Soup** — A comforting one-pot meal. Simmer chicken pieces with rice, vegetables, and broth for a warming soup.

3. **Chicken Rice Casserole** — Mix chicken, rice, cream of mushroom soup, and bake for 45 minutes. Great for meal prep!

Would you like the detailed recipe instructions for any of these?

Agent calledweb_search, searches for "chicken and rice recipes", gives 3 suggestions based on the search results, and invites the user to choose a recipe with detailed steps.

Round 2: Ask how to do it (use memory)

# 轮次 2:用户要鸡肉炒饭的做法(Agent 记住了上一轮的食材)
response2 = agent.invoke(
    {"messages": [HumanMessage(content="Can you give me the detailed recipe for chicken fried rice?")]},
    config
)
print(response2['messages'][-1].content)
Chef Agent Response (Round 2)
Here's the detailed recipe for **Chicken Fried Rice**:

**Ingredients:**
- 2 cups cooked rice (day-old works best)
- 1 cup diced chicken (cooked)
- 2 eggs, beaten
- 2 tbsp soy sauce
- 1 tbsp sesame oil
- 1 cup mixed vegetables

**Instructions:**
1. Heat oil in a wok over high heat...
...
The value of memory here

In round 2, the user directly said "give me the detailed recipe for chicken fried rice" without repeatedly mentioning "chicken and rice". The reason why Agent can understand this request and give the correct response is becauseInMemorySaver saves the conversation history of round 1——Agent knows which recipe options were discussed previously.

6In-depth analysis of Agent execution process

When the user says "I have some leftover chicken and rice. What can I make?", the complete execution process inside the Agent:

7Module 1 Knowledge Panorama Review and Next Step

At this point, all seven Lessons of Module 1 are completed. The following is a complete knowledge panorama:

LangChain Module 1 Complete Knowledge Graph
1.1a Basic model
  • init_chat_model— Unified initialization
  • create_agent— Build Agent
  • invoke / stream— Calling method
1.1b Prompt project
  • • Basic → Few-shot → Structured Prompt
  • response_format=Pydantic— Structured output
  • structured_response— Get Pydantic object
1.2a Tools
  • @toolDecorators — definition tools
  • tools=[...]— Bind to Agent
  • • ReAct loop: inference → call → inference
1.2b Web Search
  • • Tavily — Search API optimized for AI
  • • Break LLM knowledge cutoffs
  • • Live RAG: retrieval + generation
1.3 Memory
  • InMemorySaver— Memory checkpointer
  • thread_id— Distinguish conversation context
  • • Each invoke only transmits the current new message
1.4 Multimodality
  • • content list: text/picture/audio
  • • Image: base64 → Data URI
  • • Model support varies, please pay attention to model selection
Module 1 → Module 2 connection

Module 1 establishes the complete foundation of LangChain Agent: model calling, prompt engineering, tool calling, and memory. Module 2 will explore more complex Agent design patterns in depth - multi-Agent collaboration, state management, more complex memory strategies, etc. The personal chef Agent is a good starting point, and the Agent of the real project is built step by step in this way.