Skip to content

Commit b414e39

Browse files
authored
docs[patch]: Add logprob docs, more updates (#5404)
* Add logprob docs, more updates * More content updates
1 parent caa35d4 commit b414e39

9 files changed

+555
-19
lines changed

docs/core_docs/docs/how_to/chat_model_caching.mdx

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ sidebar_position: 3
44

55
# How to cache chat model responses
66

7+
:::info Prerequisites
8+
9+
This guide assumes familiarity with the following concepts:
10+
11+
- [Chat models](/docs/concepts/#chat-models)
12+
- [LLMs](/docs/concepts/#llms)
13+
14+
:::
15+
716
LangChain provides an optional caching layer for chat models. This is useful for two reasons:
817

918
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.
1019
It can speed up your application by reducing the number of API calls you make to the LLM provider.
1120

12-
import UnifiedModelParamsTooltip from "@mdx_components/unified_model_params_tooltip.mdx";
13-
14-
<UnifiedModelParamsTooltip></UnifiedModelParamsTooltip>
15-
1621
import CodeBlock from "@theme/CodeBlock";
1722

1823
```typescript
@@ -84,7 +89,7 @@ LangChain also provides a Redis-based cache. This is useful if you want to share
8489
To use it, you'll need to install the `redis` package:
8590

8691
```bash npm2yarn
87-
npm install ioredis
92+
npm install ioredis @langchain/community
8893
```
8994

9095
Then, you can pass a `cache` option when you instantiate the LLM. For example:
@@ -105,3 +110,9 @@ By default the cache is stored a temporary directory, but you can specify a cust
105110
```typescript
106111
const cache = await LocalFileCache.create();
107112
```
113+
114+
## Next steps
115+
116+
You've now learned how to cache model responses to save time and money.
117+
118+
Next, check out the other how-to guides on chat models, like [how to get a model to return structured output](/docs/how_to/structured_output) or [how to create your own custom chat model](/docs/how_to/custom_chat_model).

docs/core_docs/docs/how_to/chat_streaming.ipynb

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
"source": [
1818
"# How to stream chat model responses\n",
1919
"\n",
20-
"\n",
2120
"All [chat models](https://api.js.langchain.com/classes/langchain_core_language_models_chat_models.BaseChatModel.html) implement the [Runnable interface](https://api.js.langchain.com/classes/langchain_core_runnables.Runnable.html), which comes with a **default** implementations of standard runnable methods (i.e. `invoke`, `batch`, `stream`, `streamEvents`).\n",
2221
"\n",
23-
"The **default** streaming implementation provides an `AsyncIterator` that yields a single value: the final output from the underlying chat model provider.\n",
22+
"The **default** streaming implementation provides an `AsyncGenerator` that yields a single value: the final output from the underlying chat model provider.\n",
2423
"\n",
2524
":::{.callout-tip}\n",
2625
"\n",
@@ -40,7 +39,7 @@
4039
"source": [
4140
"## Streaming\n",
4241
"\n",
43-
"Below we use a `---` to help visualize the delimiter between tokens."
42+
"Below, we use a `---` to help visualize the delimiter between tokens."
4443
]
4544
},
4645
{
@@ -221,9 +220,9 @@
221220
"source": [
222221
"## Stream events\n",
223222
"\n",
224-
"Chat models also support the standard [astream events](https://api.js.langchain.com/classes/langchain_core_runnables.Runnable.html#streamEvents) method.\n",
223+
"Chat models also support the standard [streamEvents()](https://api.js.langchain.com/classes/langchain_core_runnables.Runnable.html#streamEvents) method.\n",
225224
"\n",
226-
"This method is useful if you're streaming output from a larger LLM application that contains multiple steps (e.g., an LLM chain composed of a prompt, llm and parser)."
225+
"This method is useful if you're streaming output from a larger LLM application that contains multiple steps (e.g., a chain composed of a prompt, chat model and parser)."
227226
]
228227
},
229228
{
@@ -358,6 +357,18 @@
358357
" console.log(event)\n",
359358
"}"
360359
]
360+
},
361+
{
362+
"cell_type": "markdown",
363+
"id": "abacb301",
364+
"metadata": {},
365+
"source": [
366+
"## Next steps\n",
367+
"\n",
368+
"You've now seen a few ways you can stream chat model responses.\n",
369+
"\n",
370+
"Next, check out this guide for more on [streaming with other LangChain modules](/docs/how_to/streaming)."
371+
]
361372
}
362373
],
363374
"metadata": {

docs/core_docs/docs/how_to/chat_token_usage_tracking.mdx

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ sidebar_position: 5
44

55
# How to track token usage
66

7+
:::info Prerequisites
8+
9+
This guide assumes familiarity with the following concepts:
10+
11+
- [Chat models](/docs/concepts/#chat-models)
12+
13+
:::
14+
715
This notebook goes over how to track your token usage for specific calls.
816

9-
## Using AIMessage.response_metadata
17+
## Using `AIMessage.response_metadata`
1018

11-
A number of model providers return token usage information as part of the chat generation response. When available, this is included in the [AIMessage.response_metadata](/docs/modules/model_io/chat/response_metadata/) field.
19+
A number of model providers return token usage information as part of the chat generation response. When available, this is included in the [`AIMessage.response_metadata`](/docs/modules/model_io/chat/response_metadata/) field.
1220
Here's an example with OpenAI:
1321

1422
import CodeBlock from "@theme/CodeBlock";
@@ -42,3 +50,9 @@ Here's an example of how you could do that:
4250
import CallbackExample from "@examples/models/chat/token_usage_tracking_callback.ts";
4351

4452
<CodeBlock language="typescript">{CallbackExample}</CodeBlock>
53+
54+
## Next steps
55+
56+
You've now seen a few examples of how to track chat model token usage for supported providers.
57+
58+
Next, check out the other how-to guides on chat models in this section, like [how to get a model to return structured output](/docs/how_to/structured_output) or [how to add caching to your chat models](/docs/how_to/chat_model_caching).

docs/core_docs/docs/how_to/custom_chat.mdx

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ sidebar_position: 4
44

55
# How to create a custom chat model class
66

7+
:::info Prerequisites
8+
9+
This guide assumes familiarity with the following concepts:
10+
11+
- [Chat models](/docs/concepts/#chat-models)
12+
13+
:::
14+
715
This notebook goes over how to create a custom chat model wrapper, in case you want to use your own chat model or a different wrapper than one that is directly supported in LangChain.
816

917
There are a few required things that a chat model needs to implement after extending the [`SimpleChatModel` class](https://api.js.langchain.com/classes/langchain_core_language_models_chat_models.SimpleChatModel.html):

docs/core_docs/docs/how_to/custom_llm.mdx

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ sidebar_position: 3
44

55
# How to create a custom LLM class
66

7+
:::info Prerequisites
8+
9+
This guide assumes familiarity with the following concepts:
10+
11+
- [LLMs](/docs/concepts/#llms)
12+
13+
:::
14+
715
This notebook goes over how to create a custom LLM wrapper, in case you want to use your own LLM or a different wrapper than one that is directly supported in LangChain.
816

917
There are a few required things that a custom LLM needs to implement after extending the [`LLM` class](https://api.js.langchain.com/classes/langchain_core_language_models_llms.LLM.html):

docs/core_docs/docs/how_to/llm_caching.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,9 @@ By default the cache is stored a temporary directory, but you can specify a cust
193193
```typescript
194194
const cache = await LocalFileCache.create();
195195
```
196+
197+
## Next steps
198+
199+
You've now learned how to cache model responses to save time and money.
200+
201+
Next, check out the other how-to guides on LLMs, like [how to create your own custom LLM class](/docs/how_to/custom_llm).

docs/core_docs/docs/how_to/llm_token_usage_tracking.mdx

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ sidebar_position: 5
44

55
# How to track token usage
66

7-
This notebook goes over how to track your token usage for specific calls. This is currently only implemented for the OpenAI API.
7+
:::info Prerequisites
88

9-
Here's an example of tracking token usage for a single LLM call:
9+
This guide assumes familiarity with the following concepts:
10+
11+
- [LLMs](/docs/concepts/#llms)
12+
13+
:::
14+
15+
This notebook goes over how to track your token usage for specific LLM calls. This is only implemented by some providers, including OpenAI.
16+
17+
Here's an example of tracking token usage for a single LLM call via a callback:
1018

1119
import CodeBlock from "@theme/CodeBlock";
12-
import Example from "@examples/models/chat/token_usage_tracking.ts";
20+
import Example from "@examples/models/llm/token_usage_tracking.ts";
1321

1422
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
1523

@@ -22,3 +30,9 @@ npm install @langchain/openai
2230
<CodeBlock language="typescript">{Example}</CodeBlock>
2331

2432
If this model is passed to a chain or agent that calls it multiple times, it will log an output each time.
33+
34+
## Next steps
35+
36+
You've now seen how to get token usage for supported LLM providers.
37+
38+
Next, check out the other how-to guides in this section, like [how to implement your own custom LLM](/docs/how_to/custom_llm).

0 commit comments

Comments
 (0)