Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ ELASTICSEARCH_URL="http://localhost:9200"
DIFY_API_URL="https://api.dify.ai/v1"
DIFY_API_KEY="xxx"

## Cosmos DB Settings
## Azure Cosmos DB Settings
COSMOSDB_HOST="https://xxx.documents.azure.com:443/"
COSMOSDB_KEY="xxx"
COSMOSDB_DATABASE_NAME="langgraph"
COSMOSDB_CONTAINER_NAME="docs_kabuto"
COSMOSDB_DATABASE_NAME="template_langgraph"
COSMOSDB_CONTAINER_NAME="kabuto"
COSMOSDB_PARTITION_KEY="/id"

# ---------
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,15 @@ ci-test-docs: install-deps-docs docs ## run CI test for documentation
.PHONY: langgraph-studio
langgraph-studio: ## run LangGraph Studio
uv run langgraph dev

.PHONY: fastapi-dev
fastapi-dev: ## run FastAPI
uv run fastapi dev ./template_langgraph/services/fastapis/main.py

.PHONY: fastapi
fastapi: ## run FastAPI in production mode
uv run fastapi run \
--host "0.0.0.0" \
--port 8000 \
--workers 4 \
template_langgraph/services/fastapis/main.py
Binary file added docs/images/fastapi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 24 additions & 4 deletions docs/index.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
- [uv](https://docs.astral.sh/uv/getting-started/installation/) - モダンな Python パッケージマネージャー
- [GNU Make](https://www.gnu.org/software/make/) - 一般的なタスクの実行用
- [Docker](https://www.docker.com/) - ベクターデータベースの実行用(オプション)
- Azure
- [Azure OpenAI](https://learn.microsoft.com/ja-jp/azure/ai-foundry/openai/overview) - LLM API
- [Azure Cosmos DB](https://learn.microsoft.com/ja-jp/azure/cosmos-db/) - データストレージ(オプション)

## クイックスタート

Expand Down Expand Up @@ -129,7 +132,7 @@ Pydantic モデルを使用して AI 応答から構造化データを取得す
- **`template_langgraph/tools/`** - 検索、データ取得用ツール実装
- **`template_langgraph/utilities/`** - ドキュメント読み込みと処理用ヘルパー関数

## 例の実行
## サンプルコードの実行

### オプション 1: LangGraph Studio(開発用推奨)

Expand Down Expand Up @@ -159,7 +162,7 @@ uv run jupyter lab

![jupyterlab.png](./images/jupyterlab.png)

### オプション 3: コマンドライン(本番環境的
### オプション 3: コマンドライン(クイックな開発

ターミナルからエージェントを実行:

Expand Down Expand Up @@ -191,6 +194,23 @@ KABUTO起動時の紫画面点滅は「忍者プロトコル」初期化エラ
')]}}
```

### オプション 4: FastAPI サーバー(プロダクションレディ)

FastAPI サーバーを実行して、エージェントを API として公開:

```shell
uv run fastapi run \
--host "0.0.0.0" \
--port 8000 \
--workers 4 \
template_langgraph/services/fastapis/main.py
# http://localhost:8000/docs を開いて Swagger UI 経由で API にアクセス
```

これにより、HTTP リクエストを介してプログラム的にエージェントと対話できます。

![fastapi.png](./images/fastapi.png)

## 実演されている主要概念

### 1. **ReAct パターン**(推論 + 行動)
Expand Down Expand Up @@ -237,8 +257,8 @@ LangGraph が複数のインタラクションステップにわたってコン

## 学習リソース

- [LangGraph 文書](https://langchain-ai.github.io/langgraph/)
- [LangChain 文書](https://python.langchain.com/)
- [LangGraph 公式ドキュメント](https://langchain-ai.github.io/langgraph/)
- [LangChain 公式ドキュメント](https://python.langchain.com/)

## アーキテクチャの例

Expand Down
22 changes: 21 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ This template provides working examples of all these patterns using LangGraph.
- [uv](https://docs.astral.sh/uv/getting-started/installation/) - Modern Python package manager
- [GNU Make](https://www.gnu.org/software/make/) - For running common tasks
- [Docker](https://www.docker.com/) - For running vector databases (optional)
- Azure
- [Azure OpenAI](https://learn.microsoft.com/ja-jp/azure/ai-foundry/openai/overview) - LLM API
- [Azure Cosmos DB](https://learn.microsoft.com/ja-jp/azure/cosmos-db/) - Data storage (optional)

## Quick Start

Expand Down Expand Up @@ -159,7 +162,7 @@ uv run jupyter lab

![jupyterlab.png](./images/jupyterlab.png)

### Option 3: Command Line (Production-like)
### Option 3: Command Line (Quick development)

Run agents from the terminal:

Expand Down Expand Up @@ -191,6 +194,23 @@ The purple screen flashing during KABUTO startup indicates a "Shinobi Protocol"
')]}}
```

### Option 4: FastAPI (Production-ready)

Run the FastAPI server to expose the agent as an API:

```shell
uv run fastapi run \
--host "0.0.0.0" \
--port 8000 \
--workers 4 \
template_langgraph/services/fastapis/main.py
# Access the API at http://localhost:8000/docs via Swagger UI
```

This allows you to interact with the agent programmatically via HTTP requests.

![fastapi.png](./images/fastapi.png)

## Key Concepts Demonstrated

### 1. **ReAct Pattern** (Reasoning + Acting)
Expand Down
4 changes: 4 additions & 0 deletions docs/references.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
- [CSVLoader](https://python.langchain.com/docs/how_to/document_loader_csv/)
- [Qdrant](https://github.com/qdrant/qdrant)
- [Azure Cosmos DB No SQL](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)

### Services

- [FastAPI](https://fastapi.tiangolo.com/)
4 changes: 4 additions & 0 deletions docs/references.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
- [CSVLoader](https://python.langchain.com/docs/how_to/document_loader_csv/)
- [Qdrant](https://github.com/qdrant/qdrant)
- [Azure Cosmos DB No SQL](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)

### Services

- [FastAPI](https://fastapi.tiangolo.com/)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ requires-python = ">=3.10"
dependencies = [
"azure-cosmos>=4.9.0",
"elasticsearch>=9.1.0",
"fastapi[standard]>=0.116.1",
"httpx>=0.28.1",
"langchain-azure-ai>=0.1.4",
"langchain-community>=0.3.27",
Expand Down
7 changes: 6 additions & 1 deletion template_langgraph/loggers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import logging


def get_logger(name: str = "default") -> logging.Logger:
def get_logger(
name: str = "default",
verbosity: int = logging.INFO,
) -> logging.Logger:
"""
Get a logger with the specified name.

Args:
name (str): The name of the logger.
verbosity (int): The logging level (default: logging.INFO).
Returns:
logging.Logger: Configured logger instance.
"""
logger = logging.getLogger(name)
logger.setLevel(verbosity)
formatter = logging.Formatter("%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
Expand Down
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions template_langgraph/services/fastapis/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from fastapi import FastAPI

from template_langgraph.services.fastapis.routers import agents as agents_router

app = FastAPI()


app.include_router(
agents_router.router,
prefix="/agents",
tags=["agents"],
)
Empty file.
56 changes: 56 additions & 0 deletions template_langgraph/services/fastapis/routers/agents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging

from fastapi import APIRouter
from pydantic import BaseModel, ConfigDict

from template_langgraph.agents.chat_with_tools_agent.agent import AgentState
from template_langgraph.agents.chat_with_tools_agent.agent import graph as chat_with_tools_agent
from template_langgraph.loggers import get_logger

router = APIRouter()
logger = get_logger(
name=__name__,
verbosity=logging.DEBUG,
)


class RunChatWithToolsAgentRequest(BaseModel):
model_config = ConfigDict(extra="ignore")
question: str


class RunChatWithToolsAgentResponse(BaseModel):
model_config = ConfigDict(extra="ignore")
response: str


@router.post(
"/chat_with_tools_agent/",
response_model=RunChatWithToolsAgentResponse,
operation_id="create_transcription_job",
)
async def run_chat_with_tools_agent(
request: RunChatWithToolsAgentRequest,
) -> RunChatWithToolsAgentResponse:
try:
async for event in chat_with_tools_agent.astream(
input=AgentState(
messages=[
{
"role": "user",
"content": request.question,
},
],
),
config={
"recursion_limit": 30,
},
):
logger.debug(f"Event received: {event}")
response = event["chat_with_tools"]["messages"][0].content
return RunChatWithToolsAgentResponse(response=response)
except Exception as e:
logger.error(f"Error processing event: {e}")
return RunChatWithToolsAgentResponse(
response=f"An error occurred while processing your request with {e}",
)
Loading