Local LangGraph Project and Development Server
توسعه عاملهای هوشمند (Agentic Ai) با Langchain و Langgraph
- Organize a Python LangGraph project into graph, state, node, tool, and configuration modules.
- Create langgraph.json with dependencies, environment file, and compiled graph entry point.
- Run a local development server and connect a local client or Studio.
از Graph تا پروژه محلی
```mermaid
flowchart LR
A["State و Nodes"] --> B["Graph builder"]
B --> C["Compiled graph"]
C --> D["langgraph.json"]
D --> E["langgraph dev"]
E --> F["Local client یا Studio"]
```
**یک مسیر واقعی**
فایل `./my_agent/agent.py` شیء `graph` را export میکند. در `langgraph.json` نام `agent` به همین مسیر اشاره میکند؛ client نیز نام `agent` را برای اجرا میفرستد.
ساختار تمیز پروژه
```mermaid
flowchart TD
R["my-app"] --> A["agent.py"]
R --> S["state.py"]
R --> N["nodes.py"]
R --> T["tools.py"]
R --> J["langgraph.json"]
```
**مثال count**
مقدار اولیه: `count = 2`
خواندن state: `2`
محاسبه: `2 + 1 = 3`
خروجی node: `{"count": 3}`
پیکربندی و خروجی graph
**زنجیره تبدیل**
`builder`
`compile()`
`compiled_graph`
`graph = compiled_graph`
`langgraph.json → ./my_agent/agent.py:graph`
**تشخیص مسیر درست**
اگر `graph = compiled_graph` باشد، مسیر `./my_agent/agent.py:graph` درست است. مسیر `./my_agent/agent.py:builder` نادرست است، چون به مرحله پیش از `compile()` اشاره میکند.
اجرای سرور توسعه محلی
```mermaid
flowchart TD
A["langgraph dev"] --> B["خواندن پیکربندی"]
B --> C["بارگذاری graph"]
C --> D["اجرای API محلی"]
```
**بررسی مرحلهبهمرحله**
۱. اجرای `langgraph --help` برای بررسی CLI.
۲. اجرای `langgraph dev`.
۳. خواندن `langgraph.json`.
۴. پیدا کردن `graphs.agent`.
۵. نمایش API روی `http://127.0.0.1:2024`.
اتصال Client و اجرای graph
```mermaid
sequenceDiagram
participant C as Local client
participant S as Dev server
participant G as Registered graph
C->>S: threads.create()
S-->>C: thread_id
C->>S: runs.create("thread_id, agent, input")
S->>G: اجرای graph
G-->>S: خروجی
S-->>C: result
```
**درخواست کامل**
آدرس: `http://127.0.0.1:2024`
نام graph: `agent`
۱. ساخت thread و دریافت `thread_id`.
۲. ارسال `runs.create(thread_id, "agent", input)`.
۳. اجرای graph روی پیام `سلام`.
۴. ذخیره پاسخ در `result`.
**بررسی سریع**
اگر `langgraph.json` نام graph را `agent` ثبت کرده باشد، client باید همان نام `agent` را بفرستد؛ نه نام فایل Python.
Back to course