?️ LLM があらゆるアプリケーションの言語を話せるようにします。 ⁉️
.txt のチームによって ❤?️ で作成されました。
Youtubeチャンネル | .txt ブログ |ツイッター
pip install outlines
ここは初めてですか?セットアップガイドに移動します
outlinesdev/outlines
で提供します。アウトラインには毎週新しいリリースと機能が登場します。必ずスターを付けてください。このリポジトリを見て、@dottxtai をフォローして最新情報を入手してください。
私たちは構造化された発電の限界を押し広げ続けるために会社を設立しました。 .txt について詳しく学び、ホストされたソリューションが必要な場合は .json API を試してみてください。
大規模な言語モデルを含むシステムの信頼性への最初のステップは、出力とユーザー定義コードの間に明確に定義されたインターフェイスがあることを確認することです。アウトラインは、言語モデルの生成を制御して出力をより予測しやすくする方法を提供します。
完了を複数の選択肢の中から選択するように減らすことができます。
import outlines
model = outlines . models . transformers ( "microsoft/Phi-3-mini-4k-instruct" )
prompt = """You are a sentiment-labelling assistant.
Is the following review positive or negative?
Review: This restaurant is just awesome!
"""
generator = outlines . generate . choice ( model , [ "Positive" , "Negative" ])
answer = generator ( prompt )
整数または浮動小数点のみを返すようにモデルに指示できます。
import outlines
model = outlines . models . transformers ( "WizardLM/WizardMath-7B-V1.1" )
prompt = "<s>result of 9 + 9 = 18</s><s>result of 1 + 2 = "
answer = outlines . generate . format ( model , int )( prompt )
print ( answer )
# 3
prompt = "sqrt(2)="
generator = outlines . generate . format ( model , float )
answer = generator ( prompt , max_tokens = 10 )
print ( answer )
# 1.41421356
アウトラインには、正規表現構造の高速生成も含まれています。実際、 choice
とformat
関数は、特に内部で正規表現構造の生成を使用します。
import outlines
model = outlines . models . transformers ( "microsoft/Phi-3-mini-4k-instruct" )
prompt = "What is the IP address of the Google DNS servers? "
generator = outlines . generate . text ( model )
unstructured = generator ( prompt , max_tokens = 30 )
generator = outlines . generate . regex (
model ,
r"((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)" ,
)
structured = generator ( prompt , max_tokens = 30 )
print ( unstructured )
# What is the IP address of the Google DNS servers?
#
# Passive DNS servers are at DNS servers that are private.
# In other words, both IP servers are private. The database
# does not contain Chelsea Manning
print ( structured )
# What is the IP address of the Google DNS servers?
# 2.2.6.1
他のライブラリとは異なり、アウトラインでの正規表現構造化生成は、非構造化生成とほぼ同じくらい高速です。
アウトラインを使用すると、出力が JSON スキーマまたは Pydantic モデルに従うように、生成プロセスをガイドできます。
from enum import Enum
from pydantic import BaseModel , constr
import outlines
import torch
class Weapon ( str , Enum ):
sword = "sword"
axe = "axe"
mace = "mace"
spear = "spear"
bow = "bow"
crossbow = "crossbow"
class Armor ( str , Enum ):
leather = "leather"
chainmail = "chainmail"
plate = "plate"
class Character ( BaseModel ):
name : constr ( max_length = 10 )
age : int
armor : Armor
weapon : Weapon
strength : int
model = outlines . models . transformers ( "microsoft/Phi-3-mini-4k-instruct" )
# Construct structured sequence generator
generator = outlines . generate . json ( model , Character )
# Draw a sample
seed = 789001
character = generator ( "Give me a character description" , seed = seed )
print ( repr ( character ))
# Character(name='Anderson', age=28, armor=<Armor.chainmail: 'chainmail'>, weapon=<Weapon.sword: 'sword'>, strength=8)
character = generator ( "Give me an interesting character description" )
print ( repr ( character ))
# Character(name='Vivian Thr', age=44, armor=<Armor.plate: 'plate'>, weapon=<Weapon.crossbow: 'crossbow'>, strength=125)
このメソッドは、ユニオン型、オプション型、配列、ネストされたスキーマなどで動作します。一部のフィールド制約はまだサポートされていませんが、その他はすべて動作するはずです。
Pydantic モデルの代わりに JSON スキーマを渡せるようにしたい場合があります。ご対応いたします:
import outlines
schema = '''{
"title": "Character",
"type": "object",
"properties": {
"name": {
"title": "Name",
"maxLength": 10,
"type": "string"
},
"age": {
"title": "Age",
"type": "integer"
},
"armor": {"$ref": "#/definitions/Armor"},
"weapon": {"$ref": "#/definitions/Weapon"},
"strength": {
"title": "Strength",
"type": "integer"
}
},
"required": ["name", "age", "armor", "weapon", "strength"],
"definitions": {
"Armor": {
"title": "Armor",
"description": "An enumeration.",
"enum": ["leather", "chainmail", "plate"],
"type": "string"
},
"Weapon": {
"title": "Weapon",
"description": "An enumeration.",
"enum": ["sword", "axe", "mace", "spear", "bow", "crossbow"],
"type": "string"
}
}
}'''
model = outlines . models . transformers ( "microsoft/Phi-3-mini-4k-instruct" )
generator = outlines . generate . json ( model , schema )
character = generator ( "Give me a character description" )
正式な文法が世界を支配しており、アウトラインによって LLM も支配されます。 EBNF 形式で任意の文脈自由文法を渡すことができ、アウトラインはこの文法に対して有効な出力を生成します。
import outlines
arithmetic_grammar = """
?start: expression
?expression: term (("+" | "-") term)*
?term: factor (("*" | "/") factor)*
?factor: NUMBER
| "-" factor
| "(" expression ")"
%import common.NUMBER
"""
model = outlines . models . transformers ( "WizardLM/WizardMath-7B-V1.1" )
generator = outlines . generate . cfg ( model , arithmetic_grammar )
sequence = generator ( "Alice had 4 apples and Bob ate 2. Write an expression for Alice's apples:" )
print ( sequence )
# (8-2)
これは非常に単純な文法であり、 outlines.generate.cfg
使用すると、構文的に有効な Python、SQL などを生成できます。本当に、あらゆる種類の構造化テキストです。 Web で「X EBNF grammar」を検索し、Outlines grammars
モジュールを確認するだけです。
アウトラインは、関数のシグネチャから出力の構造を推測できます。結果は辞書であり、通常の辞書展開構文**
を使用して関数に直接渡すことができます。
import outlines
def add ( a : int , b : int ):
return a + b
model = outlines . models . transformers ( "WizardLM/WizardMath-7B-V1.1" )
generator = outlines . generate . json ( model , add )
result = generator ( "Return json with two integers named a and b respectively. a is odd and b even." )
print ( add ( ** result ))
# 3
関数を直接渡して構造を指定することの大きな利点は、LLM の構造が関数の定義に応じて変化することです。複数の場所でコードを変更する必要はありません。
さまざまな関数を列挙型に埋め込んでパラメータを生成することもできます。
from enum import Enum
from functools import partial
import outlines
def add ( a : int , b : int ) -> int :
return a + b
def mul ( c : float , d : float ) -> float :
return c * d
class Operation ( Enum ):
add = partial ( add )
mul = partial ( mul )
model = outlines . models . transformers ( "WizardLM/WizardMath-7B-V1.1" )
generator = outlines . generate . json ( model , add )
result = generator ( "Return json with two float named c and d respectively. c is negative and d greater than 1.0." )
print ( result )
# {'c': -3.14, 'd': 1.5}
プロンプトの構築は面倒になる可能性があります。アウトラインを使用すると、「テンプレート関数」内にテンプレートをカプセル化することで、プロンプトの作成と管理が容易になります。
これらの関数により、プロンプト ロジックを一般的なプログラム ロジックからきちんと分離することができます。他のモジュールやライブラリからインポートできます。
テンプレート関数には余分な抽象化は必要ありません。Jinja2 テンプレート エンジンを使用して、複雑なプロンプトを簡潔な方法で構築できます。
import outlines
examples = [
( "The food was disgusting" , "Negative" ),
( "We had a fantastic night" , "Positive" ),
( "Recommended" , "Positive" ),
( "The waiter was rude" , "Negative" )
]
@ outlines . prompt
def labelling ( to_label , examples ):
"""You are a sentiment-labelling assistant.
{% for example in examples %}
{{ example[0] }} // {{ example[1] }}
{% endfor %}
{{ to_label }} //
"""
model = outlines . models . transformers ( "microsoft/Phi-3-mini-4k-instruct" )
prompt = labelling ( "Just awesome" , examples )
answer = outlines . generate . text ( model )( prompt , max_tokens = 100 )
@article{willard2023efficient,
title={Efficient Guided Generation for LLMs},
author={Willard, Brandon T and Louf, R{'e}mi},
journal={arXiv preprint arXiv:2307.09702},
year={2023}
}