Skip to content

Commit

Permalink
Update Refs to clean up syntax highlighting (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed May 14, 2024
1 parent 2c32a38 commit f335105
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 289 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/link_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check links in Markdown files
uses: gaurav-nelson/github-action-markdown-link-check@v1
with:
Expand All @@ -41,19 +44,23 @@ jobs:
cache-key: core

- name: Install dependencies
shell: bash
run: |
python -m pip install --upgrade pip
poetry install --with test
poetry run pip install -U pytest pytest-check-links langsmith langchain GitPython
- name: Check links in notebooks
env:
LANGCHAIN_API_KEY: test
shell: bash
run: |
if [ "${{ github.event_name }}" != "schedule" ]; then
CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep '\.ipynb$')
poetry run pytest -o python_files=non_python_only --check-links $CHANGED_FILES --check-links-ignore "https://(api|web)\.smith\.langchain\.com/.*" --check-links-ignore "https://x.com/.*"
git fetch origin main
CHANGED_FILES=$(git diff --name-only origin/main | grep '\.ipynb$')
if [ -n "$CHANGED_FILES" ]; then
poetry run pytest -o python_files=non_python_only --check-links --check-links-ignore "https://(api|web)\.smith\.langchain\.com/.*" --check-links-ignore "https://x.com/.*" $CHANGED_FILES
else
echo "No notebook files changed."
fi
else
poetry run pytest -o python_files=non_python_only --check-links --ignore="*.py" -k .ipynb --check-links-ignore "https://(api|web)\.smith\.langchain\.com/.*" --check-links-ignore "https://x.com/.*" ./examples
poetry run pytest -o python_files=non_python_only --check-links --ignore="*.py" -k .ipynb --check-links-ignore "https://(api|web)\.smith\.langchain\.com/.*" --check-links-ignore "https://x.com/.*" ./examples
fi
7 changes: 0 additions & 7 deletions docs/css/mkdocstrings.css

This file was deleted.

20 changes: 13 additions & 7 deletions docs/docs/reference/graphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

