?️讓法學碩士能夠說出每個申請的語言。 ?
由 .txt 團隊使用 ❤?️ 製作。
YouTube 頻道 | .txt 部落格 |嘰嘰喳喳
pip install outlines
第一次來這裡?前往我們的設定指南
outlinesdev/outlines
!Outlines 每週都會發布新版本和新功能。確保加註星標並 ?觀看此存儲庫,並關注@dottxtai 以了解最新資訊!
我們創辦了一家公司,不斷突破結構化發電的界限。了解有關 .txt 的更多信息,如果您需要託管解決方案,請嘗試我們的 .json API
包含大型語言模型的系統實現可靠性的第一步是確保其輸出和使用者定義的程式碼之間有一個定義良好的介面。 Outlines提供了控制語言模型產生的方法,使其輸出更加可預測。
您可以將完成簡化為在多種可能性之間進行選擇:
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
Outlines 還具有快速的正規表示式結構產生功能。事實上, 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
與其他函式庫不同,Outlines 中的正規表示式結構化產生幾乎與非結構化產生一樣快。
Outlines 可以引導生成過程,因此確保輸出遵循 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)
此方法適用於聯合類型、可選類型、陣列、巢狀模式等。
有時您只是希望能夠傳遞 JSON 模式而不是 Pydantic 模型。我們已經為您提供了保障:
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" )
正式文法統治著世界,而 Outlines 也讓它們統治法學碩士。您可以傳遞 EBNF 格式的任何上下文無關語法,Outlines 將產生對此語法有效的輸出:
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 等等。實際上,任何類型的結構化文字。您所要做的就是在網路上搜尋“X EBNF 語法”,然後查看 Outlines grammars
模組。
Outlines 可以從函數的簽章推斷輸出的結構。結果是一個字典,可以使用常用的字典擴充語法**
直接傳遞給函數:
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}
建立提示可能會變得混亂。 Outlines透過將範本封裝在「範本函數」中,使編寫和管理提示變得更加容易。
這些函數使得提示邏輯與通用程式邏輯巧妙地分離成為可能;它們可以從其他模組和庫導入。
模板函數不需要多餘的抽象,它們使用 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}
}