Guidance क्या है?
Guidance Constrained, structured generation के साथ LLM को नियंत्रित करने वाला एक programming model. यह App Orchestration टूल्स की Structured Output श्रेणी में आता है; ओपन सोर्स है और Python में लिखा गया है। शुरू करने के लिए: इंस्टॉल करें.
सारांश
Guidance Microsoft की एक Python लाइब्रेरी है, जो language models को नियंत्रित करने में मदद करती है। एक बड़ा prompt लिखकर उम्मीद करने के बजाय कि model सही format लौटाएगा, आप code में prompt और generation दोनों को साथ बनाते हैं, जिसमें fixed text को gen() और select() calls के साथ मिलाया जाता है, जो model output तैयार करते हैं।
यह उन developers के लिए बनाई गई है जिन्हें output किसी खास ढांचे में चाहिए, जैसे कोई संख्या, सूची में से कोई विकल्प, या ऐसी string जो regular expression या context-free grammar से मेल खाती हो। चूँकि constraints generation के दौरान लागू होती हैं, आपको extra retries या post-processing के बिना valid output मिलता है।
Structured-output और orchestration tool के रूप में Guidance कई backends पर काम करती है, जिनमें Transformers, llama.cpp और OpenAI शामिल हैं। आप conditionals, loops और tool use जैसे control logic को सीधे generation के साथ भी जोड़ सकते हैं।
यह क्या करता है
- Pythonic interface, जहाँ model objects immutable होते हैं और आप += operator से prompt और generation जोड़ते हैं
- regular expressions और context-free grammars से constrained generation, ताकि output का syntax पक्का रहे
- select(), जिससे model को ज्ञात विकल्पों की सूची में से चुनने के लिए मजबूर किया जा सके
- system(), user() और assistant() context managers के जरिए roles
- Reusable @guidance functions, जो built-in gen() और select() की तरह जुड़ते हैं
- grammar.match() और Mock model के साथ offline grammar debugging, बिना किसी API call के
शुरू करें
PyPI से Guidance इंस्टॉल करें, फिर chat-style prompt बनाएं और constrained output कैप्चर करें। जिस model को चलाना चाहते हैं, उसके लिए Transformers जैसा backend भी चाहिए।
इंस्टॉल करें
Guidance PyPI पर उपलब्ध है और Transformers, llama.cpp और OpenAI जैसे backends को सपोर्ट करता है। इसे pip से इंस्टॉल करें।
pip install guidanceModel से text generate करें
किसी backend के जरिए model लोड करें, role context managers से prompt बनाएं, फिर gen() call को नाम देकर generated text कैप्चर करें।
from guidance import system, user, assistant, gen
from guidance.models import Transformers
phi_lm = Transformers("microsoft/Phi-4-mini-instruct")
lm = phi_lm
with system():
lm += "You are a helpful assistant"
with user():
lm += "Hello. What is your name?"
with assistant():
lm += gen(name="lm_response", max_tokens=20)
print(lm['lm_response'])Output को constrain करें
Numeric जवाब तय करने के लिए regex इस्तेमाल करें, या model को तय विकल्पों तक सीमित करने के लिए select()।
from guidance import select
lm = phi_lm
with user():
lm += "What is the capital of Sweden? Answer A, B, C, or D."
with assistant():
lm += select(["A", "B", "C", "D"], name="model_selection")
print(lm['model_selection'])Grammar को offline debug करें
Candidate strings को grammar से validate करें और Mock model के साथ चलाएं, बिना किसी model API call के।
from guidance import gen
from guidance.models import Mock
grammar = "expr=" + gen(regex=r"\d+([+*]\d+)*", name="expr")
assert grammar.match("expr=12+7*3") is not None
assert grammar.match("expr=12+*3") is None
lm = Mock(b"<s>expr=12+7*3")
lm += grammar
print(lm["expr"]) # 12+7*3कमांड और कोड प्रोजेक्ट के अपने दस्तावेज़ों से लिए गए हैं — हमेशा जांचें आधिकारिक repo ताज़ा जानकारी के लिए।
कब इस्तेमाल करें
- Model output को strict format में मजबूर करना, जैसे JSON, एक single number, या regex से मेल खाने वाली value
- Multiple-choice या classification flows बनाना, जहाँ जवाब किसी ज्ञात सूची से ही आना चाहिए
- Loops और conditionals जैसे control flow को generation के साथ जोड़ना, उदाहरण के लिए एक ही pass में कई सवालों के जवाब देना
- Real model calls पर tokens खर्च करने से पहले constraint grammars को offline iterate और test करना
How Guidance compares
Guidance alongside other open-source structured output tools AI/TLDR tracks, ranked by GitHub stars.
| टूल | स्टार | यह क्या करता है |
|---|---|---|
| Laya | ★ 36.1k | टेक्स्ट के बारे में टाइप किए गए सवाल पूछें और कैलिब्रेटेड संभावनाएँ पाएँ, जनरेट किया हुआ JSON नहीं |
| Guidance | ★ 21.8k | Constrained, 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.2k | LLM calls को typed Python functions और agentic workflows में बदलें |
| SemIf | ★ 5k | किसी ओपन मॉडल से JSON जनरेट कराने के बजाय, टाइप किए गए विकल्पों की संभावनाएँ सीधे पढ़ें |
| LM Format Enforcer | ★ 2k | tokens फ़िल्टर करके LLM का आउटपुट JSON Schema या regex के अनुरूप बाध्य करें |