Graphs are the core abstraction of LangGraph. Each [StateGraph](#langgraph.graph.StateGraph) implementation is used to create graph workflows. Once compiled, you can run the [CompiledGraph](#compiledgraph) to run the application.

::: langgraph.graph
handler: python
## StateGraph

## CompiledGraph
```python
from langgraph.graph import StateGraph
from typing_extensions import TypedDict
class MyState(TypedDict)
...
graph = StateGraph(MyState)
```

::: langgraph.graph.graph.CompiledGraph
::: langgraph.graph.StateGraph
handler: python


## MessageGraph

::: langgraph.graph.message.MessageGraph


## add_messages
## CompiledGraph

::: langgraph.graph.graph.CompiledGraph
handler: python

::: langgraph.graph.message.add_messages
6 changes: 3 additions & 3 deletions docs/docs/reference/prebuilt.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ from langgraph.prebuilt import ToolInvocation
heading_level: 4


## `chat_agent_executor.create_tool_calling_executor`
## create_react_agent

```python
from langgraph.prebuilt.chat_agent_executor import create_tool_calling_executor
from langgraph.prebuilt import create_react_agent
```

::: langgraph.prebuilt.chat_agent_executor.create_tool_calling_executor
::: langgraph.prebuilt.create_react_agent

## `tools_condition`

Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ markdown_extensions:
- pymdownx.highlight:
anchor_linenums: true
line_spans: __span
use_pygments: true
pygments_lang_class: true
- pymdownx.inlinehilite
- pymdownx.keys
Expand Down
18 changes: 12 additions & 6 deletions examples/plan-and-execute/plan-and-execute.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@
}
],
"source": [
"agent_executor.invoke(\n",
" {\"messages\": [(\"user\", \"who is the winnner of the us open\")]}\n",
")"
"agent_executor.invoke({\"messages\": [(\"user\", \"who is the winnner of the us open\")]})"
]
},
{
Expand Down Expand Up @@ -297,7 +295,11 @@
],
"source": [
"planner.invoke(\n",
" {\"messages\": [(\"user\", \"what is the hometown of the current Australia open winner?\")]}\n",
" {\n",
" \"messages\": [\n",
" (\"user\", \"what is the hometown of the current Australia open winner?\")\n",
" ]\n",
" }\n",
")"
]
},
Expand Down Expand Up @@ -354,7 +356,9 @@
")\n",
"\n",
"\n",
"replanner = replanner_prompt | ChatOpenAI(model=\"gpt-4o\", temperature=0).with_structured_output(Act)"
"replanner = replanner_prompt | ChatOpenAI(\n",
" model=\"gpt-4o\", temperature=0\n",
").with_structured_output(Act)"
]
},
{
Expand Down Expand Up @@ -383,7 +387,9 @@
" task = plan[0]\n",
" task_formatted = f\"\"\"For the following plan:\n",
"{plan_str}\\n\\nYou are tasked with executing step {1}, {task}.\"\"\"\n",
" agent_response = await agent_executor.ainvoke({\"messages\": [(\"user\", task_formatted)]})\n",
" agent_response = await agent_executor.ainvoke(\n",
" {\"messages\": [(\"user\", task_formatted)]}\n",
" )\n",
" return {\n",
" \"past_steps\": (task, agent_response[\"messages\"][-1].content),\n",
" }\n",
Expand Down
67 changes: 32 additions & 35 deletions langgraph/checkpoint/aiosqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,40 @@ class AsyncSqliteSaver(BaseCheckpointSaver, AbstractAsyncContextManager):
Examples:
Usage within a StateGraph:
import asyncio
import aiosqlite
from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
memory = AsyncSqliteSaver.from_conn_string("checkpoints.sqlite")
graph = builder.compile(checkpointer=memory)
coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
asyncio.run(coro) # Output: 2
```pycon
>>> import asyncio
>>> import aiosqlite
>>>
>>> from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver
>>> from langgraph.graph import StateGraph
>>>
>>> builder = StateGraph(int)
>>> builder.add_node("add_one", lambda x: x + 1)
>>> builder.set_entry_point("add_one")
>>> builder.set_finish_point("add_one")
>>> memory = AsyncSqliteSaver.from_conn_string("checkpoints.sqlite")
>>> graph = builder.compile(checkpointer=memory)
>>> coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
>>> asyncio.run(coro)
Output: 2
```
Raw usage:
import asyncio
import aiosqlite
from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver
async def main():
async with aiosqlite.connect("checkpoints.db") as conn:
saver = AsyncSqliteSaver(conn)
config = {"configurable": {"thread_id": "1"}}
checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}}
saved_config = await saver.aput(config, checkpoint)
print(
saved_config
) # Output: {"configurable": {"thread_id": "1", "thread_ts": "2023-05-03T10:00:00Z"}}
asyncio.run(main())
```pycon
>>> import asyncio
>>> import aiosqlite
>>> from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver
>>>
>>> async def main():
>>> async with aiosqlite.connect("checkpoints.db") as conn:
... saver = AsyncSqliteSaver(conn)
... config = {"configurable": {"thread_id": "1"}}
... checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}}
... saved_config = await saver.aput(config, checkpoint)
... print(saved_config)
>>> asyncio.run(main())
{"configurable": {"thread_id": "1", "thread_ts": "2023-05-03T10:00:00Z"}}
```
"""

serde = JsonPlusSerializerCompat()
Expand Down
118 changes: 61 additions & 57 deletions langgraph/checkpoint/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ class JsonPlusSerializerCompat(JsonPlusSerializer):
JsonPlusSerializer behavior is used.
Examples:
import pickle
from langgraph.checkpoint.sqlite import JsonPlusSerializerCompat
serializer = JsonPlusSerializerCompat()
pickled_data = pickle.dumps({"key": "value"})
loaded_data = serializer.loads(pickled_data)
print(loaded_data) # Output: {"key": "value"}
json_data = '{"key": "value"}'.encode("utf-8")
loaded_data = serializer.loads(json_data)
print(loaded_data) # Output: {"key": "value"}
>>> import pickle
>>> from langgraph.checkpoint.sqlite import JsonPlusSerializerCompat
>>>
>>> serializer = JsonPlusSerializerCompat()
>>> pickled_data = pickle.dumps({"key": "value"})
>>> loaded_data = serializer.loads(pickled_data)
>>> print(loaded_data) # Output: {"key": "value"}
>>>
>>> json_data = '{"key": "value"}'.encode("utf-8")
>>> loaded_data = serializer.loads(json_data)
>>> print(loaded_data) # Output: {"key": "value"}
"""

