doc updates (#1639)

---------

Co-authored-by: vbarda <vadym@langchain.dev>
This commit is contained in:
Isaac Francisco
2024-09-08 16:55:52 +00:00
committed by GitHub
co-authored by vbarda
parent 38a644cf79
commit db306cd01b
98 changed files with 3227 additions and 978 deletions
+2 -2
View File
@@ -117,7 +117,7 @@ Now we can invoke our graph to ensure it is working. Make sure to change the inp
=== "Python"
```python
input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]}
input = {"messages": [{"role": "user", "content": "what's the weather in sf"}]}
async for chunk in client.runs.stream(
thread["thread_id"],
assistant_id,
@@ -131,7 +131,7 @@ Now we can invoke our graph to ensure it is working. Make sure to change the inp
=== "Javascript"
```js
const input = { "messages": [{ "role": "human", "content": "what's the weather in sf"}] }
const input = { "messages": [{ "role": "user", "content": "what's the weather in sf"}] }
const streamResponse = client.runs.stream(
thread["thread_id"],
+11 -4
View File
@@ -1,8 +1,11 @@
# How to kick off background runs
This guide covers how to kick off background runs for your agent.
This can be useful for long running jobs.
## Setup
First let's set up our client and thread:
=== "Python"
@@ -52,6 +55,8 @@ Output:
'values': None
}
## Check runs on thread
If we list the current runs on this thread, we will see that it's empty:
=== "Python"
@@ -79,19 +84,21 @@ Output:
[]
## Start runs on thread
Now let's kick off a run:
=== "Python"
```python
input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]}
input = {"messages": [{"role": "user", "content": "what's the weather in sf"}]}
run = await client.runs.create(thread["thread_id"], assistant_id, input=input)
```
=== "Javascript"
```js
let input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]};
let input = {"messages": [{"role": "user", "content": "what's the weather in sf"}]};
let run = await client.runs.create(thread["thread_id"], assistantID, { input });
```
@@ -141,7 +148,7 @@ Output:
"input": {
"messages": [
{
"role": "human",
"role": "user",
"content": "what's the weather in sf"
}
]
@@ -212,7 +219,7 @@ Output:
"input": {
"messages": [
{
"role": "human",
"role": "user",
"content": "what's the weather in sf"
}
]
@@ -219,7 +219,7 @@ We can verify the config is indeed taking effect:
"input": {
"messages": [
{
"role": "human",
"role": "user",
"content": "who made you?"
}
]
+10 -5
View File
@@ -1,9 +1,10 @@
## Enqueue
# Enqueue
This guide assumes knowledge of what double-texting is, which you can learn about in the [double-texting conceptual guide](../concepts/api.md#double-texting).
The guide covers the `enqueue` option for double texting, which adds the interruptions to a queue and executes them in the order they are received by the client. Below is a quick example of using the `enqueue` option.
## Setup
First, we will define a quick helper function for printing out JS and CURL model outputs (you can skip this if using Python):
@@ -82,6 +83,8 @@ Then, let's import our required packages and instantiate our client, assistant,
--data '{}'
```
## Create runs
Now let's start two runs, with the second interrupting the first one with a multitask strategy of "enqueue":
=== "Python"
@@ -90,12 +93,12 @@ Now let's start two runs, with the second interrupting the first one with a mult
first_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in sf?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
second_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in nyc?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
multitask_strategy="enqueue",
)
```
@@ -106,13 +109,13 @@ Now let's start two runs, with the second interrupting the first one with a mult
const firstRun = await client.runs.create(
thread["thread_id"],
assistantId,
input={"messages": [{"role": "human", "content": "what's the weather in sf?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
const secondRun = await client.runs.create(
thread["thread_id"],
assistantId,
input={"messages": [{"role": "human", "content": "what's the weather in nyc?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
multitask_strategy="enqueue",
)
```
@@ -136,6 +139,8 @@ Now let's start two runs, with the second interrupting the first one with a mult
}"
```
## View run results
Verify that the thread has data from both runs:
=== "Python"
@@ -61,7 +61,7 @@ And, now let's compile it with a breakpoint before the tool node:
=== "Python"
```python
input = {"messages": [{"role": "human", "content": "what's the weather in sf"}]}
input = {"messages": [{"role": "user", "content": "what's the weather in sf"}]}
async for chunk in client.runs.stream(
thread["thread_id"],
assistant_id,
@@ -77,7 +77,7 @@ Let's look at an example when no review is required (because no tools are called
=== "Javascript"
```js
const input = { "messages": [{ "role": "human", "content": "hi!" }] };
const input = { "messages": [{ "role": "user", "content": "hi!" }] };
const streamResponse = client.runs.stream(
thread["thread_id"],
@@ -64,7 +64,7 @@ Before replaying a state - we need to create states to replay from! In order to
=== "Javascript"
```js
const input = { "messages": [{ "role": "human", "content": "Please search the weather in SF" }] }
const input = { "messages": [{ "role": "user", "content": "Please search the weather in SF" }] }
const streamResponse = client.runs.stream(
thread["thread_id"],
@@ -62,7 +62,7 @@ Now, let's invoke our graph by interrupting before `ask_human` node:
input = {
"messages": [
{
"role": "human",
"role": "user",
"content": "Use the search tool to ask the user where they are, then look up the weather there",
}
]
@@ -1,9 +1,11 @@
## Interrupt
# Interrupt
This guide assumes knowledge of what double-texting is, which you can learn about in the [double-texting conceptual guide](../concepts/api.md#double-texting).
The guide covers the `interrupt` option for double texting, which interrupts the prior run of the graph and starts a new one with the double-text. This option does not delete the first run, but rather keeps it in the database but sets its status to `interrupted`. Below is a quick example of using the `interrupt` option.
## Setup
First, we will define a quick helper function for printing out JS and CURL model outputs (you can skip this if using Python):
=== "Javascript"
@@ -79,6 +81,8 @@ Now, let's import our required packages and instantiate our client, assistant, a
--data '{}'
```
## Create runs
Now we can start our two runs and join the second on euntil it has completed:
=== "Python"
@@ -88,13 +92,13 @@ Now we can start our two runs and join the second on euntil it has completed:
interrupted_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in sf?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
await asyncio.sleep(2)
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in nyc?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
multitask_strategy="interrupt",
)
# wait until the second run completes
@@ -145,6 +149,8 @@ Now we can start our two runs and join the second on euntil it has completed:
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join
```
## View run results
We can see that the thread has partial data from the first run + data from the second run
+10 -5
View File
@@ -1,9 +1,11 @@
## Reject
# Reject
This guide assumes knowledge of what double-texting is, which you can learn about in the [double-texting conceptual guide][double-texting].
The guide covers the `reject` option for double texting, which rejects the new run of the graph by throwing an error and continues with the original run until completion. Below is a quick example of using the `reject` option.
## Setup
First, we will define a quick helper function for printing out JS and CURL model outputs (you can skip this if using Python):
=== "Javascript"
@@ -78,6 +80,8 @@ Now, let's import our required packages and instantiate our client, assistant, a
--data '{}'
```
## Create runs
Now we can run a thread and try to run a second one with the "reject" option, which should fail since we have already started a run:
@@ -87,14 +91,14 @@ Now we can run a thread and try to run a second one with the "reject" option, wh
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in sf?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
try:
await client.runs.create(
thread["thread_id"],
assistant_id,
input={
"messages": [{"role": "human", "content": "what's the weather in nyc?"}]
"messages": [{"role": "user", "content": "what's the weather in nyc?"}]
},
multitask_strategy="reject",
)
@@ -108,7 +112,7 @@ Now we can run a thread and try to run a second one with the "reject" option, wh
const run = await client.runs.create(
thread["thread_id"],
assistantId,
input={"messages": [{"role": "human", "content": "what's the weather in sf?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
);
try {
@@ -116,7 +120,7 @@ Now we can run a thread and try to run a second one with the "reject" option, wh
thread["thread_id"],
assistantId,
{
input: {"messages": [{"role": "human", "content": "what's the weather in nyc?"}]},
input: {"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
multitask_strategy:"reject"
},
);
@@ -149,6 +153,7 @@ Output:
Failed to start concurrent run Client error '409 Conflict' for url 'http://localhost:8123/threads/f9e7088b-8028-4e5c-88d2-9cc9a2870e50/runs'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
## View run results
We can verify that the original thread finished executing:
@@ -1,9 +1,11 @@
## Rollback
# Rollback
This guide assumes knowledge of what double-texting is, which you can learn about in the [double-texting conceptual guide][double-texting].
The guide covers the `rollback` option for double texting, which interrupts the prior run of the graph and starts a new one with the double-text. This option is very similar to the `interrupt` option, but in this case the first run is completely deleted from the database and cannot be restarted. Below is a quick example of using the `rollback` option.
## Setup
First, we will define a quick helper function for printing out JS and CURL model outputs (you can skip this if using Python):
=== "Javascript"
@@ -80,6 +82,8 @@ Now, let's import our required packages and instantiate our client, assistant, a
--data '{}'
```
## Create runs
Now let's run a thread with the multitask parameter set to "rollback":
=== "Python"
@@ -89,13 +93,13 @@ Now let's run a thread with the multitask parameter set to "rollback":
rolled_back_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in sf?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
await asyncio.sleep(2)
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "human", "content": "what's the weather in nyc?"}]},
input={"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
multitask_strategy="rollback",
)
# wait until the second run completes
@@ -146,6 +150,8 @@ Now let's run a thread with the multitask parameter set to "rollback":
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join
```
## View run results
We can see that the thread has data only from the second run
=== "Python"
+10 -2
View File
@@ -6,6 +6,8 @@ This means that you can run multiple agents on the same thread, which allows a d
In this example, we will create two agents and then call them both on the same thread.
You'll see that the second agent will respond using information from the [checkpoint](https://langchain-ai.github.io/langgraph/concepts/low_level/#checkpointer-state) generated in the thread by the first agent as context.
## Setup
=== "Python"
```python
@@ -124,6 +126,10 @@ Output:
}
}
## Run assistants on thread
### Run OpenAI assistant
We can now run the OpenAI assistant on the thread first.
=== "Python"
@@ -178,7 +184,7 @@ We can now run the OpenAI assistant on the thread first.
"input": {
"messages": [
{
"role": "human",
"role": "user",
"content": "who made you?"
}
]
@@ -218,6 +224,8 @@ Output:
Receiving event of type: updates
{'agent': {'messages': [{'content': 'I was created by OpenAI, a research organization focused on developing and advancing artificial intelligence technology.', 'additional_kwargs': {}, 'response_metadata': {'finish_reason': 'stop', 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_157b3831f5'}, 'type': 'ai', 'name': None, 'id': 'run-f5735b86-b80d-4c71-8dc3-4782b5a9c7c8', 'example': False, 'tool_calls': [], 'invalid_tool_calls': [], 'usage_metadata': None}]}}
### Run default assistant
Now, we can run it on the default assistant and see that this second assistant is aware of the initial question, and can answer the question, "and you?":
=== "Python"
@@ -266,7 +274,7 @@ Now, we can run it on the default assistant and see that this second assistant i
"input": {
"messages": [
{
"role": "human",
"role": "user",
"content": "and you?"
}
]
+5 -5
View File
@@ -59,7 +59,7 @@ Now, let's run the graph on the first thread, and provide it some information ab
=== "Python"
```python
input = {"messages": [{"role": "human", "content": "i like pepperoni pizza"}]}
input = {"messages": [{"role": "user", "content": "i like pepperoni pizza"}]}
config = {"configurable": {"user_id": "123"}}
# stream values
async for chunk in client.runs.stream(
@@ -162,7 +162,7 @@ Let's stay on the same thread and provide some additional information. Note that
=== "Python"
```python
input = {"messages": [{"role": "human", "content": "i also just moved to SF"}]}
input = {"messages": [{"role": "user", "content": "i also just moved to SF"}]}
# stream values
async for chunk in client.runs.stream(
thread["thread_id"],
@@ -270,7 +270,7 @@ Now, let's run the graph on a completely different thread, and see that it remem
```python
# new thread for new conversation
thread = await client.threads.create()
input = {"messages": [{"role": "human", "content": "where and what should i eat for dinner? Can you list some restaurants?"}]}
input = {"messages": [{"role": "user", "content": "where and what should i eat for dinner? Can you list some restaurants?"}]}
# stream values
async for chunk in client.runs.stream(
thread["thread_id"],
@@ -330,7 +330,7 @@ Now, let's run the graph on a completely different thread, and see that it remem
"assistant_id": "agent",
"input": {
"messages": [{
"role": "human",
"role": "user",
"content": "where and what should i eat for dinner? Can you list some restaurants?"
}]
},
@@ -389,7 +389,7 @@ Let's now run the graph for another user to verify that the preferences of the f
# new thread for new conversation
thread = await client.threads.create()
# create input
input = {"messages": [{"role": "human", "content": "where do I live? what do I like to eat?"}]}
input = {"messages": [{"role": "user", "content": "where do I live? what do I like to eat?"}]}
config = {"configurable": {"user_id": "321"}}
# stream values
async for chunk in client.runs.stream(
+4 -2
View File
@@ -6,6 +6,8 @@ This guide covers how to stream debug events from your graph (`stream_mode="debu
- `task`: These events will get streamed before each super-step, and will contain information about a single task. Each super-step works by executing a list of tasks, where each task is scoped to a specific node and input. Below we will discuss the format of these tasks in more detail.
- `task_result`: After each `task` event, you will see a corresponding `task_result` event which as the name suggests contains information on the results of the task executed in the super-step. Scroll more to learn about the exact structure of these events.
## Setup
First let's set up our client and thread:
=== "Python"
@@ -56,7 +58,7 @@ Output:
'values': None
}
## Stream graph in debug mode
=== "Python"
@@ -65,7 +67,7 @@ Output:
input = {
"messages": [
{
"role": "human",
"role": "user",
"content": "What's the weather in SF?",
}
]
+5 -3
View File
@@ -2,6 +2,8 @@
This guide covers how to stream events from your graph (`stream_mode="events"`). Depending on the use case and user experience of your LangGraph application, your application may process event types differently. Read more about events in this [conceptual guide](https://langchain-ai.github.io/langgraph/concepts/low_level/#astream_events-for-streaming-tokens-of-llm-calls).
## Setup
=== "Python"
```python
@@ -50,7 +52,7 @@ Output:
'values': None
}
## Stream graph in events mode
Streaming events produces responses containing an `event` key (in addition to other keys such as `data`). See the LangChain [`Runnable.astream_events()` reference](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable.astream_events) for all event types.
@@ -62,7 +64,7 @@ Streaming events produces responses containing an `event` key (in addition to ot
input = {
"messages": [
{
"role": "human",
"role": "user",
"content": "What's the weather in SF?",
}
]
@@ -87,7 +89,7 @@ Streaming events produces responses containing an `event` key (in addition to ot
const input = {
"messages": [
{
"role": "human",
"role": "user",
"content": "What's the weather in SF?",
}
]
@@ -38,6 +38,8 @@ With `stream_mode="messages"` two things will be streamed back:
Read more about how the `messages` streaming mode works [here](https://langchain-ai.github.io/langgraph/cloud/concepts/api/#modemessages)
## Setup
First let's set up our client and thread:
=== "Python"
@@ -179,6 +181,7 @@ Let's also define a helper function for better formatting of the tool calls in m
done
```
## Stream graph in messages mode
Now we can stream by messages, which will return complete messages (at the end of node execution) as well as tokens for any messages generated inside a node:
+5 -1
View File
@@ -2,6 +2,8 @@
This guide covers how to configure multiple streaming modes at the same time.
## Setup
First let's set up our client and thread:
=== "Python"
@@ -51,6 +53,8 @@ Output:
'values': None
}
## Stream graph with multiple modes
When configuring multiple streaming modes for a run, responses for each respective mode will be produced. In the following example, note that a `list` of modes (`messages`, `events`, `debug`) is passed to the `stream_mode` parameter and the response contains `events`, `debug`, `messages/complete`, `messages/metadata`, and `messages/partial` event types.
=== "Python"
@@ -60,7 +64,7 @@ When configuring multiple streaming modes for a run, responses for each respecti
input = {
"messages": [
{
"role": "human",
"role": "user",
"content": "What's the weather in SF?",
}
]
+5 -1
View File
@@ -2,6 +2,8 @@
This guide covers how to use `stream_mode="updates"` for your graph, which will stream the updates to the graph state that are made after each node is executed. This differs from using `stream_mode="values"`: instead of streaming the entire value of the state at each superstep, it only streams the updates from each of the nodes that made an update to the state at that superstep. Read [this conceptual guide](https://langchain-ai.github.io/langgraph/concepts/low_level/#stream-and-astream) to learn more.
## Setup
First let's set up our client and thread:
=== "Python"
@@ -51,6 +53,8 @@ Output:
'values': None
}
## Stream graph in updates mode
Now we can stream by updates, which outputs updates made to the state by each node after it has executed:
@@ -60,7 +64,7 @@ Now we can stream by updates, which outputs updates made to the state by each no
input = {
"messages": [
{
"role": "human",
"role": "user",
"content": "what's the weather in la"
}
]
+6 -2
View File
@@ -2,6 +2,8 @@
This guide covers how to use `stream_mode="values"`, which streams the value of the state at each superstep. This differs from using `stream_mode="updates"`: instead of streaming just the updates to the state from each node, it streams the entire graph state at that superstep. Read [this conceptual guide](https://langchain-ai.github.io/langgraph/concepts/low_level/#stream-and-astream) to learn more.
## Setup
First let's set up our client and thread:
=== "Python"
@@ -51,12 +53,14 @@ Output:
'values': None
}
## Stream graph in values mode
Now we can stream by values, which streams the full state of the graph after each node has finished executing:
=== "Python"
```python
input = {"messages": [{"role": "human", "content": "what's the weather in la"}]}
input = {"messages": [{"role": "user", "content": "what's the weather in la"}]}
# stream values
async for chunk in client.runs.stream(
@@ -73,7 +77,7 @@ Now we can stream by values, which streams the full state of the graph after eac
=== "Javascript"
```js
const input = {"messages": [{"role": "human", "content": "what's the weather in la"}]}
const input = {"messages": [{"role": "user", "content": "what's the weather in la"}]}
const streamResponse = client.runs.stream(
thread["thread_id"],
+8 -2
View File
@@ -14,7 +14,11 @@ The following endpoints accept `webhook` as a parameter:
- Stream Run Stateless -> POST /runs/stream
- Wait Run Stateless -> POST /runs/wait
In this example, we will show calling a webhook after streaming a run. First, let's setup our assistant and thread:
In this example, we will show calling a webhook after streaming a run.
## Setup
First, let's setup our assistant and thread:
=== "Python"
@@ -70,13 +74,15 @@ Output:
'values': None
}
## Use graph with a webhook
Now we can invoke a run with a webhook:
=== "Python"
```python
# create input
input = { "messages": [{ "role": "human", "content": "Hello!" }] }
input = { "messages": [{ "role": "user", "content": "Hello!" }] }
async for chunk in client.runs.stream(
thread_id=thread["thread_id"],