forked from phoenix/litellm-mirror
add everyting for docs
This commit is contained in:
parent
de45a738ee
commit
0fe8799f94
1015 changed files with 185353 additions and 0 deletions
|
@ -0,0 +1,10 @@
|
|||
```python
|
||||
class BaseExampleSelector(ABC):
|
||||
"""Interface for selecting examples to include in prompts."""
|
||||
|
||||
@abstractmethod
|
||||
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
|
||||
"""Select which examples to use based on the inputs."""
|
||||
```
|
||||
|
||||
The only method it needs to expose is a ``select_examples`` method. This takes in the input variables and then returns a list of examples. It is up to each specific implementation as to how those examples are selected. Let's take a look at some below.
|
|
@ -0,0 +1,130 @@
|
|||
```python
|
||||
from langchain.prompts import PromptTemplate
|
||||
from langchain.prompts import FewShotPromptTemplate
|
||||
from langchain.prompts.example_selector import LengthBasedExampleSelector
|
||||
|
||||
|
||||
# These are a lot of examples of a pretend task of creating antonyms.
|
||||
examples = [
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
{"input": "energetic", "output": "lethargic"},
|
||||
{"input": "sunny", "output": "gloomy"},
|
||||
{"input": "windy", "output": "calm"},
|
||||
|
||||
example_prompt = PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
)
|
||||
example_selector = LengthBasedExampleSelector(
|
||||
# These are the examples it has available to choose from.
|
||||
examples=examples,
|
||||
# This is the PromptTemplate being used to format the examples.
|
||||
example_prompt=example_prompt,
|
||||
# This is the maximum length that the formatted examples should be.
|
||||
# Length is measured by the get_text_length function below.
|
||||
max_length=25,
|
||||
# This is the function used to get the length of a string, which is used
|
||||
# to determine which examples to include. It is commented out because
|
||||
# it is provided as a default value if none is specified.
|
||||
# get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x))
|
||||
)
|
||||
dynamic_prompt = FewShotPromptTemplate(
|
||||
# We provide an ExampleSelector instead of examples.
|
||||
example_selector=example_selector,
|
||||
example_prompt=example_prompt,
|
||||
prefix="Give the antonym of every input",
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
input_variables=["adjective"],
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
# An example with small input, so it selects all examples.
|
||||
print(dynamic_prompt.format(adjective="big"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Give the antonym of every input
|
||||
|
||||
Input: happy
|
||||
Output: sad
|
||||
|
||||
Input: tall
|
||||
Output: short
|
||||
|
||||
Input: energetic
|
||||
Output: lethargic
|
||||
|
||||
Input: sunny
|
||||
Output: gloomy
|
||||
|
||||
Input: windy
|
||||
Output: calm
|
||||
|
||||
Input: big
|
||||
Output:
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# An example with long input, so it selects only one example.
|
||||
long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else"
|
||||
print(dynamic_prompt.format(adjective=long_string))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Give the antonym of every input
|
||||
|
||||
Input: happy
|
||||
Output: sad
|
||||
|
||||
Input: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else
|
||||
Output:
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# You can add an example to an example selector as well.
|
||||
new_example = {"input": "big", "output": "small"}
|
||||
dynamic_prompt.example_selector.add_example(new_example)
|
||||
print(dynamic_prompt.format(adjective="enthusiastic"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Give the antonym of every input
|
||||
|
||||
Input: happy
|
||||
Output: sad
|
||||
|
||||
Input: tall
|
||||
Output: short
|
||||
|
||||
Input: energetic
|
||||
Output: lethargic
|
||||
|
||||
Input: sunny
|
||||
Output: gloomy
|
||||
|
||||
Input: windy
|
||||
Output: calm
|
||||
|
||||
Input: big
|
||||
Output: small
|
||||
|
||||
Input: enthusiastic
|
||||
Output:
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
|
@ -0,0 +1,112 @@
|
|||
```python
|
||||
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
|
||||
from langchain.vectorstores import Chroma
|
||||
from langchain.embeddings import OpenAIEmbeddings
|
||||
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
|
||||
|
||||
example_prompt = PromptTemplate(
|
||||
input_variables=["input", "output"],
|
||||
template="Input: {input}\nOutput: {output}",
|
||||
)
|
||||
|
||||
# These are a lot of examples of a pretend task of creating antonyms.
|
||||
examples = [
|
||||
{"input": "happy", "output": "sad"},
|
||||
{"input": "tall", "output": "short"},
|
||||
{"input": "energetic", "output": "lethargic"},
|
||||
{"input": "sunny", "output": "gloomy"},
|
||||
{"input": "windy", "output": "calm"},
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
example_selector = SemanticSimilarityExampleSelector.from_examples(
|
||||
# This is the list of examples available to select from.
|
||||
examples,
|
||||
# This is the embedding class used to produce embeddings which are used to measure semantic similarity.
|
||||
OpenAIEmbeddings(),
|
||||
# This is the VectorStore class that is used to store the embeddings and do a similarity search over.
|
||||
Chroma,
|
||||
# This is the number of examples to produce.
|
||||
k=1
|
||||
)
|
||||
similar_prompt = FewShotPromptTemplate(
|
||||
# We provide an ExampleSelector instead of examples.
|
||||
example_selector=example_selector,
|
||||
example_prompt=example_prompt,
|
||||
prefix="Give the antonym of every input",
|
||||
suffix="Input: {adjective}\nOutput:",
|
||||
input_variables=["adjective"],
|
||||
)
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Running Chroma using direct local API.
|
||||
Using DuckDB in-memory for database. Data will be transient.
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# Input is a feeling, so should select the happy/sad example
|
||||
print(similar_prompt.format(adjective="worried"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Give the antonym of every input
|
||||
|
||||
Input: happy
|
||||
Output: sad
|
||||
|
||||
Input: worried
|
||||
Output:
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# Input is a measurement, so should select the tall/short example
|
||||
print(similar_prompt.format(adjective="fat"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Give the antonym of every input
|
||||
|
||||
Input: happy
|
||||
Output: sad
|
||||
|
||||
Input: fat
|
||||
Output:
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
# You can add new examples to the SemanticSimilarityExampleSelector as well
|
||||
similar_prompt.example_selector.add_example({"input": "enthusiastic", "output": "apathetic"})
|
||||
print(similar_prompt.format(adjective="joyful"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Give the antonym of every input
|
||||
|
||||
Input: happy
|
||||
Output: sad
|
||||
|
||||
Input: joyful
|
||||
Output:
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
|
@ -0,0 +1,257 @@
|
|||
### Use Case
|
||||
|
||||
In this tutorial, we'll configure few shot examples for self-ask with search.
|
||||
|
||||
|
||||
## Using an example set
|
||||
|
||||
### Create the example set
|
||||
|
||||
To get started, create a list of few shot examples. Each example should be a dictionary with the keys being the input variables and the values being the values for those input variables.
|
||||
|
||||
```python
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
examples = [
|
||||
{
|
||||
"question": "Who lived longer, Muhammad Ali or Alan Turing?",
|
||||
"answer":
|
||||
"""
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: How old was Muhammad Ali when he died?
|
||||
Intermediate answer: Muhammad Ali was 74 years old when he died.
|
||||
Follow up: How old was Alan Turing when he died?
|
||||
Intermediate answer: Alan Turing was 41 years old when he died.
|
||||
So the final answer is: Muhammad Ali
|
||||
"""
|
||||
},
|
||||
{
|
||||
"question": "When was the founder of craigslist born?",
|
||||
"answer":
|
||||
"""
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who was the founder of craigslist?
|
||||
Intermediate answer: Craigslist was founded by Craig Newmark.
|
||||
Follow up: When was Craig Newmark born?
|
||||
Intermediate answer: Craig Newmark was born on December 6, 1952.
|
||||
So the final answer is: December 6, 1952
|
||||
"""
|
||||
},
|
||||
{
|
||||
"question": "Who was the maternal grandfather of George Washington?",
|
||||
"answer":
|
||||
"""
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who was the mother of George Washington?
|
||||
Intermediate answer: The mother of George Washington was Mary Ball Washington.
|
||||
Follow up: Who was the father of Mary Ball Washington?
|
||||
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
|
||||
So the final answer is: Joseph Ball
|
||||
"""
|
||||
},
|
||||
{
|
||||
"question": "Are both the directors of Jaws and Casino Royale from the same country?",
|
||||
"answer":
|
||||
"""
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who is the director of Jaws?
|
||||
Intermediate Answer: The director of Jaws is Steven Spielberg.
|
||||
Follow up: Where is Steven Spielberg from?
|
||||
Intermediate Answer: The United States.
|
||||
Follow up: Who is the director of Casino Royale?
|
||||
Intermediate Answer: The director of Casino Royale is Martin Campbell.
|
||||
Follow up: Where is Martin Campbell from?
|
||||
Intermediate Answer: New Zealand.
|
||||
So the final answer is: No
|
||||
"""
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
### Create a formatter for the few shot examples
|
||||
|
||||
Configure a formatter that will format the few shot examples into a string. This formatter should be a `PromptTemplate` object.
|
||||
|
||||
|
||||
```python
|
||||
example_prompt = PromptTemplate(input_variables=["question", "answer"], template="Question: {question}\n{answer}")
|
||||
|
||||
print(example_prompt.format(**examples[0]))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Question: Who lived longer, Muhammad Ali or Alan Turing?
|
||||
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: How old was Muhammad Ali when he died?
|
||||
Intermediate answer: Muhammad Ali was 74 years old when he died.
|
||||
Follow up: How old was Alan Turing when he died?
|
||||
Intermediate answer: Alan Turing was 41 years old when he died.
|
||||
So the final answer is: Muhammad Ali
|
||||
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
### Feed examples and formatter to `FewShotPromptTemplate`
|
||||
|
||||
Finally, create a `FewShotPromptTemplate` object. This object takes in the few shot examples and the formatter for the few shot examples.
|
||||
|
||||
|
||||
```python
|
||||
prompt = FewShotPromptTemplate(
|
||||
examples=examples,
|
||||
example_prompt=example_prompt,
|
||||
suffix="Question: {input}",
|
||||
input_variables=["input"]
|
||||
)
|
||||
|
||||
print(prompt.format(input="Who was the father of Mary Ball Washington?"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Question: Who lived longer, Muhammad Ali or Alan Turing?
|
||||
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: How old was Muhammad Ali when he died?
|
||||
Intermediate answer: Muhammad Ali was 74 years old when he died.
|
||||
Follow up: How old was Alan Turing when he died?
|
||||
Intermediate answer: Alan Turing was 41 years old when he died.
|
||||
So the final answer is: Muhammad Ali
|
||||
|
||||
|
||||
Question: When was the founder of craigslist born?
|
||||
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who was the founder of craigslist?
|
||||
Intermediate answer: Craigslist was founded by Craig Newmark.
|
||||
Follow up: When was Craig Newmark born?
|
||||
Intermediate answer: Craig Newmark was born on December 6, 1952.
|
||||
So the final answer is: December 6, 1952
|
||||
|
||||
|
||||
Question: Who was the maternal grandfather of George Washington?
|
||||
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who was the mother of George Washington?
|
||||
Intermediate answer: The mother of George Washington was Mary Ball Washington.
|
||||
Follow up: Who was the father of Mary Ball Washington?
|
||||
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
|
||||
So the final answer is: Joseph Ball
|
||||
|
||||
|
||||
Question: Are both the directors of Jaws and Casino Royale from the same country?
|
||||
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who is the director of Jaws?
|
||||
Intermediate Answer: The director of Jaws is Steven Spielberg.
|
||||
Follow up: Where is Steven Spielberg from?
|
||||
Intermediate Answer: The United States.
|
||||
Follow up: Who is the director of Casino Royale?
|
||||
Intermediate Answer: The director of Casino Royale is Martin Campbell.
|
||||
Follow up: Where is Martin Campbell from?
|
||||
Intermediate Answer: New Zealand.
|
||||
So the final answer is: No
|
||||
|
||||
|
||||
Question: Who was the father of Mary Ball Washington?
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Using an example selector
|
||||
|
||||
### Feed examples into `ExampleSelector`
|
||||
|
||||
We will reuse the example set and the formatter from the previous section. However, instead of feeding the examples directly into the `FewShotPromptTemplate` object, we will feed them into an `ExampleSelector` object.
|
||||
|
||||
|
||||
In this tutorial, we will use the `SemanticSimilarityExampleSelector` class. This class selects few shot examples based on their similarity to the input. It uses an embedding model to compute the similarity between the input and the few shot examples, as well as a vector store to perform the nearest neighbor search.
|
||||
|
||||
|
||||
```python
|
||||
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
|
||||
from langchain.vectorstores import Chroma
|
||||
from langchain.embeddings import OpenAIEmbeddings
|
||||
|
||||
|
||||
example_selector = SemanticSimilarityExampleSelector.from_examples(
|
||||
# This is the list of examples available to select from.
|
||||
examples,
|
||||
# This is the embedding class used to produce embeddings which are used to measure semantic similarity.
|
||||
OpenAIEmbeddings(),
|
||||
# This is the VectorStore class that is used to store the embeddings and do a similarity search over.
|
||||
Chroma,
|
||||
# This is the number of examples to produce.
|
||||
k=1
|
||||
)
|
||||
|
||||
# Select the most similar example to the input.
|
||||
question = "Who was the father of Mary Ball Washington?"
|
||||
selected_examples = example_selector.select_examples({"question": question})
|
||||
print(f"Examples most similar to the input: {question}")
|
||||
for example in selected_examples:
|
||||
print("\n")
|
||||
for k, v in example.items():
|
||||
print(f"{k}: {v}")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Running Chroma using direct local API.
|
||||
Using DuckDB in-memory for database. Data will be transient.
|
||||
Examples most similar to the input: Who was the father of Mary Ball Washington?
|
||||
|
||||
|
||||
question: Who was the maternal grandfather of George Washington?
|
||||
answer:
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who was the mother of George Washington?
|
||||
Intermediate answer: The mother of George Washington was Mary Ball Washington.
|
||||
Follow up: Who was the father of Mary Ball Washington?
|
||||
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
|
||||
So the final answer is: Joseph Ball
|
||||
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
### Feed example selector into `FewShotPromptTemplate`
|
||||
|
||||
Finally, create a `FewShotPromptTemplate` object. This object takes in the example selector and the formatter for the few shot examples.
|
||||
|
||||
|
||||
```python
|
||||
prompt = FewShotPromptTemplate(
|
||||
example_selector=example_selector,
|
||||
example_prompt=example_prompt,
|
||||
suffix="Question: {input}",
|
||||
input_variables=["input"]
|
||||
)
|
||||
|
||||
print(prompt.format(input="Who was the father of Mary Ball Washington?"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Question: Who was the maternal grandfather of George Washington?
|
||||
|
||||
Are follow up questions needed here: Yes.
|
||||
Follow up: Who was the mother of George Washington?
|
||||
Intermediate answer: The mother of George Washington was Mary Ball Washington.
|
||||
Follow up: Who was the father of Mary Ball Washington?
|
||||
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
|
||||
So the final answer is: Joseph Ball
|
||||
|
||||
|
||||
Question: Who was the father of Mary Ball Washington?
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
|
@ -0,0 +1,140 @@
|
|||
Here's the simplest example:
|
||||
|
||||
```python
|
||||
from langchain import PromptTemplate
|
||||
|
||||
|
||||
template = """\
|
||||
You are a naming consultant for new companies.
|
||||
What is a good name for a company that makes {product}?
|
||||
"""
|
||||
|
||||
prompt = PromptTemplate.from_template(template)
|
||||
prompt.format(product="colorful socks")
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
You are a naming consultant for new companies.
|
||||
What is a good name for a company that makes colorful socks?
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
## Create a prompt template
|
||||
|
||||
You can create simple hardcoded prompts using the `PromptTemplate` class. Prompt templates can take any number of input variables, and can be formatted to generate a prompt.
|
||||
|
||||
|
||||
```python
|
||||
from langchain import PromptTemplate
|
||||
|
||||
# An example prompt with no input variables
|
||||
no_input_prompt = PromptTemplate(input_variables=[], template="Tell me a joke.")
|
||||
no_input_prompt.format()
|
||||
# -> "Tell me a joke."
|
||||
|
||||
# An example prompt with one input variable
|
||||
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="Tell me a {adjective} joke.")
|
||||
one_input_prompt.format(adjective="funny")
|
||||
# -> "Tell me a funny joke."
|
||||
|
||||
# An example prompt with multiple input variables
|
||||
multiple_input_prompt = PromptTemplate(
|
||||
input_variables=["adjective", "content"],
|
||||
template="Tell me a {adjective} joke about {content}."
|
||||
)
|
||||
multiple_input_prompt.format(adjective="funny", content="chickens")
|
||||
# -> "Tell me a funny joke about chickens."
|
||||
```
|
||||
|
||||
If you do not wish to specify `input_variables` manually, you can also create a `PromptTemplate` using `from_template` class method. `langchain` will automatically infer the `input_variables` based on the `template` passed.
|
||||
|
||||
```python
|
||||
template = "Tell me a {adjective} joke about {content}."
|
||||
|
||||
prompt_template = PromptTemplate.from_template(template)
|
||||
prompt_template.input_variables
|
||||
# -> ['adjective', 'content']
|
||||
prompt_template.format(adjective="funny", content="chickens")
|
||||
# -> Tell me a funny joke about chickens.
|
||||
```
|
||||
|
||||
You can create custom prompt templates that format the prompt in any way you want. For more information, see [Custom Prompt Templates](./custom_prompt_template.html).
|
||||
|
||||
|
||||
<!-- TODO(shreya): Add link to Jinja -->
|
||||
|
||||
## Chat prompt template
|
||||
|
||||
[Chat Models](../models/chat) take a list of chat messages as input - this list commonly referred to as a `prompt`.
|
||||
These chat messages differ from raw string (which you would pass into a [LLM](/docs/modules/model_io/models/llms) model) in that every message is associated with a `role`.
|
||||
|
||||
For example, in OpenAI [Chat Completion API](https://platform.openai.com/docs/guides/chat/introduction), a chat message can be associated with the AI, human or system role. The model is supposed to follow instruction from system chat message more closely.
|
||||
|
||||
LangChain provides several prompt templates to make constructing and working with prompts easily. You are encouraged to use these chat related prompt templates instead of `PromptTemplate` when querying chat models to fully exploit the potential of underlying chat model.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
```python
|
||||
from langchain.prompts import (
|
||||
ChatPromptTemplate,
|
||||
PromptTemplate,
|
||||
SystemMessagePromptTemplate,
|
||||
AIMessagePromptTemplate,
|
||||
HumanMessagePromptTemplate,
|
||||
)
|
||||
from langchain.schema import (
|
||||
AIMessage,
|
||||
HumanMessage,
|
||||
SystemMessage
|
||||
)
|
||||
```
|
||||
|
||||
To create a message template associated with a role, you use `MessagePromptTemplate`.
|
||||
|
||||
For convenience, there is a `from_template` method exposed on the template. If you were to use this template, this is what it would look like:
|
||||
|
||||
|
||||
```python
|
||||
template="You are a helpful assistant that translates {input_language} to {output_language}."
|
||||
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
|
||||
human_template="{text}"
|
||||
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
||||
```
|
||||
|
||||
If you wanted to construct the `MessagePromptTemplate` more directly, you could create a PromptTemplate outside and then pass it in, eg:
|
||||
|
||||
|
||||
```python
|
||||
prompt=PromptTemplate(
|
||||
template="You are a helpful assistant that translates {input_language} to {output_language}.",
|
||||
input_variables=["input_language", "output_language"],
|
||||
)
|
||||
system_message_prompt_2 = SystemMessagePromptTemplate(prompt=prompt)
|
||||
|
||||
assert system_message_prompt == system_message_prompt_2
|
||||
```
|
||||
|
||||
After that, you can build a `ChatPromptTemplate` from one or more `MessagePromptTemplates`. You can use `ChatPromptTemplate`'s `format_prompt` -- this returns a `PromptValue`, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an llm or chat model.
|
||||
|
||||
|
||||
```python
|
||||
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
|
||||
|
||||
# get a chat completion from the formatted messages
|
||||
chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}),
|
||||
HumanMessage(content='I love programming.', additional_kwargs={})]
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
|
@ -0,0 +1,92 @@
|
|||
## Partial With Strings
|
||||
|
||||
One common use case for wanting to partial a prompt template is if you get some of the variables before others. For example, suppose you have a prompt template that requires two variables, `foo` and `baz`. If you get the `foo` value early on in the chain, but the `baz` value later, it can be annoying to wait until you have both variables in the same place to pass them to the prompt template. Instead, you can partial the prompt template with the `foo` value, and then pass the partialed prompt template along and just use that. Below is an example of doing this:
|
||||
|
||||
|
||||
|
||||
|
||||
```python
|
||||
from langchain.prompts import PromptTemplate
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["foo", "bar"])
|
||||
partial_prompt = prompt.partial(foo="foo");
|
||||
print(partial_prompt.format(bar="baz"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
foobaz
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
You can also just initialize the prompt with the partialed variables.
|
||||
|
||||
|
||||
```python
|
||||
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["bar"], partial_variables={"foo": "foo"})
|
||||
print(prompt.format(bar="baz"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
foobaz
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
## Partial With Functions
|
||||
|
||||
The other common use is to partial with a function. The use case for this is when you have a variable you know that you always want to fetch in a common way. A prime example of this is with date or time. Imagine you have a prompt which you always want to have the current date. You can't hard code it in the prompt, and passing it along with the other input variables is a bit annoying. In this case, it's very handy to be able to partial the prompt with a function that always returns the current date.
|
||||
|
||||
|
||||
```python
|
||||
from datetime import datetime
|
||||
|
||||
def _get_datetime():
|
||||
now = datetime.now()
|
||||
return now.strftime("%m/%d/%Y, %H:%M:%S")
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
prompt = PromptTemplate(
|
||||
template="Tell me a {adjective} joke about the day {date}",
|
||||
input_variables=["adjective", "date"]
|
||||
);
|
||||
partial_prompt = prompt.partial(date=_get_datetime)
|
||||
print(partial_prompt.format(adjective="funny"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Tell me a funny joke about the day 02/27/2023, 22:15:16
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
You can also just initialize the prompt with the partialed variables, which often makes more sense in this workflow.
|
||||
|
||||
|
||||
```python
|
||||
prompt = PromptTemplate(
|
||||
template="Tell me a {adjective} joke about the day {date}",
|
||||
input_variables=["adjective"],
|
||||
partial_variables={"date": _get_datetime}
|
||||
);
|
||||
print(prompt.format(adjective="funny"))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
Tell me a funny joke about the day 02/27/2023, 22:15:16
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
|
@ -0,0 +1,88 @@
|
|||
```python
|
||||
from langchain.prompts.pipeline import PipelinePromptTemplate
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
full_template = """{introduction}
|
||||
|
||||
{example}
|
||||
|
||||
{start}"""
|
||||
full_prompt = PromptTemplate.from_template(full_template)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
introduction_template = """You are impersonating {person}."""
|
||||
introduction_prompt = PromptTemplate.from_template(introduction_template)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
example_template = """Here's an example of an interaction:
|
||||
|
||||
Q: {example_q}
|
||||
A: {example_a}"""
|
||||
example_prompt = PromptTemplate.from_template(example_template)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
start_template = """Now, do this for real!
|
||||
|
||||
Q: {input}
|
||||
A:"""
|
||||
start_prompt = PromptTemplate.from_template(start_template)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
input_prompts = [
|
||||
("introduction", introduction_prompt),
|
||||
("example", example_prompt),
|
||||
("start", start_prompt)
|
||||
]
|
||||
pipeline_prompt = PipelinePromptTemplate(final_prompt=full_prompt, pipeline_prompts=input_prompts)
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
pipeline_prompt.input_variables
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
['example_a', 'person', 'example_q', 'input']
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
||||
|
||||
|
||||
```python
|
||||
print(pipeline_prompt.format(
|
||||
person="Elon Musk",
|
||||
example_q="What's your favorite car?",
|
||||
example_a="Tesla",
|
||||
input="What's your favorite social media site?"
|
||||
))
|
||||
```
|
||||
|
||||
<CodeOutputBlock lang="python">
|
||||
|
||||
```
|
||||
You are impersonating Elon Musk.
|
||||
Here's an example of an interaction:
|
||||
|
||||
Q: What's your favorite car?
|
||||
A: Tesla
|
||||
Now, do this for real!
|
||||
|
||||
Q: What's your favorite social media site?
|
||||
A:
|
||||
|
||||
```
|
||||
|
||||
</CodeOutputBlock>
|
Loading…
Add table
Add a link
Reference in a new issue