Skip to content

Commit

Permalink
Merge branch 'main' into brace/runnable-tool-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul authored Jul 16, 2024
2 parents 23afd3e + 223cdbe commit c436766
Show file tree
Hide file tree
Showing 128 changed files with 5,869 additions and 3,711 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/unit-tests-integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ on:
paths:
- 'langchain-core/**'
- 'libs/**/**'
workflow_dispatch: # Allows triggering the workflow manually in GitHub UI

workflow_dispatch: # Allows triggering the workflow manually in GitHub UI

# If another push to the same PR or branch happens while this workflow is still running,
# cancel the earlier run in favor of the next run.
Expand Down Expand Up @@ -48,6 +47,7 @@ jobs:
PACKAGES: "anthropic,azure-openai,cloudflare,cohere,core,community,exa,google-common,google-gauth,google-genai,google-vertexai,google-vertexai-web,google-webauth,groq,mistralai,mongo,nomic,openai,pinecone,qdrant,redis,textsplitters,weaviate,yandex,baidu-qianfan"
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
matrix_length: ${{ steps.set-matrix.outputs.matrix_length }}
steps:
- id: set-matrix
run: |
Expand All @@ -71,14 +71,17 @@ jobs:
fi
done
matrix="$matrix]}"
matrix_length=$(echo $matrix | jq '.include | length')
echo "Matrix: $matrix"
echo "Matrix length: $matrix_length"
echo "matrix_length=$matrix_length" >> $GITHUB_OUTPUT
echo "matrix=$matrix" >> $GITHUB_OUTPUT
unit-tests:
name: Unit Tests
needs: prepare-matrix
# Only run this job if there are packages to test
if: ${{ fromJson(needs.prepare-matrix.outputs.matrix).include.length != 0 }}
if: needs.prepare-matrix.outputs.matrix_length > 0
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }}
Expand Down
560 changes: 280 additions & 280 deletions .yarn/releases/yarn-3.4.1.cjs → .yarn/releases/yarn-3.5.1.cjs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ plugins:
spec: "@yarnpkg/plugin-typescript"

supportedArchitectures:
os:
- darwin
- linux
cpu:
- x64
- arm64
libc:
- glibc
- musl
os:
- darwin
- linux

