Fastapi Serviceのロゴ

Fastapi Service

コミュニティ
0xBakeer
fastapi-service

Writing a Python HTTP API with FastAPI (or Flask) - pydantic models, routers, validation errors, status codes, dependencies, settings from the environment, running with uvicorn and checking routes with curl.

概要

公開者0xBakeer
リポジトリTandemLLM
スキル名fastapi-service
スター
75
フォーク
14
同梱ファイル
指示のみ
ライセンスAGPL-3.0
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    0xBakeerがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキルFastapi ServiceをTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/0xBakeer/TandemLLM/tree/main/packages/library/fastapi-service
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/0xBakeer/TandemLLM.git /tmp/TandemLLM
mkdir -p .claude/skills
cp -r /tmp/TandemLLM/packages/library/fastapi-service .claude/skills/fastapi-service
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでFastapi Serviceを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあると、モデルが自動的にFastapi Serviceを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Fastapi Serviceは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、未使用のスキルにはコストがかかりません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

FastAPI service

Rules:

  • Layout: app/main.py (app + routers), app/models.py (pydantic in/out), app/db.py, app/routes/*.py. One router per resource.
  • Every request body and response is a pydantic model. Return response_model= so the output is validated and documented.
  • Status codes on purpose: 201 on create, 204 on delete, 404 with HTTPException(404, "item not found"), 422 left to pydantic.
  • Settings from the environment (pydantic-settings), never hard-coded; .env ignored by git.
  • Run: uvicorn app.main:app --port 8000 > {tandem_scratch}/run.log 2>&1 &, then curl /healthz. Add /healthz and /__state.

What goes wrong:

  • async def route calling a sync database driver; the whole server stalls.
  • 500 on a known error path; map it to 400/404/409 with a JSON body.
  • Mutable default or module-level state shared across requests.

Check: curl each route: a valid call, an invalid body (422), a missing id (404); read status and body each time; read the log tail; stop the server at the end.

Done when:

  • Log line shows uvicorn listening; /healthz returns 200 (curl output in the result).
  • For every route the status and body for valid, invalid and not-found calls are in the result.
  • Create then read-back agrees; the server you started is stopped.

Reference

python
# app/main.py
from fastapi import FastAPI, HTTPException, status
from pydantic import BaseModel, Field
app = FastAPI()
class ItemIn(BaseModel):
    text: str = Field(min_length=1, max_length=200)
class Item(ItemIn):
    id: int
DB: dict[int, Item] = {}
@app.get("/healthz")
def healthz(): return {"ok": True}
@app.get("/__state")
def state(): return {"tandem_state": 1, "items": len(DB), "routes": [r.path for r in app.routes]}
@app.post("/items", response_model=Item, status_code=status.HTTP_201_CREATED)
def create(body: ItemIn):
    item = Item(id=len(DB) + 1, **body.model_dump()); DB[item.id] = item; return item
@app.get("/items/{item_id}", response_model=Item)
def get(item_id: int):
    if item_id not in DB: raise HTTPException(404, "item not found")
    return DB[item_id]

Run and probe:

bash
python -m venv .venv && . .venv/bin/activate && pip install fastapi uvicorn
(uvicorn app.main:app --port 8000 > {tandem_scratch}/run.log 2>&1 &) ; sleep 1
curl -s -o /dev/null -w '%{http_code}\n' localhost:8000/healthz
curl -s -X POST localhost:8000/items -H 'Content-Type: application/json' -d '{"text":"a"}'
curl -s -w ' %{http_code}\n' -X POST localhost:8000/items -H 'Content-Type: application/json' -d '{"text":""}'
curl -s -w ' %{http_code}\n' localhost:8000/items/99
tail -n 20 {tandem_scratch}/run.log; pkill -f 'uvicorn app.main'

Settings: class Settings(BaseSettings): db_url: str = "sqlite:///app.db" with model_config = SettingsConfigDict(env_file=".env").

More rules:

  • Blocking work (files, CPU) in def routes or a threadpool; async def only with async libraries.

More pitfalls:

  • Port in use from a previous run; check lsof -i :8000 and kill the old process.
  • Flask alternative: the same rules; validate by hand or with marshmallow, run with flask --app app run --port 8000.

よくある質問

AIスキル「Fastapi Service」では何ができますか?

Writing a Python HTTP API with FastAPI (or Flask) - pydantic models, routers, validation errors, status codes, dependencies, settings from the environment, running with uvicorn and checking routes with curl.

TypingMindでFastapi Serviceを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Fastapi Serviceはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにFastapi Serviceをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/0xBakeer/TandemLLM/tree/main/packages/library/fastapi-serviceを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Fastapi Serviceを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストが合致したときにこのスキルを読み込めます。

Fastapi Serviceは何個のAIモデルで使えますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもFastapi Serviceを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Fastapi Service」は無料ですか?

はい。0xBakeerがGitHubでAGPL-3.0ライセンスの下で公開しています。支払いが必要なのは、利用したトークンに対する、ご自身が契約するAIプロバイダーへの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