Few-shot prompting

LangChain के साथ LLM एप्लिकेशन विकसित करना

Jonathan Bennion

AI Engineer & LangChain Contributor

स्टैंडर्ड प्रॉम्प्ट टेम्पलेट्स की सीमाएँ

 

  • PromptTemplate + ChatPromptTemplate
  • ✅ कम उदाहरणों को संभालना
  • ❌ कई उदाहरणों पर स्केल नहीं होता
  • FewShotPromptTemplate

 

examples = [
    {
        "question": "..."
        "answer": "..."
    },
    ...
]
LangChain के साथ LLM एप्लिकेशन विकसित करना

उदाहरण सेट बनाना

examples = [
    {
        "question": "Does Henry Campbell have any pets?",
        "answer": "Henry Campbell has a dog called Pluto."
    },
    ...
]
# Convert pandas DataFrame to list of dicts
examples = df.to_dict(orient="records")
LangChain के साथ LLM एप्लिकेशन विकसित करना

उदाहरणों को फ़ॉर्मैट करना

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate.from_template("Question: {question}\n{answer}")
prompt = example_prompt.invoke({"question": "What is the capital of Italy?"
                                "answer": "Rome"})
print(prompt.text)
Question: What is the capital of Italy?
Rome
LangChain के साथ LLM एप्लिकेशन विकसित करना

FewShotPromptTemplate

 

prompt_template = FewShotPromptTemplate(

examples=examples,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"]
)

 

 

  • examples: डिक्ट्स की लिस्ट
  • example_prompt: फ़ॉर्मैट किया टेम्पलेट
  • suffix: इनपुट में जोड़ने वाला_SUFFIX
  • input_variables
LangChain के साथ LLM एप्लिकेशन विकसित करना

Few-shot प्रॉम्प्ट टेम्पलेट चलाना

prompt = prompt_template.invoke({"input": "What is the name of Henry Campbell's dog?"})

print(prompt.text)
Question: Does Henry Campbell have any pets?
Henry Campbell has a dog called Pluto.
...

Question: What is the name of Henry Campbell's dog?
LangChain के साथ LLM एप्लिकेशन विकसित करना

चेन के साथ इंटिग्रेशन

llm = ChatOpenAI(model="gpt-4o-mini", api_key="...")

llm_chain = prompt_template | llm response = llm_chain.invoke({"input": "What is the name of Henry Campbell's dog?"})
print(response.content)
The name of Henry Campbell's dog is Pluto.
LangChain के साथ LLM एप्लिकेशन विकसित करना

अभ्यास करते हैं!

LangChain के साथ LLM एप्लिकेशन विकसित करना

Preparing Video For Download...