yarnPath: .yarn/releases/yarn-3.4.1.cjs
yarnPath: .yarn/releases/yarn-3.5.1.cjs
16 changes: 8 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ To make creating packages like this easier, we offer the [`create-langchain-inte
$ npx create-langchain-integration
```

After creating the new integration package, you should add it to the [`unit-tests-integrations.yml`](./.github/workflows/unit-tests-integrations.yml) GitHub action workflow so that it is tested in CI. To do this, simply add the integration name inside the `jobs.unit-tests.strategy.matrix.package` array:
After creating the new integration package, you should add it to the [`unit-tests-integrations.yml`](./.github/workflows/unit-tests-integrations.yml) GitHub action workflow so that it is tested in CI. To do this,simply update the `env` section of the `prepare-matrix` job with your package name inside the `PACKAGES` variable:

```yaml
jobs:
unit-tests:
name: Unit Tests
strategy:
matrix:
package: [anthropic, azure-openai, cloudflare, <your package name>]
...
prepare-matrix:
needs: get-changed-files
runs-on: ubuntu-latest
env:
PACKAGES: "anthropic,azure-openai,cloudflare,<your-package>"
...
```

### Want to add a feature that's already in Python?
Expand Down Expand Up @@ -159,6 +158,7 @@ cd libs/langchain-community
```

### Setup

**Prerequisite**: Node version 18+ is required. Please check node version `node -v` and update it if required.

To get started, you will need to install the dependencies for the project. To do so, run:
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@langchain/textsplitters": "npm:@langchain/textsplitters",
"@langchain/google-vertexai-web": "npm:@langchain/google-vertexai-web",
"@langchain/mistralai": "npm:@langchain/mistralai",
"@langchain/core/": "npm:/@langchain/core/",
"@langchain/core/": "npm:/@langchain/core@0.2.16/",
"@langchain/pinecone": "npm:@langchain/pinecone",
"@langchain/google-common": "npm:@langchain/google-common",
"@langchain/langgraph": "npm:/@langchain/[email protected]",
Expand Down
6 changes: 6 additions & 0 deletions docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ docs/how_to/document_loader_html.md
docs/how_to/document_loader_html.mdx
docs/how_to/custom_tools.md
docs/how_to/custom_tools.mdx
docs/how_to/custom_llm.md
docs/how_to/custom_llm.mdx
docs/how_to/custom_chat.md
docs/how_to/custom_chat.mdx
docs/how_to/custom_callbacks.md
docs/how_to/custom_callbacks.mdx
docs/how_to/code_splitter.md
Expand All @@ -171,6 +175,8 @@ docs/how_to/character_text_splitter.md
docs/how_to/character_text_splitter.mdx
docs/how_to/callbacks_runtime.md
docs/how_to/callbacks_runtime.mdx
docs/how_to/callbacks_custom_events.md
docs/how_to/callbacks_custom_events.mdx
docs/how_to/callbacks_constructor.md
docs/how_to/callbacks_constructor.mdx
docs/how_to/callbacks_backgrounding.md
Expand Down
4 changes: 3 additions & 1 deletion docs/core_docs/docs/concepts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import useBaseUrl from "@docusaurus/useBaseUrl";
dark: useBaseUrl("/svg/langchain_stack_062024_dark.svg"),
}}
title="LangChain Framework Overview"
style={{ width: "100%" }}
/>

### `@langchain/core`
Expand Down Expand Up @@ -764,7 +765,8 @@ for await (const event of eventStream) {

You can roughly think of it as an iterator over callback events (though the format differs) - and you can use it on almost all LangChain components!

See [this guide](/docs/how_to/streaming/#using-stream-events) for more detailed information on how to use `.streamEvents()`.
See [this guide](/docs/how_to/streaming/#using-stream-events) for more detailed information on how to use `.streamEvents()`,
or [this guide](/docs/how_to/callbacks_custom_events) for how to stream custom events from within a chain.

#### Tokens

Expand Down
232 changes: 232 additions & 0 deletions docs/core_docs/docs/how_to/callbacks_custom_events.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to dispatch custom callback events\n",
"\n",
":::info Prerequisites\n",
"\n",
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [Callbacks](/docs/concepts/#callbacks)\n",
"- [Custom callback handlers](/docs/how_to/custom_callbacks)\n",
"- [Stream Events API](/docs/concepts#streamevents)\n",
"\n",
":::\n",
"\n",
"In some situations, you may want to dipsatch a custom callback event from within a [Runnable](/docs/concepts/#runnable-interface) so it can be surfaced\n",
"in a custom callback handler or via the [Stream Events API](/docs/concepts/#streamevents).\n",
"\n",
"For example, if you have a long running tool with multiple steps, you can dispatch custom events between the steps and use these custom events to monitor progress.\n",
"You could also surface these custom events to an end user of your application to show them how the current task is progressing.\n",
"\n",
"To dispatch a custom event you need to decide on two attributes for the event: the `name` and the `data`.\n",
"\n",
"| Attribute | Type | Description |\n",
"|-----------|------|----------------------------------------------------------------------------------------------------------|\n",
"| name | string | A user defined name for the event. |\n",
"| data | any | The data associated with the event. This can be anything, though we suggest making it JSON serializable. |\n",
"\n",
"\n",
":::info\n",
"- Custom callback events can only be dispatched from within an existing `Runnable`.\n",
"- If using `streamEvents`, you must use `version: \"v2\"` to consume custom events.\n",
"- Sending or rendering custom callback events in LangSmith is not yet supported.\n",
":::\n",
"\n",
"## Stream Events API\n",
"\n",
"The most useful way to consume custom events is via the [`.streamEvents()`](/docs/concepts/#streamevents) method.\n",
"\n",
"We can use the `dispatchCustomEvent` API to emit custom events from this method. \n",
"\n",
"```{=mdx}\n",
":::caution Compatibility\n",
"Dispatching custom callback events requires `@langchain/core>=0.2.16`. See [this guide](/docs/how_to/installation/#installing-integration-packages) for some considerations to take when upgrading `@langchain/core`.\n",
"\n",
"The default entrypoint below triggers an import and initialization of [`async_hooks`](https://nodejs.org/api/async_hooks.html) to enable automatic `RunnableConfig` passing, which is not supported in all environments. If you see import issues, you must import from `@langchain/callbacks/dispatch/web` and propagate the `RunnableConfig` object manually (see example below).\n",
":::\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" event: 'on_custom_event',\n",
" run_id: '9eac217d-3a2d-4563-a91f-3bd49bee4b3d',\n",
" name: 'event1',\n",
" tags: [],\n",
" metadata: {},\n",
" data: { reversed: 'dlrow olleh' }\n",
"}\n",
"{\n",
" event: 'on_custom_event',\n",
" run_id: '9eac217d-3a2d-4563-a91f-3bd49bee4b3d',\n",
" name: 'event2',\n",
" tags: [],\n",
" metadata: {},\n",
" data: 5\n",
"}\n"
]
}
],
"source": [
"import { RunnableLambda } from \"@langchain/core/runnables\";\n",
"import { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\n",
"\n",
"const reflect = RunnableLambda.from(async (value: string) => {\n",
" await dispatchCustomEvent(\"event1\", { reversed: value.split(\"\").reverse().join(\"\") });\n",
" await dispatchCustomEvent(\"event2\", 5);\n",
" return value;\n",
"});\n",
"\n",
"const eventStream = await reflect.streamEvents(\"hello world\", { version: \"v2\" });\n",
"\n",
"for await (const event of eventStream) {\n",
" if (event.event === \"on_custom_event\") {\n",
" console.log(event);\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are in a web environment that does not support `async_hooks`, you must import from the web entrypoint and propagate the config manually instead:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" event: 'on_custom_event',\n",
" run_id: 'dee1e4f0-c5ff-4118-9391-461a0dcc4cb2',\n",
" name: 'event1',\n",
" tags: [],\n",
" metadata: {},\n",
" data: { reversed: 'dlrow olleh' }\n",
"}\n",
"{\n",
" event: 'on_custom_event',\n",
" run_id: 'dee1e4f0-c5ff-4118-9391-461a0dcc4cb2',\n",
" name: 'event2',\n",
" tags: [],\n",
" metadata: {},\n",
" data: 5\n",
"}\n"
]
}
],
"source": [
"import { RunnableConfig, RunnableLambda } from \"@langchain/core/runnables\";\n",
"import { dispatchCustomEvent as dispatchCustomEventWeb } from \"@langchain/core/callbacks/dispatch/web\";\n",
"\n",
"const reflect = RunnableLambda.from(async (value: string, config?: RunnableConfig) => {\n",
" await dispatchCustomEventWeb(\"event1\", { reversed: value.split(\"\").reverse().join(\"\") }, config);\n",
" await dispatchCustomEventWeb(\"event2\", 5, config);\n",
" return value;\n",
"});\n",
"\n",
"const eventStream = await reflect.streamEvents(\"hello world\", { version: \"v2\" });\n",
"\n",
"for await (const event of eventStream) {\n",
" if (event.event === \"on_custom_event\") {\n",
" console.log(event);\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Callback Handler\n",
"\n",
"Let's see how to emit custom events with `dispatchCustomEvent`.\n",
"\n",
"Remember, you **must** call `dispatchCustomEvent` from within an existing `Runnable`."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"event1 { reversed: 'dlrow olleh' } 9c3770ac-c83d-4626-9643-b5fd80eb5431\n",
"event2 5 9c3770ac-c83d-4626-9643-b5fd80eb5431\n",
"hello world\n"
]
}
],
"source": [
"import { RunnableConfig, RunnableLambda } from \"@langchain/core/runnables\";\n",
"import { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\n",
"\n",
"const reflect = RunnableLambda.from(async (value: string) => {\n",
" await dispatchCustomEvent(\"event1\", { reversed: value.split(\"\").reverse().join(\"\") });\n",
" await dispatchCustomEvent(\"event2\", 5);\n",
" return value;\n",
"});\n",
"\n",
"await reflect.invoke(\"hello world\", {\n",
" callbacks: [{\n",
" handleCustomEvent(eventName, data, runId) {\n",
" console.log(eventName, data, runId);\n",
" },\n",
" }]\n",
"});"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Related\n",
"\n",
"You've now seen how to emit custom events from within your chains.\n",
"\n",
"You can check out the more in depth guide for [stream events](/docs/how_to/streaming/#using-stream-events) for more ways to parse and receive intermediate steps from your chains."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "TypeScript",
"language": "typescript",
"name": "tslab"
},
"language_info": {
"codemirror_mode": {
"mode": "typescript",
"name": "javascript",
"typescript": true
},
"file_extension": ".ts",
"mimetype": "text/typescript",
"name": "typescript",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit c436766

Please sign in to comment.