█

AI/TLDR

मुझे पिलाएं कॉफ़ी
मुझे एक कॉफ़ी पिलाएँ

LM Format Enforcer

tokens फ़िल्टर करके LLM का आउटपुट JSON Schema या regex के अनुरूप बाध्य करें

Structured Outputओपन सोर्स
भाषा
Python
$pip install lm-format-enforcer
संक्षेप में

LM Format Enforcer क्या है?

LM Format Enforcer tokens फ़िल्टर करके LLM का आउटपुट JSON Schema या regex के अनुरूप बाध्य करें. यह App Orchestration टूल्स की Structured Output श्रेणी में आता है; ओपन सोर्स है और Python में लिखा गया है। शुरू करने के लिए: पैकेज इंस्टॉल करें.

सारांश

LM Format Enforcer एक Python लाइब्रेरी है, जो भाषा मॉडल को किसी तय आउटपुट format पर बने रहने के लिए बाध्य करती है, जैसे JSON Schema या regex। prompt के शब्दों पर भरोसा करके यह उम्मीद करने के बजाय कि मॉडल मान जाएगा, यह हर step पर मॉडल को जो tokens बनाने की अनुमति है उन्हें फ़िल्टर करती है, ताकि नतीजा हमेशा आपके माँगे गए format से मेल खाए।

यह उन डेवलपर्स के लिए बना है जिन्हें लोकल या self-hosted मॉडल से भरोसेमंद structured आउटपुट चाहिए और जो गलत format वाले जवाबों को पार्स करने और बार-बार दोबारा कोशिश करने से तंग आ चुके हैं। यह किसी भी Python भाषा मॉडल और tokenizer के साथ काम करता है, और पहले से transformers, LangChain, LlamaIndex, llama.cpp, vLLM, Haystack, NVIDIA TensorRT-LLM और ExLlamaV2 के साथ integrate होता है।

structured-output टूल के रूप में, यह आपकी मौजूदा generation pipeline को बदलने के बजाय उसमें फ़िट हो जाता है। यह अब भी मॉडल को JSON Schema के भीतर whitespace और फ़ील्ड के क्रम का चुनाव करने देता है, जिससे टेक्स्ट स्वाभाविक रहता है और hallucinations कम होते हैं, जबकि आउटपुट के ढांचे की गारंटी बनी रहती है।

यह क्या करता है

  • JSON Schema, schemaless JSON mode और regular-expression formats को लागू करता है
  • किसी भी Python भाषा मॉडल और tokenizer के साथ काम करता है; transformers, LangChain, LlamaIndex, llama.cpp, vLLM, Haystack, TensorRT-LLM और ExLlamaV2 के साथ integrate होता है
  • Batched generation और beam search सपोर्ट करता है, और tokens को हर input या हर beam के लिए अलग-अलग फ़िल्टर करता है
  • JSON Schema में required और optional fields के साथ-साथ nested objects, arrays और dictionaries को संभालता है
  • मॉडल को whitespace और फ़ील्ड के क्रम को नियंत्रित करने देता है, जिससे hallucinations कम होते हैं
  • high-level generation loop बदले बिना मौजूदा pipelines में जुड़ जाता है

शुरू करें

पैकेज इंस्टॉल करें, फिर अपने schema से एक parser बनाएं और उसे token filter के रूप में अपनी generation pipeline में पास करें। नीचे दिया उदाहरण README से लिया गया है और सीधे Hugging Face transformers का इस्तेमाल करता है।

पैकेज इंस्टॉल करें

PyPI से इंस्टॉल करें। transformers वाले उदाहरण के लिए आपको transformers, torch और मॉडल की runtime dependencies भी चाहिए होंगी।

bashbash
pip install lm-format-enforcer

अपना आउटपुट schema तय करें

एक Pydantic model की मदद से वह आकार (shape) बताएं जो आप चाहते हैं।

pythonpython
from pydantic import BaseModel

class AnswerFormat(BaseModel):
    first_name: str
    last_name: str
    year_of_birth: int
    num_seasons_in_nba: int

Token filter बनाएं और generation चलाएं

JsonSchemaParser बनाएं, उससे और अपने tokenizer से एक prefix function तैयार करें, फिर वह function transformers pipeline को दें ताकि आउटपुट schema से मेल खाए।

pythonpython
from lmformatenforcer import JsonSchemaParser
from lmformatenforcer.integrations.transformers import build_transformers_prefix_allowed_tokens_fn
from transformers import pipeline

hf_pipeline = pipeline('text-generation', model='TheBloke/Llama-2-7b-Chat-GPTQ', device_map='auto')
prompt = f'Here is information about Michael Jordan in the following json schema: {AnswerFormat.schema_json()} :\n'

parser = JsonSchemaParser(AnswerFormat.schema())
prefix_function = build_transformers_prefix_allowed_tokens_fn(hf_pipeline.tokenizer, parser)

output_dict = hf_pipeline(prompt, prefix_allowed_tokens_fn=prefix_function)
result = output_dict[0]['generated_text'][len(prompt):]
print(result)

कमांड और कोड प्रोजेक्ट के अपने दस्तावेज़ों से लिए गए हैं — हमेशा जांचें आधिकारिक repo ताज़ा जानकारी के लिए।

कब इस्तेमाल करें

  • बिना retry loops के downstream parsing के लिए local मॉडल से भरोसेमंद JSON records निकालना
  • lm-format-enforcer guided-decoding backend का इस्तेमाल करके vLLM OpenAI-compatible server से structured आउटपुट देना
  • मॉडल के आउटपुट को regex तक सीमित करना, जैसे date, phone number या enum value
  • transformers, llama.cpp या LlamaIndex pipelines में optional fields वाले nested schemas के लिए मान्य जवाब तैयार करना

How LM Format Enforcer compares

LM Format Enforcer alongside other open-source structured output tools AI/TLDR tracks, ranked by GitHub stars.

टूलस्टारयह क्या करता है
Laya★ 36.1kटेक्स्ट के बारे में टाइप किए गए सवाल पूछें और कैलिब्रेटेड संभावनाएँ पाएँ, जनरेट किया हुआ JSON नहीं
Guidance★ 21.8kConstrained, structured generation के साथ LLM को नियंत्रित करने वाला एक programming model
Outlines★ 16kकिसी भी LLM को हमेशा valid structured output देने के लिए बाध्य करें
Instructor★ 14.2kकिसी भी LLM से भरोसेमंद structured आउटपुट, Pydantic से validated
BAML★ 9.6kएक prompting language जो LLM prompts को typed functions में बदलती है और भरोसेमंद structured output देती है
Marvin★ 6.2kLLM calls को typed Python functions और agentic workflows में बदलें
SemIf★ 5kकिसी ओपन मॉडल से JSON जनरेट कराने के बजाय, टाइप किए गए विकल्पों की संभावनाएँ सीधे पढ़ें
LM Format Enforcer★ 2ktokens फ़िल्टर करके LLM का आउटपुट JSON Schema या regex के अनुरूप बाध्य करें