def loads(self, data: bytes) -> Any:
Expand All @@ -64,24 +62,22 @@ class SqliteSaver(BaseCheckpointSaver, AbstractContextManager):
Examples:
import sqlite3
from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
conn = sqlite3.connect("checkpoints.sqlite")
memory = SqliteSaver(conn)
graph = builder.compile(checkpointer=memory)
config = {"configurable": {"thread_id": "1"}}
# checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}}
result = graph.invoke(3, config)
graph.get_state(config)
# Output: StateSnapshot(values=4, next=(), config={'configurable': {'thread_id': '1', 'thread_ts': '2024-05-04T06:32:42.235444+00:00'}}, parent_config=None)
>>> import sqlite3
>>> from langgraph.checkpoint.sqlite import SqliteSaver
>>> from langgraph.graph import StateGraph
>>>
>>> builder = StateGraph(int)
>>> builder.add_node("add_one", lambda x: x + 1)
>>> builder.set_entry_point("add_one")
>>> builder.set_finish_point("add_one")
>>> conn = sqlite3.connect("checkpoints.sqlite")
>>> memory = SqliteSaver(conn)
>>> graph = builder.compile(checkpointer=memory)
>>> config = {"configurable": {"thread_id": "1"}}
>>> graph.get_state(config)
>>> result = graph.invoke(3, config)
>>> graph.get_state(config)
StateSnapshot(values=4, next=(), config={'configurable': {'thread_id': '1', 'thread_ts': '2024-05-04T06:32:42.235444+00:00'}}, parent_config=None)
""" # noqa

serde = JsonPlusSerializerCompat()
Expand Down Expand Up @@ -203,21 +199,22 @@ def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
Examples:
Basic:
config = {"configurable": {"thread_id": "1"}}
checkpoint_tuple = memory.get_tuple(config)
print(checkpoint_tuple) # Output: CheckpointTuple(...)
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
With timestamp:
config = {
"configurable": {
"thread_id": "1",
"thread_ts": "2024-05-04T06:32:42.235444+00:00",
}
}
checkpoint_tuple = memory.get_tuple(config)
print(checkpoint_tuple) # Output: CheckpointTuple(...)
>>> config = {
... "configurable": {
... "thread_id": "1",
... "thread_ts": "2024-05-04T06:32:42.235444+00:00",
... }
... }
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
""" # noqa
with self.cursor(transaction=False) as cur:
if config["configurable"].get("thread_ts"):
Expand Down Expand Up @@ -292,14 +289,19 @@ def list(
Iterator[CheckpointTuple]: An iterator of checkpoint tuples.
Examples:
config = {"configurable": {"thread_id": "1"}}
checkpoints = list(memory.list(config, limit=2))
print(checkpoints) # Output: [CheckpointTuple(...), CheckpointTuple(...)]
config = {"configurable": {"thread_id": "1"}}
before = {"configurable": {"thread_ts": "2024-05-04T06:32:42.235444+00:00"}}
checkpoints = list(memory.list(config, before=before))
print(checkpoints) # Output: [CheckpointTuple(...), ...]
>>> from langgraph.checkpoint.sqlite import SqliteSaver
>>> memory = SqliteSaver.from_conn_string(":memory:")
... # Run a graph, then list the checkpoints
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoints = list(memory.list(config, limit=2))
>>> print(checkpoints)
[CheckpointTuple(...), CheckpointTuple(...)]
>>> config = {"configurable": {"thread_id": "1"}}
>>> before = {"configurable": {"thread_ts": "2024-05-04T06:32:42.235444+00:00"}}
>>> checkpoints = list(memory.list(config, before=before))
>>> print(checkpoints)
[CheckpointTuple(...), ...]
"""
query = (
"SELECT thread_id, thread_ts, parent_ts, checkpoint, metadata FROM checkpoints WHERE thread_id = ? ORDER BY thread_ts DESC"
Expand Down Expand Up @@ -358,12 +360,14 @@ def put(
Examples:
config = {"configurable": {"thread_id": "1"}}
checkpoint = {"ts": "2024-05-04T06:32:42.235444+00:00", "data": {"key": "value"}}
saved_config = memory.put(config, checkpoint)
print(
saved_config
) # Output: {"configurable": {"thread_id": "1", "thread_ts": 2024-05-04T06:32:42.235444+00:00"}}
>>> from langgraph.checkpoint.sqlite import SqliteSaver
>>> memory = SqliteSaver.from_conn_string(":memory:")
... # Run a graph, then list the checkpoints
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoint = {"ts": "2024-05-04T06:32:42.235444+00:00", "data": {"key": "value"}}
>>> saved_config = memory.put(config, checkpoint, {"source": "input", "step": 1, "writes": {"key": "value"}})
>>> print(saved_config)
{"configurable": {"thread_id": "1", "thread_ts": 2024-05-04T06:32:42.235444+00:00"}}
"""
with self.lock, self.cursor() as cur:
cur.execute(
Expand Down

0 comments on commit f335105

Please sign in to comment.