Skip to content

Commit 538c18d

Browse files
authored
Merge pull request #39 from ks6088ts-labs/feature/issue-30_fastapi
add FastAPI server for running agents
2 parents ee869f1 + d5c4e52 commit 538c18d

File tree

15 files changed

+508
-14
lines changed

15 files changed

+508
-14
lines changed

.env.template

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ ELASTICSEARCH_URL="http://localhost:9200"
3333
DIFY_API_URL="https://api.dify.ai/v1"
3434
DIFY_API_KEY="xxx"
3535

36-
## Cosmos DB Settings
36+
## Azure Cosmos DB Settings
3737
COSMOSDB_HOST="https://xxx.documents.azure.com:443/"
3838
COSMOSDB_KEY="xxx"
39-
COSMOSDB_DATABASE_NAME="langgraph"
40-
COSMOSDB_CONTAINER_NAME="docs_kabuto"
39+
COSMOSDB_DATABASE_NAME="template_langgraph"
40+
COSMOSDB_CONTAINER_NAME="kabuto"
4141
COSMOSDB_PARTITION_KEY="/id"
4242

4343
# ---------

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,15 @@ ci-test-docs: install-deps-docs docs ## run CI test for documentation
115115
.PHONY: langgraph-studio
116116
langgraph-studio: ## run LangGraph Studio
117117
uv run langgraph dev
118+
119+
.PHONY: fastapi-dev
120+
fastapi-dev: ## run FastAPI
121+
uv run fastapi dev ./template_langgraph/services/fastapis/main.py
122+
123+
.PHONY: fastapi
124+
fastapi: ## run FastAPI in production mode
125+
uv run fastapi run \
126+
--host "0.0.0.0" \
127+
--port 8000 \
128+
--workers 4 \
129+
template_langgraph/services/fastapis/main.py

docs/images/fastapi.png

205 KB
Loading

docs/index.ja.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
- [uv](https://docs.astral.sh/uv/getting-started/installation/) - モダンな Python パッケージマネージャー
3636
- [GNU Make](https://www.gnu.org/software/make/) - 一般的なタスクの実行用
3737
- [Docker](https://www.docker.com/) - ベクターデータベースの実行用(オプション)
38+
- Azure
39+
- [Azure OpenAI](https://learn.microsoft.com/ja-jp/azure/ai-foundry/openai/overview) - LLM API
40+
- [Azure Cosmos DB](https://learn.microsoft.com/ja-jp/azure/cosmos-db/) - データストレージ(オプション)
3841

3942
## クイックスタート
4043

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

132-
## 例の実行
135+
## サンプルコードの実行
133136

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

@@ -159,7 +162,7 @@ uv run jupyter lab
159162

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

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

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

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

197+
### オプション 4: FastAPI サーバー(プロダクションレディ)
198+
199+
FastAPI サーバーを実行して、エージェントを API として公開:
200+
201+
```shell
202+
uv run fastapi run \
203+
--host "0.0.0.0" \
204+
--port 8000 \
205+
--workers 4 \
206+
template_langgraph/services/fastapis/main.py
207+
# http://localhost:8000/docs を開いて Swagger UI 経由で API にアクセス
208+
```
209+
210+
これにより、HTTP リクエストを介してプログラム的にエージェントと対話できます。
211+
212+
![fastapi.png](./images/fastapi.png)
213+
194214
## 実演されている主要概念
195215

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

238258
## 学習リソース
239259

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

243263
## アーキテクチャの例
244264

docs/index.md

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

3942
## Quick Start
4043

@@ -159,7 +162,7 @@ uv run jupyter lab
159162

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

162-
### Option 3: Command Line (Production-like)
165+
### Option 3: Command Line (Quick development)
163166

164167
Run agents from the terminal:
165168

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

197+
### Option 4: FastAPI (Production-ready)
198+
199+
Run the FastAPI server to expose the agent as an API:
200+
201+
```shell
202+
uv run fastapi run \
203+
--host "0.0.0.0" \
204+
--port 8000 \
205+
--workers 4 \
206+
template_langgraph/services/fastapis/main.py
207+
# Access the API at http://localhost:8000/docs via Swagger UI
208+
```
209+
210+
This allows you to interact with the agent programmatically via HTTP requests.
211+
212+
![fastapi.png](./images/fastapi.png)
213+
194214
## Key Concepts Demonstrated
195215

196216
### 1. **ReAct Pattern** (Reasoning + Acting)

docs/references.ja.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
- [CSVLoader](https://python.langchain.com/docs/how_to/document_loader_csv/)
2121
- [Qdrant](https://github.com/qdrant/qdrant)
2222
- [Azure Cosmos DB No SQL](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)
23+
24+
### Services
25+
26+
- [FastAPI](https://fastapi.tiangolo.com/)

docs/references.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
- [CSVLoader](https://python.langchain.com/docs/how_to/document_loader_csv/)
2121
- [Qdrant](https://github.com/qdrant/qdrant)
2222
- [Azure Cosmos DB No SQL](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)
23+
24+
### Services
25+
26+
- [FastAPI](https://fastapi.tiangolo.com/)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ requires-python = ">=3.10"
77
dependencies = [
88
"azure-cosmos>=4.9.0",
99
"elasticsearch>=9.1.0",
10+
"fastapi[standard]>=0.116.1",
1011
"httpx>=0.28.1",
1112
"langchain-azure-ai>=0.1.4",
1213
"langchain-community>=0.3.27",

template_langgraph/loggers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import logging
22

33

4-
def get_logger(name: str = "default") -> logging.Logger:
4+
def get_logger(
5+
name: str = "default",
6+
verbosity: int = logging.INFO,
7+
) -> logging.Logger:
58
"""
69
Get a logger with the specified name.
710
811
Args:
912
name (str): The name of the logger.
13+
verbosity (int): The logging level (default: logging.INFO).
1014
Returns:
1115
logging.Logger: Configured logger instance.
1216
"""
1317
logger = logging.getLogger(name)
18+
logger.setLevel(verbosity)
1419
formatter = logging.Formatter("%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)")
1520
handler = logging.StreamHandler()
1621
handler.setFormatter(formatter)

template_langgraph/services/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)