Skip to content

docs[patch]: Guide content fixes #5400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/core_docs/docs/how_to/example_selectors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
"source": [
"# How to use example selectors\n",
"\n",
"```{=mdx}\n",
"import PrerequisiteLinks from \"@theme/PrerequisiteLinks\";\n",
"\n",
"<PrerequisiteLinks\n",
" content={`\n",
"- [Prompt templates](/docs/concepts/#prompt-templates)\n",
"- [Few-shot examples](/docs/how_to/few_shot_examples)\n",
"`}\n",
"/>\n",
"```\n",
"\n",
"If you have a large number of examples, you may need to select which ones to include in the prompt. The Example Selector is the class responsible for doing so.\n",
"\n",
"The base interface is defined as below:\n",
Expand Down Expand Up @@ -244,7 +255,16 @@
"| Name | Description |\n",
"|------------|---------------------------------------------------------------------------------------------|\n",
"| Similarity | Uses semantic similarity between inputs and examples to decide which examples to choose. |\n",
"| Length | Selects examples based on how many can fit within a certain length |"
"| Length | Selects examples based on how many can fit within a certain length |\n",
"\n",
"## Next steps\n",
"\n",
"You've now learned a bit about using example selectors to few shot LLMs.\n",
"\n",
"Next, check out some guides on some other techniques for selecting examples:\n",
"\n",
"- [How to select examples by length](/docs/how_to/example_selectors_length)\n",
"- [How to select examples by similarity](/docs/how_to/example_selectors_similarity)"
]
}
],
Expand Down
17 changes: 17 additions & 0 deletions docs/core_docs/docs/how_to/example_selectors_length_based.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# How to select examples by length

