LLM-toepassingen ontwikkelen met LangChain
Jonathan Bennion
AI Engineer & LangChain Contributor
PromptTemplate + ChatPromptTemplateFewShotPromptTemplate
examples = [
{
"question": "..."
"answer": "..."
},
...
]
examples = [
{
"question": "Does Henry Campbell have any pets?",
"answer": "Henry Campbell has a dog called Pluto."
},
...
]
# Zet pandas DataFrame om naar een lijst met dicts
examples = df.to_dict(orient="records")
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplateexample_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
prompt_template = FewShotPromptTemplate(examples=examples,example_prompt=example_prompt,suffix="Question: {input}",input_variables=["input"])
examples: de lijst met dictsexample_prompt: opgemaakte templatesuffix: achtervoegsel bij de inputinput_variablesprompt = prompt_template.invoke({"input": "What is the name of Henry Campbell's dog?"})print(prompt.text)
Question: Heeft Henry Campbell huisdieren?
Henry Campbell heeft een hond die Pluto heet.
...
Question: What is the name of Henry Campbell's dog?
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)
De hond van Henry Campbell heet Pluto.
LLM-toepassingen ontwikkelen met LangChain