seansullivan/Langchain-Docs huggingface dataset:
## Caching

LangChain provides an optional caching layer for LLMs. This is useful for two reasons:

It can save you money by reducing the number of API calls you make to the LLM provider, if you’re often requesting the same completion multiple times. It can speed up your application by reducing the number of API calls you make to the LLM provider.

```
from langchain.globals import set_llm_cachefrom langchain_openai import OpenAI# To make the caching really obvious, lets use a slower model.llm = OpenAI(model_name="gpt-3.5-turbo-instruct", n=2, best_of=2)
```

```
%%timefrom langchain.cache import InMemoryCacheset_llm_cache(InMemoryCache())# The first time, it is not yet in cache, so it should take longerllm.predict("Tell me a joke")
```

```
CPU times: user 13.7 ms, sys: 6.54 ms, total: 20.2 msWall time: 330 ms
```

```
"\n\nWhy couldn't the bicycle stand up by itself? Because it was two-tired!"
```

```
%%time# The second time it is, so it goes fasterllm.predict("Tell me a joke")
```

```
CPU times: user 436 µs, sys: 921 µs, total: 1.36 msWall time: 1.36 ms
```

```
"\n\nWhy couldn't the bicycle stand up by itself? Because it was two-tired!"
```

## SQLite Cache[​](#sqlite-cache "Direct link to SQLite Cache")

```
# We can do the same thing with a SQLite cachefrom langchain.cache import SQLiteCacheset_llm_cache(SQLiteCache(database_path=".langchain.db"))
```

```
%%time# The first time, it is not yet in cache, so it should take longerllm.predict("Tell me a joke")
```

```
CPU times: user 29.3 ms, sys: 17.3 ms, total: 46.7 msWall time: 364 ms
```

```
'\n\nWhy did the tomato turn red?\n\nBecause it saw the salad dressing!'
```

```
%%time# The second time it is, so it goes fasterllm.predict("Tell me a joke")
```

```
CPU times: user 4.58 ms, sys: 2.23 ms, total: 6.8 msWall time: 4.68 ms
```

```
'\n\nWhy did the tomato turn red?\n\nBecause it saw the salad dressing!'
```

* * *

#### Help us out by providing feedback on this documentation page:
All `LLM`s implement the `Runnable` interface, which comes with default implementations of all methods, ie. ainvoke, batch, abatch, stream, astream. This gives all `LLM`s basic support for streaming.

Streaming support defaults to returning an Iterator (or AsyncIterator in the case of async streaming) of a single value, the final result returned by the underlying `LLM` provider. This obviously doesn’t give you token-by-token streaming, which requires native support from the `LLM` provider, but ensures your code that expects an iterator of tokens can work for any of our `LLM` integrations.

```
Verse 1:Bubbles dancing in my glassClear and crisp, it's such a blastRefreshing taste, it's like a dreamSparkling water, you make me beamChorus:Oh sparkling water, you're my delightWith every sip, you make me feel so rightYou're like a party in my mouthI can't get enough, I'm hooked no doubtVerse 2:No sugar, no calories, just pure blissYou're the perfect drink, I must confessFrom lemon to lime, so many flavors to chooseSparkling water, you never fail to amuseChorus:Oh sparkling water, you're my delightWith every sip, you make me feel so rightYou're like a party in my mouthI can't get enough, I'm hooked no doubtBridge:Some may say you're just plain waterBut to me, you're so much moreYou bring a sparkle to my dayIn every single wayChorus:Oh sparkling water, you're my delightWith every sip, you make me feel so rightYou're like a party in my mouthI can't get enough, I'm hooked no doubtOutro:So here's to you, my dear sparkling waterYou'll always be my go-to drink foreverWith your effervescence and refreshing tasteYou'll always have a special place.
```
## CSV parser

This output parser can be used when you want to return a list of comma-separated items.

```
from langchain.output_parsers import CommaSeparatedListOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_openai import ChatOpenAIoutput_parser = CommaSeparatedListOutputParser()format_instructions = output_parser.get_format_instructions()prompt = PromptTemplate(    template="List five {subject}.\n{format_instructions}",    input_variables=["subject"],    partial_variables={"format_instructions": format_instructions},)model = ChatOpenAI(temperature=0)chain = prompt | model | output_parser
```

