mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
87e502c6bc
Co-authored-by: jacoblee93 <jacoblee93@gmail.com> Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
93 lines
2.8 KiB
Plaintext
93 lines
2.8 KiB
Plaintext
## 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:
|
|
|
|
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! Instead, edit the notebook w/the location & name as this file. -->
|
|
|
|
|
|
```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>
|