nikkie-ftnextの日記

イベントレポートや読書メモを発信

Hugging FaceのText Generation InferenceのGuidance、ユーザが望む出力形式を実現する仕組みの概略

Today I Learned1です。

目次

Hugging FaceのText Generation Inference

A Rust, Python and gRPC server for text generation inference.

Text Generation Inference (TGI) is a toolkit for deploying and serving Large Language Models (LLMs).

Hugging Face🤗製、テキスト生成するモデルのサービングツールととらえています。
Hugging Faceのサイトでも使われているらしく、実績は十分ですね。

豊富な機能の中から今回取り上げるのはこちら:

Guidance/JSON. Specify output format to speed up inference and make sure the output is valid according to some specs.. (GitHubより)

Guidance (Tutorialより)

huggingface.co

TGI 1.4.3以降でサポートされました。

Text Generation Inference (TGI) now supports JSON and regex grammars and tools and functions to help developers guide LLM responses to fit their needs.

TGIで立てたサーバの/genarategrammarパラメタを渡せます!
チュートリアルの例がこちら

curl localhost:3000/generate \
    -X POST \
    -H 'Content-Type: application/json' \
    -d '{
    "inputs": "I saw a puppy a cat and a raccoon during my bike ride in the park",
    "parameters": {
        "repetition_penalty": 1.3,
        "grammar": {
            "type": "json",
            "value": {
                "properties": {
                    "location": {
                        "type": "string"
                    },
                    "animals_seen": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 5
                    },
                },
                "required": ["location", "animals_seen"]
            }
        }
    }
}'

これで出力はJSONになります
(OpenAIのGPTのJSON mode2とも関係するのかな?)

note: guidance is supported as grammar in the /generate endpoint and as tools in the /chat/completions endpoint.

TGIはOpenAIのAPI互換の/chat/completionsエンドポイントも生やせるのですが、そちらではtoolsパラメタとのことです。

チュートリアルにはさまざまな例が掲載されています(Pydanticを使う例などなど)

ドキュメントより、Guidanceの概念

チュートリアルの中の「How it works」によるとoutlinesというライブラリを使っているようです。

TGI leverages the outlines library to efficiently parse and compile the grammatical structures and tools specified by users.

🔥 Fast JSON generation following a JSON schema or a Pydantic model (outlinesのGitHubより)

Guidanceの仕組みに興味をもった人向けのドキュメントを確認しましょう。

huggingface.co

How is it used?(どのように使われるのか)

以下のようなユースケースにも

  • 非構造化されたテキストから構造化されたデータを抽出
  • テキストを特定の形式に要約
  • (などなど)

OpenAIのfunction callingと重なりますね3
fucntion callingは、Guidanceの1つの具体例ってことなんですかね?

How it works?(仕組み)

LLMは続く単語の確率分布を返し、そこから次の語を選ぶ4ことでテキストが生成されます。
ここにGuidanceが絡んでいました。

Grammars are applied as a processor that masks out tokens that are not allowed by the grammar. (3より)

確率分布に対してGrammarを適用して、許容されないトークンは隠してしまいます(確率がマイナス無限大)。
修正された確率分布から次のトークンが選ばれるので、与えたgrammarに沿った出力となる、と理解しました。

さらに次のトークンを選ぶ際はgrammarの状態を更新し、確率分布を修正してから次のトークンを選びます。
grammarに沿わせるための確率分布の修正は、1語進むごとに更新されるわけですね。

Guidanceの使い方のtipsもありました:

If you are using the /generate with a grammar it is recommended to include the grammar in the prompt prefixed by something like Please use the following JSON schema to generate the output:.

grammar(JSON)と「以下のJSONスキーマに沿って出力してください」のような指示をプロンプトに含める

If you are getting a response with many repeated tokens, please use the frequency_penalty or repetition_penalty

レスポンスに多くの繰り返しトークンがある場合は、frequency_penaltyまたはrepetition_penaltyを指定する

上で取り上げた例でも指定しています!

終わりに

Hugging Face TGI (Text Generation Inference) で知ったGuidanceについて書きました。
LLMが出力するトークンの確率分布に、grammarを適用して分布を編集。
これによりユーザが望む形式の出力を実現するのですね。

TGIが依存しているOutlines、気になるライブラリです