LoginSignup
0
0

Kor: LLMベースのデータ抽出

Last updated at Posted at 2023-09-10

初めに

LLMは伝統的にテキストデータを扱うために設計されています。これは、テキストから構造化データを抽出しようとする際に不利になります。彼らはしばしば根底にある文脈を捉えたり、パターンを識別したり、スキーマによって表されるエンティティ間の関係を推測するのに苦労します。

しかし、これらの課題にもかかわらず、既存のLLMは、適切なツールがあれば、テキスト入力と構造化データとの間のギャップを埋めるのに十分なパワーを持っています。LangChainフレームワークの上に書かれたシンプルなライブラリであるKorは、LLM支援型のデータ抽出の手間を軽減できるツールの一つです。

Korとは

Korは、Pythonライブラリであり、LLMを使用してデータから構造化データを抽出するための組み込みサポートを提供します。データ抽出を支援するために、LLMの上に以下の機能を追加します。

  • 型定義、フィールドの説明、および抽出例を使用してデータスキーマを定義するサポート。
  • 組み込みの抽出プロンプトを提供します。また、カスタムプロンプトを追加することもサポートしています。
  • データを変換するためのCSVまたはJSONデータエンコーダを使用します。
  • スキーマに従って抽出されたデータを検証します。
  • 抽出エラーを処理します。

Korの例

Installation

pip install kor

Kor style schema

from langchain.chat_models import ChatOpenAI
from kor import create_extraction_chain, Object, Text

llm = ChatOpenAI(
    model_name="gpt-3.5-turbo",
    temperature=0,
    max_tokens=2000,
    frequency_penalty=0,
    presence_penalty=0,
    top_p=1.0,
)

schema = Object(
    id="player",
    description=(
        "User is controlling a music player to select songs, pause or start them or play"
        " music by a particular artist."
    ),
    attributes=[
        Text(
            id="song",
            description="User wants to play this song",
            examples=[],
            many=True,
        ),
        Text(
            id="album",
            description="User wants to play this album",
            examples=[],
            many=True,
        ),
        Text(
            id="artist",
            description="Music by the given artist",
            examples=[("Songs by paul simon", "paul simon")],
            many=True,
        ),
        Text(
            id="action",
            description="Action to take one of: `play`, `stop`, `next`, `previous`.",
            examples=[
                ("Please stop the music", "stop"),
                ("play something", "play"),
                ("play a song", "play"),
                ("next song", "next"),
            ],
        ),
    ],
    many=False,
)

chain = create_extraction_chain(llm, schema, encoder_or_encoder_class='json')
chain.run("play songs by paul simon and led zeppelin and the doors")['data']
{'player': {'artist': ['paul simon', 'led zeppelin', 'the doors']}}

詳細:
https://github.com/eyurtsev/kor

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