```{=mdx}
import PrerequisiteLinks from "@theme/PrerequisiteLinks";

<PrerequisiteLinks
content={`
- [Prompt templates](/docs/concepts/#prompt-templates)
- [Example selectors](/docs/how_to/example_selectors)
`}
/>
```

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.
Expand All @@ -8,3 +19,9 @@ import CodeBlock from "@theme/CodeBlock";
import ExampleLength from "@examples/prompts/length_based_example_selector.ts";

<CodeBlock language="typescript">{ExampleLength}</CodeBlock>

## Next steps

You've now learned a bit about using a length based example selector.

Next, check out this guide on how to use a [similarity based example selector](/docs/how_to/example_selectors_similarity).
18 changes: 18 additions & 0 deletions docs/core_docs/docs/how_to/example_selectors_similarity.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# How to select examples by similarity

```{=mdx}
import PrerequisiteLinks from "@theme/PrerequisiteLinks";

<PrerequisiteLinks
content={`
- [Prompt templates](/docs/concepts/#prompt-templates)
- [Example selectors](/docs/how_to/example_selectors)
- [Vector stores](/docs/concepts#vectorstores)
`}
/>
```

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.

Expand Down Expand Up @@ -54,3 +66,9 @@ besides similarity search such as maximal marginal relevance:
import ExampleSimilarityCustomRetriever from "@examples/prompts/semantic_similarity_example_selector_custom_retriever.ts";

<CodeBlock language="typescript">{ExampleSimilarityCustomRetriever}</CodeBlock>

## Next steps

You've now learned a bit about using similarity in an example selector.

Next, check out this guide on how to use a [length-based example selector](/docs/how_to/example_selectors_length_based).
9 changes: 9 additions & 0 deletions docs/core_docs/docs/how_to/fallbacks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import CodeBlock from "@theme/CodeBlock";

# Fallbacks

:::info Prerequisites

This guide assumes familiarity with the following concepts:

- [LangChain Expression Language (LCEL)](/docs/concepts/#langchain-expression-language)
- [Chaining runnables](/docs/how_to/sequence/)

:::

When working with language models, you may encounter issues from the underlying APIs, e.g. rate limits or downtime.
As you move your LLM applications into production it becomes more and more important to have contingencies for errors.
That's why we've introduced the concept of fallbacks.
Expand Down
22 changes: 16 additions & 6 deletions docs/core_docs/docs/how_to/message_history.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# How to add message history

:::info Prerequisites

This guide assumes familiarity with the following concepts:

- [LangChain Expression Language (LCEL)](/docs/concepts/#langchain-expression-language)
- [Chaining runnables](/docs/how_to/sequence/)
- [Configuring chain parameters at runtime](/docs/how_to/configure)
- [Prompt templates](/docs/concepts/#prompt-templates)
- [Chat Messages](/docs/concepts/#message-types)

:::

The `RunnableWithMessageHistory` let's us add message history to certain types of chains.

Specifically, it can be used for any Runnable that takes as input one of

- a sequence of `BaseMessage`
- a sequence of [`BaseMessages`](/docs/concepts/#message-types)
- a dict with a key that takes a sequence of `BaseMessage`
- a dict with a key that takes the latest message(s) as a string or sequence of `BaseMessage`, and a separate key that takes historical messages

Expand Down Expand Up @@ -38,9 +50,7 @@ export LANGCHAIN_TRACING_V2="true"
export LANGCHAIN_API_KEY="<your-api-key>"
```

## Example: Dict input, message output

Let's create a simple chain that takes a dict as input and returns a BaseMessage.
Let's create a simple runnable that takes a dict as input and returns a `BaseMessage`.

In this case the `"question"` key in the input represents our input message, and the `"history"` key is where our historical messages will be injected.

Expand Down Expand Up @@ -69,9 +79,9 @@ const chain = prompt.pipe(

To add message history to our original chain we wrap it in the `RunnableWithMessageHistory` class.

Crucially, we also need to define a method that takes a `sessionId` string and based on it returns a `BaseChatMessageHistory`. Given the same input, this method should return an equivalent output.
Crucially, we also need to define a `getMessageHistory()` method that takes a `sessionId` string and based on it returns a `BaseChatMessageHistory`. Given the same input, this method should return an equivalent output.

In this case we'll also want to specify `inputMessagesKey` (the key to be treated as the latest input message) and `historyMessagesKey` (the key to add historical messages to).
In this case, we'll also want to specify `inputMessagesKey` (the key to be treated as the latest input message) and `historyMessagesKey` (the key to add historical messages to).

```typescript
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
Expand Down
2 changes: 1 addition & 1 deletion docs/core_docs/docs/how_to/passthrough.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"id": "392cd4c4-e7ed-4ab8-934d-f7a4eca55ee1",
"metadata": {},
"source": [
"Here the input to prompt is expected to be a map with keys \"context\" and \"question\". The user input is just the question. So we need to get the context using our retriever and passthrough the user input under the \"question\" key. The `RunnablePassthrough` allows us to pass on the user's question to the prompt and model. \n",
"Here the input to prompt is expected to be a map with keys `\"context\"` and `\"question\"`. The user input is just the question. So we need to get the context using our retriever and passthrough the user input under the `\"question\"` key. The `RunnablePassthrough` allows us to pass on the user's question to the prompt and model.\n",
"\n",
"## Next steps\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/core_docs/docs/how_to/prompts_composition.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"source": [
"# How to compose prompts together\n",
"\n",
"LangChain provides a user friendly interface for composing different parts of prompts together. You can do this with either string prompts or chat prompts. Constructing prompts this way allows for easy reuse of components.\n",
"\n",
"```{=mdx}\n",
"import PrerequisiteLinks from \"@theme/PrerequisiteLinks\";\n",
"\n",
"<PrerequisiteLinks content={`- [Prompt templates](/docs/concepts/#prompt-templates)`} />\n",
"```"
"```\n",
"\n",
"LangChain provides a user friendly interface for composing different parts of prompts together. You can do this with either string prompts or chat prompts. Constructing prompts this way allows for easy reuse of components."
]
},
{
Expand Down
22 changes: 14 additions & 8 deletions docs/core_docs/docs/how_to/prompts_partial.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# How to partially format prompt templates

import PrerequisiteLinks from "@theme/PrerequisiteLinks";

<PrerequisiteLinks
content={`
- [Prompt templates](/docs/concepts/#prompt-templates)
`}
/>

Like partially binding arguments to a function, 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.

Expand All @@ -10,14 +18,6 @@ LangChain supports this in two ways:

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

import PrerequisiteLinks from "@theme/PrerequisiteLinks";

<PrerequisiteLinks
content={`
- [Prompt templates](/docs/concepts/#prompt-templates)
`}
/>

## Partial with strings

One common use case for wanting to partial a prompt template is if you get access to some of the variables in a
Expand Down Expand Up @@ -113,3 +113,9 @@ console.log(formattedPrompt);

// Tell me a funny joke about the day 2023-07-13T00:54:59.287Z
```

## Next steps

You've now learned how to partially apply variables to your prompt templates.

Next, check out the other how-to guides on prompt templates in this section, like [adding few-shot examples to your prompt templates](/docs/how_to/few_shot_examples_chat).
Loading