```
chain.invoke({"subject": "ice cream flavors"})
```

```
['Vanilla', 'Chocolate', 'Strawberry', 'Mint Chocolate Chip', 'Cookies and Cream']
```

```
for s in chain.stream({"subject": "ice cream flavors"}):    print(s)
```

```
['Vanilla']['Chocolate']['Strawberry']['Mint Chocolate Chip']['Cookies and Cream']
```

Find out api documentation for [CommaSeparatedListOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.list.CommaSeparatedListOutputParser.html#langchain_core.output_parsers.list.CommaSeparatedListOutputParser).

* * *

#### Help us out by providing feedback on this documentation page:
## Datetime parser

This OutputParser can be used to parse LLM output into datetime format.

```
from langchain.output_parsers import DatetimeOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_openai import OpenAI
```

```
output_parser = DatetimeOutputParser()template = """Answer the users question:{question}{format_instructions}"""prompt = PromptTemplate.from_template(    template,    partial_variables={"format_instructions": output_parser.get_format_instructions()},)
```

```
PromptTemplate(input_variables=['question'], partial_variables={'format_instructions': "Write a datetime string that matches the following pattern: '%Y-%m-%dT%H:%M:%S.%fZ'.\n\nExamples: 0668-08-09T12:56:32.732651Z, 1213-06-23T21:01:36.868629Z, 0713-07-06T18:19:02.257488Z\n\nReturn ONLY this string, no other words!"}, template='Answer the users question:\n\n{question}\n\n{format_instructions}')
```

```
chain = prompt | OpenAI() | output_parser
```

```
output = chain.invoke({"question": "when was bitcoin founded?"})
```

Find out api documentation for [DatetimeOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.datetime.DatetimeOutputParser.html#langchain.output_parsers.datetime.DatetimeOutputParser).

* * *

#### Help us out by providing feedback on this documentation page:
## Enum parser

This notebook shows how to use an Enum output parser.

```
from langchain.output_parsers.enum import EnumOutputParser
```

```
from enum import Enumclass Colors(Enum):    RED = "red"    GREEN = "green"    BLUE = "blue"
```

```
parser = EnumOutputParser(enum=Colors)
```

```
from langchain_core.prompts import PromptTemplatefrom langchain_openai import ChatOpenAIprompt = PromptTemplate.from_template(    """What color eyes does this person have?> Person: {person}Instructions: {instructions}""").partial(instructions=parser.get_format_instructions())chain = prompt | ChatOpenAI() | parser
```

```
chain.invoke({"person": "Frank Sinatra"})
```

Find out api documentation for [EnumOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.enum.EnumOutputParser.html#langchain.output_parsers.enum.EnumOutputParser).

* * *

#### Help us out by providing feedback on this documentation page:
## Output-fixing parser

This output parser wraps another output parser, and in the event that the first one fails it calls out to another LLM to fix any errors.

But we can do other things besides throw errors. Specifically, we can pass the misformatted output, along with the formatted instructions, to the model and ask it to fix it.

For this example, we’ll use the above Pydantic output parser. Here’s what happens if we pass it a result that does not comply with the schema:

```
from typing import Listfrom langchain.output_parsers import PydanticOutputParserfrom langchain_core.pydantic_v1 import BaseModel, Fieldfrom langchain_openai import ChatOpenAI
```

```
class Actor(BaseModel):    name: str = Field(description="name of an actor")    film_names: List[str] = Field(description="list of names of films they starred in")actor_query = "Generate the filmography for a random actor."parser = PydanticOutputParser(pydantic_object=Actor)
```

```
misformatted = "{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"
```

```
parser.parse(misformatted)
```

```
OutputParserException: Failed to parse Actor from completion {'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}. Got: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
```

Now we can construct and use a `OutputFixingParser`. This output parser takes as an argument another output parser but also an LLM with which to try to correct any formatting mistakes.

```
from langchain.output_parsers import OutputFixingParsernew_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())
```

```
new_parser.parse(misformatted)
```

```
Actor(name='Tom Hanks', film_names=['Forrest Gump'])
```

Find out api documentation for [OutputFixingParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.fix.OutputFixingParser.html#langchain.output_parsers.fix.OutputFixingParser).
## Pydantic parser

This output parser allows users to specify an arbitrary Pydantic Model and query LLMs for outputs that conform to that schema.

Keep in mind that large language models are leaky abstractions! You’ll have to use an LLM with sufficient capacity to generate well-formed JSON. In the OpenAI family, DaVinci can do reliably but [Curie](https://wiprotechblogs.medium.com/davinci-vs-curie-a-comparison-between-gpt-3-engines-for-extractive-summarization-b568d4633b3b)’s ability already drops off dramatically.

Use Pydantic to declare your data model. Pydantic’s BaseModel is like a Python dataclass, but with actual type checking + coercion.

```
from typing import Listfrom langchain.output_parsers import PydanticOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_core.pydantic_v1 import BaseModel, Field, validatorfrom langchain_openai import ChatOpenAI
```

```
model = ChatOpenAI(temperature=0)
```

```
# Define your desired data structure.class Joke(BaseModel):    setup: str = Field(description="question to set up a joke")    punchline: str = Field(description="answer to resolve the joke")    # You can add custom validation logic easily with Pydantic.    @validator("setup")    def question_ends_with_question_mark(cls, field):        if field[-1] != "?":            raise ValueError("Badly formed question!")        return field# And a query intented to prompt a language model to populate the data structure.joke_query = "Tell me a joke."# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=Joke)prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserchain.invoke({"query": joke_query})
```

```
Joke(setup="Why don't scientists trust atoms?", punchline='Because they make up everything!')
```

```
# Here's another example, but with a compound typed field.class Actor(BaseModel):    name: str = Field(description="name of an actor")    film_names: List[str] = Field(description="list of names of films they starred in")actor_query = "Generate the filmography for a random actor."parser = PydanticOutputParser(pydantic_object=Actor)prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserchain.invoke({"query": actor_query})
```

```
Actor(name='Tom Hanks', film_names=['Forrest Gump', 'Cast Away', 'Saving Private Ryan', 'Toy Story', 'The Green Mile'])
```

Find out api documentation for [PydanticOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.pydantic.PydanticOutputParser.html#langchain_core.output_parsers.pydantic.PydanticOutputParser).
## Retry parser

While in some cases it is possible to fix any parsing mistakes by only looking at the output, in other cases it isn’t. An example of this is when the output is not just in the incorrect format, but is partially complete. Consider the below example.

```
from langchain.output_parsers import (    OutputFixingParser,    PydanticOutputParser,)from langchain_core.prompts import (    PromptTemplate,)from langchain_core.pydantic_v1 import BaseModel, Fieldfrom langchain_openai import ChatOpenAI, OpenAI
```

```
template = """Based on the user question, provide an Action and Action Input for what step should be taken.{format_instructions}Question: {query}Response:"""class Action(BaseModel):    action: str = Field(description="action to take")    action_input: str = Field(description="input to the action")parser = PydanticOutputParser(pydantic_object=Action)
```

```
prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)
```

```
prompt_value = prompt.format_prompt(query="who is leo di caprios gf?")
```

```
bad_response = '{"action": "search"}'
```

If we try to parse this response as is, we will get an error:

```
parser.parse(bad_response)
```

```
OutputParserException: Failed to parse Action from completion {"action": "search"}. Got: 1 validation error for Actionaction_input  field required (type=value_error.missing)
```

If we try to use the `OutputFixingParser` to fix this error, it will be confused - namely, it doesn’t know what to actually put for action input.

```
fix_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())
```

```
fix_parser.parse(bad_response)
```

```
Action(action='search', action_input='input')
```

Instead, we can use the RetryOutputParser, which passes in the prompt (as well as the original output) to try again to get a better response.

```
from langchain.output_parsers import RetryOutputParser
```

```
retry_parser = RetryOutputParser.from_llm(parser=parser, llm=OpenAI(temperature=0))
```

```
retry_parser.parse_with_prompt(bad_response, prompt_value)
```

```
Action(action='search', action_input='leo di caprio girlfriend')
```

We can also add the RetryOutputParser easily with a custom chain which transform the raw LLM/ChatModel output into a more workable format.

```
from langchain_core.runnables import RunnableLambda, RunnableParallelcompletion_chain = prompt | OpenAI(temperature=0)main_chain = RunnableParallel(    completion=completion_chain, prompt_value=prompt) | RunnableLambda(lambda x: retry_parser.parse_with_prompt(**x))main_chain.invoke({"query": "who is leo di caprios gf?"})
```

```
Action(action='search', action_input='leo di caprio girlfriend')
```

Find out api documentation for [RetryOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.retry.RetryOutputParser.html#langchain.output_parsers.retry.RetryOutputParser).
## Structured output parser

This output parser can be used when you want to return multiple fields. While the Pydantic/JSON parser is more powerful, this is useful for less powerful models.

```
from langchain.output_parsers import ResponseSchema, StructuredOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_openai import ChatOpenAI
```

```
response_schemas = [    ResponseSchema(name="answer", description="answer to the user's question"),    ResponseSchema(        name="source",        description="source used to answer the user's question, should be a website.",    ),]output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
```

We now get a string that contains instructions for how the response should be formatted, and we then insert that into our prompt.

```
format_instructions = output_parser.get_format_instructions()prompt = PromptTemplate(    template="answer the users question as best as possible.\n{format_instructions}\n{question}",    input_variables=["question"],    partial_variables={"format_instructions": format_instructions},)
```

```
model = ChatOpenAI(temperature=0)chain = prompt | model | output_parser
```

```
chain.invoke({"question": "what's the capital of france?"})
```

```
{'answer': 'The capital of France is Paris.', 'source': 'https://en.wikipedia.org/wiki/Paris'}
```

```
for s in chain.stream({"question": "what's the capital of france?"}):    print(s)
```

```
{'answer': 'The capital of France is Paris.', 'source': 'https://en.wikipedia.org/wiki/Paris'}
```

Find out api documentation for [StructuredOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.structured.StructuredOutputParser.html#langchain.output_parsers.structured.StructuredOutputParser).

* * *

#### Help us out by providing feedback on this documentation page:
## YAML parser

This output parser allows users to specify an arbitrary schema and query LLMs for outputs that conform to that schema, using YAML to format their response.

Keep in mind that large language models are leaky abstractions! You’ll have to use an LLM with sufficient capacity to generate well-formed YAML. In the OpenAI family, DaVinci can do reliably but Curie’s ability already drops off dramatically.

You can optionally use Pydantic to declare your data model.

```
from typing import Listfrom langchain.output_parsers import YamlOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_core.pydantic_v1 import BaseModel, Fieldfrom langchain_openai import ChatOpenAI
```

```
model = ChatOpenAI(temperature=0)
```

```
# Define your desired data structure.class Joke(BaseModel):    setup: str = Field(description="question to set up a joke")    punchline: str = Field(description="answer to resolve the joke")
```

```
# And a query intented to prompt a language model to populate the data structure.joke_query = "Tell me a joke."# Set up a parser + inject instructions into the prompt template.parser = YamlOutputParser(pydantic_object=Joke)prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)chain = prompt | model | parserchain.invoke({"query": joke_query})
```

```
Joke(setup="Why don't scientists trust atoms?", punchline='Because they make up everything!')
```

Find out api documentation for [YamlOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.yaml.YamlOutputParser.html#langchain.output_parsers.yaml.YamlOutputParser).
## Prompts

A prompt for a language model is a set of instructions or input provided by a user to guide the model's response, helping it understand the context and generate relevant and coherent language-based output, such as answering questions, completing sentences, or engaging in a conversation.

## [Quickstart](https://python.langchain.com/docs/modules/model_io/prompts/quick_start/)[​](#quickstart "Direct link to quickstart")

This [quick start](https://python.langchain.com/docs/modules/model_io/prompts/quick_start/) provides a basic overview of how to work with prompts.

## How-To Guides[​](#how-to-guides "Direct link to How-To Guides")

We have many how-to guides for working with prompts. These include:

*   [How to use few-shot examples with LLMs](https://python.langchain.com/docs/modules/model_io/prompts/few_shot_examples/)
*   [How to use few-shot examples with chat models](https://python.langchain.com/docs/modules/model_io/prompts/few_shot_examples_chat/)
*   [How to use example selectors](https://python.langchain.com/docs/modules/model_io/prompts/example_selectors/)
*   [How to partial prompts](https://python.langchain.com/docs/modules/model_io/prompts/partial/)
*   [How to compose prompts together](https://python.langchain.com/docs/modules/model_io/prompts/composition/)

## [Example Selector Types](https://python.langchain.com/docs/modules/model_io/prompts/example_selectors/)[​](#example-selector-types "Direct link to example-selector-types")

LangChain has a few different types of example selectors you can use off the shelf. You can explore those types [here](https://python.langchain.com/docs/modules/model_io/prompts/example_selectors/)
The `MaxMarginalRelevanceExampleSelector` selects examples based on a combination of which examples are most similar to the inputs, while also optimizing for diversity. It does this by finding the examples with the embeddings that have the greatest cosine similarity with the inputs, and then iteratively adding them while penalizing them for closeness to already selected examples.

```
from langchain_community.vectorstores import FAISSfrom langchain_core.example_selectors import (    MaxMarginalRelevanceExampleSelector,    SemanticSimilarityExampleSelector,)from langchain_core.prompts import FewShotPromptTemplate, PromptTemplatefrom langchain_openai import OpenAIEmbeddingsexample_prompt = PromptTemplate(    input_variables=["input", "output"],    template="Input: {input}\nOutput: {output}",)# 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_selector = MaxMarginalRelevanceExampleSelector.from_examples(    # The list of examples available to select from.    examples,    # The embedding class used to produce embeddings which are used to measure semantic similarity.    OpenAIEmbeddings(),    # The VectorStore class that is used to store the embeddings and do a similarity search over.    FAISS,    # The number of examples to produce.    k=2,)mmr_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"],)
```

```
# Input is a feeling, so should select the happy/sad example as the first oneprint(mmr_prompt.format(adjective="worried"))
```

```
Give the antonym of every inputInput: happyOutput: sadInput: windyOutput: calmInput: worriedOutput:
```

```
# Let's compare this to what we would just get if we went solely off of similarity,# by using SemanticSimilarityExampleSelector instead of MaxMarginalRelevanceExampleSelector.example_selector = SemanticSimilarityExampleSelector.from_examples(    # The list of examples available to select from.    examples,    # The embedding class used to produce embeddings which are used to measure semantic similarity.    OpenAIEmbeddings(),    # The VectorStore class that is used to store the embeddings and do a similarity search over.    FAISS,    # The number of examples to produce.    k=2,)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"],)print(similar_prompt.format(adjective="worried"))
```

```
Give the antonym of every inputInput: happyOutput: sadInput: sunnyOutput: gloomyInput: worriedOutput:
```
This object selects examples based on similarity to the inputs. It does this by finding the examples with the embeddings that have the greatest cosine similarity with the inputs.

```
from langchain_chroma import Chromafrom langchain_core.example_selectors import SemanticSimilarityExampleSelectorfrom langchain_core.prompts import FewShotPromptTemplate, PromptTemplatefrom langchain_openai import OpenAIEmbeddingsexample_prompt = PromptTemplate(    input_variables=["input", "output"],    template="Input: {input}\nOutput: {output}",)# 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_selector = SemanticSimilarityExampleSelector.from_examples(    # The list of examples available to select from.    examples,    # The embedding class used to produce embeddings which are used to measure semantic similarity.    OpenAIEmbeddings(),    # The VectorStore class that is used to store the embeddings and do a similarity search over.    Chroma,    # 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"],)
```

```
Give the antonym of every inputInput: happyOutput: sadInput: worriedOutput:
```

```
Give the antonym of every inputInput: tallOutput: shortInput: largeOutput:
```

```
# You can add new examples to the SemanticSimilarityExampleSelector as wellsimilar_prompt.example_selector.add_example(    {"input": "enthusiastic", "output": "apathetic"})print(similar_prompt.format(adjective="passionate"))
```

```
Give the antonym of every inputInput: enthusiasticOutput: apatheticInput: passionateOutput:
```
This example selector selects which examples to use based on length. This is useful when you are worried about constructing a prompt that will go over the length of the context window. For longer inputs, it will select fewer examples to include, while for shorter inputs it will select more.

```
from langchain_core.example_selectors import LengthBasedExampleSelectorfrom langchain_core.prompts import FewShotPromptTemplate, PromptTemplate# 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(    # The examples it has available to choose from.    examples=examples,    # The PromptTemplate being used to format the examples.    example_prompt=example_prompt,    # The maximum length that the formatted examples should be.    # Length is measured by the get_text_length function below.    max_length=25,    # 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"],)
```

```
# An example with small input, so it selects all examples.print(dynamic_prompt.format(adjective="big"))
```

```
Give the antonym of every inputInput: happyOutput: sadInput: tallOutput: shortInput: energeticOutput: lethargicInput: sunnyOutput: gloomyInput: windyOutput: calmInput: bigOutput:
```

```
# 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))
```

```
Give the antonym of every inputInput: happyOutput: sadInput: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything elseOutput:
```

```
# 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"))
```

```
Give the antonym of every inputInput: happyOutput: sadInput: tallOutput: shortInput: energeticOutput: lethargicInput: sunnyOutput: gloomyInput: windyOutput: calmInput: bigOutput: smallInput: enthusiasticOutput:
```
## Partial prompt templates

Like other methods, it can make sense to “partial” a prompt template - e.g. pass in a subset of the required values, as to create a new prompt template which expects only the remaining subset of values.

LangChain supports this in two ways: 1. Partial formatting with string values. 2. Partial formatting with functions that return string values.

These two different ways support different use cases. In the examples below, we go over the motivations for both use cases as well as how to do it in LangChain.

## Partial with strings[​](#partial-with-strings "Direct link to 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:

```
from langchain_core.prompts import PromptTemplateprompt = PromptTemplate.from_template("{foo}{bar}")partial_prompt = prompt.partial(foo="foo")print(partial_prompt.format(bar="baz"))
```

You can also just initialize the prompt with the partialed variables.

```
prompt = PromptTemplate(    template="{foo}{bar}", input_variables=["bar"], partial_variables={"foo": "foo"})print(prompt.format(bar="baz"))
```

## Partial with functions[​](#partial-with-functions "Direct link to 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.

```
from datetime import datetimedef _get_datetime():    now = datetime.now()    return now.strftime("%m/%d/%Y, %H:%M:%S")
```

```
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"))
```

```
Tell me a funny joke about the day 12/27/2023, 10:45:22
```

You can also just initialize the prompt with the partialed variables, which often makes more sense in this workflow.

```
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"))
```

```
Tell me a funny joke about the day 12/27/2023, 10:45:36
```
Toolkits are collections of tools that are designed to be used together for specific tasks. They have convenient loading methods. For a complete list of available ready-made toolkits, visit [Integrations](https://python.langchain.com/docs/integrations/toolkits/).

All Toolkits expose a `get_tools` method which returns a list of tools. You can therefore do:

```
# Initialize a toolkittoolkit = ExampleTookit(...)# Get list of toolstools = toolkit.get_tools()# Create agentagent = create_agent_method(llm, tools, prompt)
```
## Tools as OpenAI Functions

This notebook goes over how to use LangChain tools as OpenAI functions.

```
%pip install -qU langchain-community langchain-openai
```

```
from langchain_community.tools import MoveFileToolfrom langchain_core.messages import HumanMessagefrom langchain_core.utils.function_calling import convert_to_openai_functionfrom langchain_openai import ChatOpenAI
```

```
model = ChatOpenAI(model="gpt-3.5-turbo")
```

```
tools = [MoveFileTool()]functions = [convert_to_openai_function(t) for t in tools]
```

```
{'name': 'move_file', 'description': 'Move or rename a file from one location to another', 'parameters': {'type': 'object',  'properties': {'source_path': {'description': 'Path of the file to move',    'type': 'string'},   'destination_path': {'description': 'New path for the moved file',    'type': 'string'}},  'required': ['source_path', 'destination_path']}}
```

```
message = model.invoke(    [HumanMessage(content="move file foo to bar")], functions=functions)
```

```
AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}})
```

```
message.additional_kwargs["function_call"]
```

```
{'name': 'move_file', 'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}'}
```

With OpenAI chat models we can also automatically bind and convert function-like objects with `bind_functions`

```
model_with_functions = model.bind_functions(tools)model_with_functions.invoke([HumanMessage(content="move file foo to bar")])
```

```
AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}})
```

Or we can use the update OpenAI API that uses `tools` and `tool_choice` instead of `functions` and `function_call` by using `ChatOpenAI.bind_tools`:

```
model_with_tools = model.bind_tools(tools)model_with_tools.invoke([HumanMessage(content="move file foo to bar")])
```

```
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_btkY3xV71cEVAOHnNa5qwo44', 'function': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}, 'type': 'function'}]})
```

* * *

#### Help us out by providing feedback on this documentation page:
## 📕 Package Versioning

As of now, LangChain has an ad hoc release process: releases are cut with high frequency by a maintainer and published to [PyPI](https://pypi.org/). The different packages are versioned slightly differently.

## `langchain-core`[​](#langchain-core "Direct link to langchain-core")

`langchain-core` is currently on version `0.1.x`.

As `langchain-core` contains the base abstractions and runtime for the whole LangChain ecosystem, we will communicate any breaking changes with advance notice and version bumps. The exception for this is anything marked with the `beta` decorator (you can see this in the API reference and will see warnings when using such functionality). The reason for beta features is that given the rate of change of the field, being able to move quickly is still a priority.

Minor version increases will occur for:

*   Breaking changes for any public interfaces marked as `beta`.

Patch version increases will occur for:

*   Bug fixes
*   New features
*   Any changes to private interfaces
*   Any changes to `beta` features

## `langchain`[​](#langchain "Direct link to langchain")

`langchain` is currently on version `0.1.x`

Minor version increases will occur for:

*   Breaking changes for any public interfaces NOT marked as `beta`.

Patch version increases will occur for:

*   Bug fixes
*   New features
*   Any changes to private interfaces
*   Any changes to `beta` features

We are working on the `langchain` v0.2 release, which will have some breaking changes to legacy Chains and Agents. Additionally, we will remove `langchain-community` as a dependency and stop re-exporting integrations that have been moved to `langchain-community`.

`langchain-community` is currently on version `0.0.x`

All changes will be accompanied by a patch version increase.

## `langchain-experimental`[​](#langchain-experimental "Direct link to langchain-experimental")

`langchain-experimental` is currently on version `0.0.x`

All changes will be accompanied by a patch version increase.

## Partner Packages[​](#partner-packages "Direct link to Partner Packages")

Partner packages are versioned independently.
## rag-gpt-crawler

GPT-crawler will crawl websites to produce files for use in custom GPTs or other apps (RAG).

This template uses [gpt-crawler](https://github.com/BuilderIO/gpt-crawler) to build a RAG app

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.

## Crawling[​](#crawling "Direct link to Crawling")

Run GPT-crawler to extact content from a set of urls, using the config file in GPT-crawler repo.

Here is example config for LangChain use-case docs:

```
export const config: Config = {  url: "https://python.langchain.com/docs/use_cases/",  match: "https://python.langchain.com/docs/use_cases/**",  selector: ".docMainContainer_gTbr",  maxPagesToCrawl: 10,  outputFileName: "output.json",};
```

Then, run this as described in the [gpt-crawler](https://github.com/BuilderIO/gpt-crawler) README:

And copy the `output.json` file into the folder containing this README.

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package rag-gpt-crawler
```

If you want to add this to an existing project, you can just run:

```
langchain app add rag-gpt-crawler
```

And add the following code to your `server.py` file:

```
from rag_chroma import chain as rag_gpt_crawleradd_routes(app, rag_gpt_crawler, path="/rag-gpt-crawler")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/rag-gpt-crawler/playground](http://127.0.0.1:8000/rag-gpt-crawler/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/rag-gpt-crawler")
```
## rag-jaguardb

This template performs RAG using JaguarDB and OpenAI.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

You should export two environment variables, one being your Jaguar URI, the other being your OpenAI API KEY. If you do not have JaguarDB set up, see the `Setup Jaguar` section at the bottom for instructions on how to do so.

```
export JAGUAR_API_KEY=...export OPENAI_API_KEY=...
```

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package rag-jaguardb
```

If you want to add this to an existing project, you can just run:

```
langchain app add rag-jagaurdb
```

And add the following code to your `server.py` file:

```
from rag_jaguardb import chain as rag_jaguardbadd_routes(app, rag_jaguardb_chain, path="/rag-jaguardb")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/rag-jaguardb/playground](http://127.0.0.1:8000/rag-jaguardb/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/rag-jaguardb")
```

## JaguarDB Setup[​](#jaguardb-setup "Direct link to JaguarDB Setup")

To utilize JaguarDB, you can use docker pull and docker run commands to quickly setup JaguarDB.

```
docker pull jaguardb/jaguardb docker run -d -p 8888:8888 --name jaguardb jaguardb/jaguardb
```

To launch the JaguarDB client terminal to interact with JaguarDB server:

```
docker exec -it jaguardb /home/jaguar/jaguar/bin/jag
```

Another option is to download an already-built binary package of JaguarDB on Linux, and deploy the database on a single node or in a cluster of nodes. The streamlined process enables you to quickly start using JaguarDB and leverage its powerful features and functionalities. [here](http://www.jaguardb.com/download.html).
## anthropic-iterative-search

This template will create a virtual research assistant with the ability to search Wikipedia to find answers to your questions.

It is heavily inspired by [this notebook](https://github.com/anthropics/anthropic-cookbook/blob/main/long_context/wikipedia-search-cookbook.ipynb).

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Set the `ANTHROPIC_API_KEY` environment variable to access the Anthropic models.

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package anthropic-iterative-search
```

If you want to add this to an existing project, you can just run:

```
langchain app add anthropic-iterative-search
```

And add the following code to your `server.py` file:

```
from anthropic_iterative_search import chain as anthropic_iterative_search_chainadd_routes(app, anthropic_iterative_search_chain, path="/anthropic-iterative-search")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/anthropic-iterative-search/playground](http://127.0.0.1:8000/anthropic-iterative-search/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/anthropic-iterative-search")
```

* * *

#### Help us out by providing feedback on this documentation page:
## basic-critique-revise

Iteratively generate schema candidates and revise them based on errors.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

This template uses OpenAI function calling, so you will need to set the `OPENAI_API_KEY` environment variable in order to use this template.

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U "langchain-cli[serve]"
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package basic-critique-revise
```

If you want to add this to an existing project, you can just run:

```
langchain app add basic-critique-revise
```

And add the following code to your `server.py` file:

```
from basic_critique_revise import chain as basic_critique_revise_chainadd_routes(app, basic_critique_revise_chain, path="/basic-critique-revise")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/basic-critique-revise/playground](http://127.0.0.1:8000/basic-critique-revise/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/basic-critique-revise")
```

* * *

#### Help us out by providing feedback on this documentation page:
## rag-lancedb

This template performs RAG using LanceDB and OpenAI.

## Environment Setup[​](#environment-setup "Direct link to Environment Setup")

Set the `OPENAI_API_KEY` environment variable to access the OpenAI models.

## Usage[​](#usage "Direct link to Usage")

To use this package, you should first have the LangChain CLI installed:

```
pip install -U langchain-cli
```

To create a new LangChain project and install this as the only package, you can do:

```
langchain app new my-app --package rag-lancedb
```

If you want to add this to as existing project, you can just run:

```
langchain app add rag-lancedb
```

And add the following code to your `server.py` file:

```
from rag_lancedb import chain as rag_lancedb_chainadd_routes(app, rag_lancedb_chain, path="/rag-lancedb")
```

(Optional) Let's now configure LangSmith. LangSmith will help us trace, monitor and debug LangChain applications. You can sign up for LangSmith [here](https://smith.langchain.com/). If you don't have access, you can skip this section

```
export LANGCHAIN_TRACING_V2=trueexport LANGCHAIN_API_KEY=<your-api-key>export LANGCHAIN_PROJECT=<your-project>  # if not specified, defaults to "default"
```

If you are inside this directory, then you can spin up a LangServe instance directly by:

This will start the FastAPI app with a server is running locally at [http://localhost:8000](http://localhost:8000/)

We can see all templates at [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) We can access the playground at [http://127.0.0.1:8000/rag-lancedb/playground](http://127.0.0.1:8000/rag-lancedb/playground)

We can access the template from code with:

```
from langserve.client import RemoteRunnablerunnable = RemoteRunnable("http://localhost:8000/rag-lancedb")
```

* * *

#### Help us out by providing feedback on this documentation page: