# 知識検索 API ユーザーガイド

## リクエストエンドポイント

```
POST /external/use/seek-knowledge HTTP/1.1
```

## リクエストヘッダー

| ヘッダー            | 値                     | 説明                                                 |
| --------------- | --------------------- | -------------------------------------------------- |
| `Authorization` | `Bearer <your_token>` | 認証トークン、ユーザー識別のため。`<your_token>`を実際のトークンに置き換えてください。 |
| `Content-Type`  | `application/json`    | リクエストボディがJSON形式であることを示します。                         |
| `Host`          | `edge.flowith.net`    | APIサーバーアドレス。                                       |
| `User-Agent`    | `HTTPie`              | （オプション）クライアント識別子。                                  |

## リクエストボディ（JSON）

| パラメータ      | 型     | 説明                                                            |
| ---------- | ----- | ------------------------------------------------------------- |
| `messages` | 配列    | 会話メッセージリスト。各メッセージオブジェクトには`role`（役割）と`content`（内容）フィールドが含まれます。 |
| `model`    | 文字列   | モデル名。例：`"gpt-4o-mini"`                                        |
| `stream`   | ブール値  | ストリーミングレスポンスを有効にするか。`true`で有効、`false`で無効。                     |
| `kb_list`  | 文字列配列 | ナレッジベースIDリスト。知識検索の範囲を指定します。                                   |

### **`messages`フィールドの詳細**

* `role`: メッセージの役割、`"assistant"`または`"user"`が可能。
* `content`: メッセージ内容、高度なクエリや指示など。

## レスポンス

### **`stream`が`true`の場合（ストリーミングレスポンス）**

* レスポンスステータスコード: `200 OK`
* レスポンスヘッダー: `Content-Type: text/plain; charset=UTF-8`などの標準HTTPヘッダーを含む。
* レスポンスボディ: `data:`で始まるテキストストリーム。各`data:`ブロックには以下の可能な`tag`値を持つJSONオブジェクトが含まれます：
  * `searching`: 検索中を示し、`content`フィールドには進捗状況が含まれます。
  * `seeds`: 中間検索結果、`content`は各要素が`id`、`tokens`、`content`、`order`、`source_id`、`source_title`、`nip`を含む個別の知識であるJSON配列です。
  * `final`: 最終結果を示し、`content`には完全なレスポンステキストが含まれます。

### **`stream`が`false`の場合**

* レスポンスステータスコード: `200 OK`
* レスポンスヘッダー: `Content-Type: application/json`などの標準HTTPヘッダーを含む。
* レスポンスボディ: `tag`と`content`フィールドを含むJSONオブジェクト。
  * `tag`: 常に`"final"`。
  * `content`: 完全なレスポンステキスト。

## `curl`の例

### **ストリーミングレスポンス (stream = true)**

```bash
curl -X POST \
  -H "Authorization: Bearer <your_token>" \
  -H "Content-Type: application/json" \
  -H "Host: edge.flowith.net" \
  -d '{
    "messages": [
      {"role": "assistant", "content": "I'd like to explore effective social dynamics and interpersonal relationships"},
      {"role": "user", "content": "Share your best strategies for authentic connection and building rapport in professional settings"}
    ],
    "model": "gpt-4o-mini",
    "stream": true,
    "kb_list": ["<knowledge_base_id>"]
  }' \
  https://edge.flowith.net/external/use/seek-knowledge
```

### **非ストリーミングレスポンス (stream = false)**

```bash
curl -X POST \
  -H "Authorization: Bearer <your_token>" \
  -H "Content-Type: application/json" \
  -H "Host: edge.flowith.net" \
  -d '{
    "messages": [
      {"role": "assistant", "content": "I'd like to explore effective social dynamics and interpersonal relationships"},
      {"role": "user", "content": "Share your best strategies for authentic connection and building rapport in professional settings"}
    ],
    "model": "gpt-4o-mini",
    "stream": false,
    "kb_list": ["<knowledge_base_id>"]
  }' \
  https://edge.flowith.net/external/use/seek-knowledge
```

`<your_token>`と`<knowledge_base_id>`を実際の値に置き換えることを忘れないでください。

## レート制限

* リクエスト制限：1分間に12リクエストまで(RPM)
* 注意事項：制限を超えた場合、HTTP 429エラー(リクエスト過多)が返されます

## 課金について

* クォータ使用量：対応するFlowithアカウントのT1/T2モデルクォータに基づいて計算されます
* 請求サイクル：使用量はリアルタイムで計算され、アカウント残高から差し引かれます
