0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LLMの検証でrate limitに引っかかるのは恥ずかしいからバッチ推論しよう

Last updated at Posted at 2025-08-03

恥ずかしいコード書くやつ、いねぇよな?

LLMを使用した検証案件でこんなコード書いてる人、まさかいませんよね?


while True:
    try:
        res = inference(...)
        break
    except:
        await asyncio.sleep(n)

ないとは思いますが、このようなコードで429エラーを連発しているとしたら非常に非効率です。
リクエストをjsonlにまとめたバッチ推論でコストを抑えましょう。
ちなみに僕は書いてました。

バッチリクエストについて

各社のLLMはバッチリクエストに対応しています。
これを利用することで大量のデータを高速に処理できるだけでなく、料金が安くなる等の優遇が得られます。

注意点

jsonlに記述するリクエストは基本的にcurlでAPIを使用する際のbodyとなります。
一部、Python向けSDKとプロパティの名前が異なります。

例えばGeminiにおいてはconfigをgeneration_configに置き換える必要があるなどの差異があります。

## SDK
res = client.models.generate_content(
    model="gemini-2.5-flash",
    config=types.GenerateContentConfig(
        response_mime_type="application/json",
        response_schema=MyModel,
        thinking_config=types.ThinkingConfig(
            include_thoughts=False, thinking_budget=0
        ),
    ),
    contents=[
        {
            "parts": [
                {
                    "text": prompt,
                }
            ],
            "role": "user",
        }
    ],
)

## jsonl
req = {
    "key": key,
    "request": {
        "contents": [{"parts": [{"text": prompt}], "role": "user"}],
        "generation_config": {
            "response_mime_type": "application/json",
            "response_schema": {
                "type": "object",
                "properties": {
                    ...
                },
            },
            "thinking_config": {
                "include_thoughts": False,
                "thinking_budget": 0,
            },
        },
    },
}


OpenAIのバッチ推論ではbeta版の機能が利用できないなどの制約もあるので注意です。
かなり料金と時間の節約になるので積極的に利用しましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?