LLAMPPL เป็นต้นแบบการวิจัยสำหรับการเขียนโปรแกรมแบบจำลองภาษา: การระบุงานการสร้างภาษาโดยการเขียนโปรแกรมความน่าจะเป็นที่รวมการโทรไปยัง LLMS, ตรรกะของโปรแกรมสัญลักษณ์และการปรับสภาพความน่าจะเป็น เพื่อแก้ปัญหางานเหล่านี้ LLAMPPL ใช้อัลกอริทึมการอนุมาน Monte Carlo ตามลำดับพิเศษ เทคนิคนี้พวงมาลัย SMC ได้อธิบายไว้ในบทคัดย่อการประชุมเชิงปฏิบัติการล่าสุดของเรา
ที่เก็บนี้ใช้ LLAPPPL สำหรับใช้กับ Transformers HuggingFace
หากคุณต้องการลองใช้ LLAMPPL ลองดูสมุดบันทึกตัวอย่างของเราบน Colab ซึ่งทำหน้าที่สร้างแบบ จำกัด อย่างง่ายโดยใช้ GPT-2 (รุ่นที่ใหญ่กว่าอาจต้องใช้ทรัพยากร RAM หรือ GPU มากกว่ารุ่นฟรีของ Colab)
บันทึก
เราใช้บทกวีเพื่อจัดการการพึ่งพา หากคุณไม่ได้ติดตั้งบทกวีคุณสามารถติดตั้งด้วย pip install poetry
ในการเริ่มต้นในเครื่องของคุณเองให้โคลนที่เก็บนี้และเรียกใช้ poetry install เพื่อติดตั้ง hfppl และการพึ่งพา
git clone https://github.com/probcomp/hfppl
cd hfppl
poetry install
จากนั้นลองใช้ตัวอย่าง โปรดทราบว่าสิ่งนี้จะทำให้น้ำหนักสำหรับการดาวน์โหลด Vicuna-7b-v1.5
poetry run python examples/hard_constraints.py
หากทุกอย่างทำงานคุณควรเห็นแบบจำลองสร้างข่าวการเมืองโดยใช้คำที่มีความยาวมากที่สุดห้าตัวอักษร (เช่น "ดร. จิลล์ไบเดนอาจยังอยู่ห่างจากทำเนียบขาวหนึ่งปี
โปรแกรม LLAMPPL เป็นคลาสย่อยของคลาส hfppl.Model
from hfppl import Model , LMContext , CachedCausalLM
# A LLaMPPL model subclasses the Model class
class MyModel ( Model ):
# The __init__ method is used to process arguments
# and initialize instance variables.
def __init__ ( self , lm , prompt , forbidden_letter ):
super (). __init__ ()
# A stateful context object for the LLM, initialized with the prompt
self . context = LMContext ( lm , prompt )
self . eos_token = lm . tokenizer . eos_token_id
# The forbidden letter
self . forbidden_tokens = set ( i for ( i , v ) in enumerate ( lm . vocab )
if forbidden_letter in v )
# The step method is used to perform a single 'step' of generation.
# This might be a single token, a single phrase, or any other division.
# Here, we generate one token at a time.
async def step ( self ):
# Condition on the next token *not* being a forbidden token.
await self . observe ( self . context . mask_dist ( self . forbidden_tokens ), False )
# Sample the next token from the LLM -- automatically extends `self.context`.
token = await self . sample ( self . context . next_token ())
# Check for EOS or end of sentence
if token . token_id == self . eos_token or str ( token ) in [ '.' , '!' , '?' ]:
# Finish generation
self . finish ()
# To improve performance, a hint that `self.forbidden_tokens` is immutable
def immutable_properties ( self ):
return set ([ 'forbidden_tokens' ])คลาสโมเดลมีวิธีการที่เป็นประโยชน์จำนวนหนึ่งสำหรับการระบุโปรแกรม LLAMPPL:
self.sample(dist[, proposal]) ตัวอย่างจากการแจกแจงที่กำหนด การให้ข้อเสนอไม่ได้แก้ไขคำอธิบายงาน แต่สามารถปรับปรุงการอนุมานได้ ตัวอย่างเช่นที่นี่เราใช้ข้อเสนอที่หลีกเลี่ยงจดหมายต้องห้ามล่วงหน้าself.condition(cond) ในการแสดงออกของบูลีนที่กำหนดself.finish() บ่งชี้ว่ารุ่นเสร็จสมบูรณ์self.observe(dist, obs) ดำเนินการ 'การปรับสภาพอ่อน' ในการกระจายที่กำหนด มันเทียบเท่ากับ (แต่มีประสิทธิภาพมากกว่า) การสุ่มตัวอย่างค่า v จาก dist และจากนั้นเรียกใช้ condition(v == obs) ในการเรียกใช้การอนุมานเราใช้วิธี smc_steer หรือ smc_standard :
import asyncio
from hfppl import smc_steer
# Initialize the HuggingFace model
lm = CachedCausalLM . from_pretrained ( "meta-llama/Llama-2-7b-hf" , auth_token = < YOUR_HUGGINGFACE_API_TOKEN_HERE > )
# Create a model instance
model = MyModel ( lm , "The weather today is expected to be" , "e" )
# Run inference
particles = asyncio . run ( smc_steer ( model , 5 , 3 )) # number of particles N, and beam factor Kตัวอย่างเอาท์พุท:
sunny.
sunny and cool.
34° (81°F) in Chicago with winds at 5mph.
34° (81°F) in Chicago with winds at 2-9 mph.
hot and humid with a possibility of rain, which is not uncommon for this part of Mississippi.
เอกสารเพิ่มเติมสามารถดูได้ที่ https://probcomp.github.io/hfppl