would be to convert each example into one human message and one AI message response, or a human message followed by a function call message.Below is a simple demonstration. First, import the modules for this example:from langchain.prompts import (    FewShotChatMessagePromptTemplate,    ChatPromptTemplate,)Then, define the examples you'd like to include.examples = [    {"input": "2+2", "output": "4"},    {"input": "2+3", "output": "5"},]Next, assemble them into the few-shot prompt template.# This is a prompt template used to format each individual example.example_prompt = ChatPromptTemplate.from_messages(    [        ("human", "{input}"),        ("ai", "{output}"),    ])few_shot_prompt = FewShotChatMessagePromptTemplate(    example_prompt=example_prompt,    examples=examples,)print(few_shot_prompt.format())    Human: 2+2    AI: 4    Human: 2+3    AI: 5Finally, assemble your final prompt and use it with a model.final_prompt = ChatPromptTemplate.from_messages(    [        ("system", "You are a wondrous wizard of math."),        few_shot_prompt,        ("human", "{input}"),    ])from langchain.chat_models import ChatAnthropicchain = final_prompt | ChatAnthropic(temperature=0.0)chain.invoke({"input": "What's the square of a triangle?"})    AIMessage(content=' Triangles do not have a "square". A square refers to a shape with 4 equal sides and 4 right angles. Triangles have 3 sides and 3 angles.\n\nThe area of a triangle can be calculated using the formula:\n\nA = 1/2 * b * h\n\nWhere:\n\nA is the area \nb is the base (the length of one of the sides)\nh is the height (the length from the base to the opposite vertex)\n\nSo the area depends on the specific dimensions of the triangle. There is no single "square of a triangle". The area can vary greatly depending on the base and height measurements.', additional_kwargs={}, example=False)Dynamic few-shot prompting‚ÄãSometimes you may want to condition which examples are shown based on the input. For this, you can replace the
This notebook covers how to use few-shot examples in chat models. There does not appear to be solid consensus on how best to do few-shot prompting, and the optimal prompt compilation will likely vary by model. Because of this, we provide few-shot prompt templates like the FewShotChatMessagePromptTemplate as a flexible starting point, and you can modify or replace them as you see fit.
This notebook covers how to use few-shot examples in chat models. There does not appear to be solid consensus on how best to do few-shot prompting, and the optimal prompt compilation will likely vary by model. Because of this, we provide few-shot prompt templates like the FewShotChatMessagePromptTemplate as a flexible starting point, and you can modify or replace them as you see fit. ->: would be to convert each example into one human message and one AI message response, or a human message followed by a function call message.Below is a simple demonstration. First, import the modules for this example:from langchain.prompts import (    FewShotChatMessagePromptTemplate,    ChatPromptTemplate,)Then, define the examples you'd like to include.examples = [    {"input": "2+2", "output": "4"},    {"input": "2+3", "output": "5"},]Next, assemble them into the few-shot prompt template.# This is a prompt template used to format each individual example.example_prompt = ChatPromptTemplate.from_messages(    [        ("human", "{input}"),        ("ai", "{output}"),    ])few_shot_prompt = FewShotChatMessagePromptTemplate(    example_prompt=example_prompt,    examples=examples,)print(few_shot_prompt.format())    Human: 2+2    AI: 4    Human: 2+3    AI: 5Finally, assemble your final prompt and use it with a model.final_prompt = ChatPromptTemplate.from_messages(    [        ("system", "You are a wondrous wizard of math."),        few_shot_prompt,        ("human", "{input}"),    ])from langchain.chat_models import ChatAnthropicchain = final_prompt | ChatAnthropic(temperature=0.0)chain.invoke({"input": "What's the square of a triangle?"})    AIMessage(content=' Triangles do not have a "square". A square refers to a shape with 4 equal sides and 4 right angles. Triangles have 3 sides and 3 angles.\n\nThe area of a triangle can be calculated using the formula:\n\nA = 1/2 * b * h\n\nWhere:\n\nA is the area \nb is the base (the length of one of the sides)\nh is the height (the length from the base to the opposite vertex)\n\nSo the area depends on the specific dimensions of the triangle. There is no single "square of a triangle". The area can vary greatly depending on the base and height measurements.', additional_kwargs={}, example=False)Dynamic few-shot prompting‚ÄãSometimes you may want to condition which examples are shown based on the input. For this, you can replace the