Skip to content
雨天的烟花
Go back

快速了解LangChain

编辑文章

快速了解LangChain

Agent + Tools + Workflow + LangGraph

Get Started,开始

安装相应的库

第一步:创建Agent(智能体)

构建一个基本的智能体,A basic agent

from langchain.agents import create_agent

def get_weather(city: str) -> str:
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"

agent = create_agent(
    model="google_genai:gemini-2.5-flash-lite",
    tools=[get_weather],
    system_prompt="You are a helpful assistant",
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
)

print(result["messages"][-1].content_blocks)

AI Agent的四大组件:

Model      = 大脑
Tool       = 手脚
Prompt     = 规则人格
Invoke     = 执行动作

Docstring:"""Get weather for a given city."""这是给LLM看的工具说明,模型会读它,然后理解:

这是一个查询天气的工具

不能做:

Build a real-world agent

real-world agent:多工具 + memory + workflow

  1. Detailed system prompts for better agent behavior 行为控制层

    更强的系统提示词:从原先的”You are a helpful assistant”到而今的

    定义角色 + 行为规范 + 输出结构 + 工具使用策略

    你是谁 + 不能做什么 + 工具使用规则 + 输出格式 + 决策策略

    这样就更像一个系统

    本质:控制 AI 行为的“操作系统”

    • 控制风格
    • 控制是否调用 tool
    • 控制输出结构
    • 限制 hallucination
  2. Create tools that integrate with external data外部能力层

    本质:给 AI 接上现实世界能力

    多工具相当于手,用来行动做事

  3. Model configuration for consistent responses稳定控制层

    本质:让 AI 稳定,而不是随机

    temperature = 0.1
    max_tokens = 2000
    timeout = 60

!agent是非确定性系统

必须进行测试


编辑文章
Share this post on:

Previous Post
小知识点
Next Post
虚拟环境