mirror of
https://github.com/langchain-ai/langgraph-codeact.git
synced 2026-07-09 05:35:41 -04:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 39ef3ce44a |
@@ -1,4 +1,3 @@
|
||||
# pip install langgraph-codeact "langchain[anthropic]"
|
||||
import asyncio
|
||||
import inspect
|
||||
from typing import Any
|
||||
@@ -10,13 +9,21 @@ from langgraph.checkpoint.memory import MemorySaver
|
||||
from langgraph_codeact import EvalCoroutine, create_codeact
|
||||
|
||||
|
||||
def create_pyodide_eval_fn(sandbox: PyodideSandbox) -> EvalCoroutine:
|
||||
def create_pyodide_eval_fn(
|
||||
sandbox_dir: str = "./sessions", session_id: str | None = None
|
||||
) -> EvalCoroutine:
|
||||
"""Create an eval_fn that uses PyodideSandbox.
|
||||
"""
|
||||
|
||||
async def async_eval_fn(
|
||||
code: str, _locals: dict[str, Any]
|
||||
) -> tuple[str, dict[str, Any]]:
|
||||
Args:
|
||||
sandbox_dir: Directory to store session files
|
||||
session_id: ID of the session to use
|
||||
|
||||
Returns:
|
||||
A function that evaluates code using PyodideSandbox
|
||||
"""
|
||||
sandbox = PyodideSandbox(sandbox_dir, allow_net=True)
|
||||
|
||||
async def async_eval_fn(code: str, _locals: dict[str, Any]) -> tuple[str, dict[str, Any]]:
|
||||
# Create a wrapper function that will execute the code and return locals
|
||||
wrapper_code = f"""
|
||||
def execute():
|
||||
@@ -43,6 +50,7 @@ execute()
|
||||
# Execute the code and get the result
|
||||
response = await sandbox.execute(
|
||||
code=context_setup + "\n\n" + wrapper_code,
|
||||
session_id=session_id,
|
||||
)
|
||||
|
||||
# Check if execution was successful
|
||||
@@ -51,9 +59,7 @@ execute()
|
||||
|
||||
# Get the output from stdout
|
||||
output = (
|
||||
response.stdout
|
||||
if response.stdout
|
||||
else "<Code ran, no output printed to stdout>"
|
||||
response.stdout if response.stdout else "<Code ran, no output printed to stdout>"
|
||||
)
|
||||
result = response.result
|
||||
|
||||
@@ -63,9 +69,7 @@ execute()
|
||||
|
||||
# Get the new variables by comparing with original locals
|
||||
new_vars = {
|
||||
k: v
|
||||
for k, v in result.items()
|
||||
if k not in _locals and not k.startswith("_")
|
||||
k: v for k, v in result.items() if k not in _locals and not k.startswith("_")
|
||||
}
|
||||
return output, new_vars
|
||||
|
||||
@@ -150,8 +154,7 @@ tools = [
|
||||
|
||||
model = init_chat_model("claude-3-7-sonnet-latest", model_provider="anthropic")
|
||||
|
||||
sandbox = PyodideSandbox(allow_net=True)
|
||||
eval_fn = create_pyodide_eval_fn(sandbox)
|
||||
eval_fn = create_pyodide_eval_fn()
|
||||
code_act = create_codeact(model, tools, eval_fn)
|
||||
agent = code_act.compile(checkpointer=MemorySaver())
|
||||
|
||||
|
||||
+2
-2
@@ -9,7 +9,7 @@ license = {text = "MIT"}
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10,<4.0"
|
||||
dependencies = [
|
||||
"langgraph>=0.4.5"
|
||||
"langgraph (>=0.3.5,<0.4.0)"
|
||||
]
|
||||
|
||||
[build-system]
|
||||
@@ -20,7 +20,7 @@ build-backend = "pdm.backend"
|
||||
dev = [
|
||||
"langchain-anthropic>=0.3.9,<0.4.0",
|
||||
"langchain>=0.3.20,<0.4.0",
|
||||
"langchain-sandbox>=0.0.5,<0.1.0"
|
||||
"langchain-sandbox>=0.0.3,<0.1.0"
|
||||
]
|
||||
test = [
|
||||
"pytest>=8.0.0",
|
||||
|
||||
@@ -432,7 +432,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langchain-core"
|
||||
version = "0.3.60"
|
||||
version = "0.3.48"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "jsonpatch" },
|
||||
@@ -443,22 +443,18 @@ dependencies = [
|
||||
{ name = "tenacity" },
|
||||
{ name = "typing-extensions" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/5b/75/95129aaada92980a002a31e002610a80af3c8967ae7884710372e89cdde0/langchain_core-0.3.60.tar.gz", hash = "sha256:63dd1bdf7939816115399522661ca85a2f3686a61440f2f46ebd86d1b028595b", size = 557456 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/13/f3/7dffb16b9245d333d5af381dd3b0130a0d4c652c22e386d0819e773c90f3/langchain_core-0.3.48.tar.gz", hash = "sha256:be4b2fe36d8a11fb4b6b13e0808b12aea9f25e345624ffafe1d606afb6059f21", size = 534337 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/2d/bc/344f5b11fdfe0e27f7064d2e829921a791461dc32e5ed285fe6325518c26/langchain_core-0.3.60-py3-none-any.whl", hash = "sha256:2ccdf06b12e699b1b0962bc02837056c075b4981c3d13f82a4d4c30bb22ea3dc", size = 437890 },
|
||||
{ url = "https://files.pythonhosted.org/packages/66/06/4ddafa246b4f1b2017918237b681e884fd02d34b0cb3dcc802105b53872d/langchain_core-0.3.48-py3-none-any.whl", hash = "sha256:21e4fe84262b9c7ad8aefe7816439ede130893f8a64b8c965cd9695c2be91c73", size = 418651 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "langchain-sandbox"
|
||||
version = "0.0.5"
|
||||
version = "0.0.3"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
{ name = "langgraph" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/c4/51/4b1a276d145b35653ffad06393d0b2f09d0cc74f103d6f9b71e47b477280/langchain_sandbox-0.0.5.tar.gz", hash = "sha256:3cad5a33033268238ac25e9499bc0ee7ed0500940601ff0629e2b75fb54d5ebd", size = 72853 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/5e/bd/31183b7c7d3f22ecbd0598bac764f2d5a79509aebfefc91b0f0a3b816357/langchain_sandbox-0.0.3.tar.gz", hash = "sha256:4736d5e5671bbf5ec93ee5c9b26ab1abc2b6859e75cea3b390de8948a8652a57", size = 22430 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/56/9a/bd80e7fbf43871e385d67965dc8f4ae71fb80259e5bcca2cfcb6b0000c19/langchain_sandbox-0.0.5-py3-none-any.whl", hash = "sha256:7117c6af9fec634582bc0784b418b2e2c54bf51a9accf000b565e09b49a8532d", size = 7298 },
|
||||
{ url = "https://files.pythonhosted.org/packages/12/22/2f4154ca10f49bfde86124ced7fab304972cb39586d03eb3785b17ffb25f/langchain_sandbox-0.0.3-py3-none-any.whl", hash = "sha256:03068c117a2e125460c88b8c0aded49644e87646c154b51412fd64d05159a505", size = 5546 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -475,32 +471,31 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph"
|
||||
version = "0.4.5"
|
||||
version = "0.3.20"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
{ name = "langgraph-checkpoint" },
|
||||
{ name = "langgraph-prebuilt" },
|
||||
{ name = "langgraph-sdk" },
|
||||
{ name = "pydantic" },
|
||||
{ name = "xxhash" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/85/b6/eafa5f260e10e60c969def1cb26740aa9e57dc49dd1ea133ccbaaf09cf63/langgraph-0.4.5.tar.gz", hash = "sha256:08a8c6577b09cda4e0c16712e762927f00930dabbc7fe235562985ad85891349", size = 128672 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/40/4f/3026a775bf6bd03ace19bb25c1b91b0a77f46a71b4486bdb61e48aada0b4/langgraph-0.3.20.tar.gz", hash = "sha256:c980ec26aae3b52f4d75e7b852c22360c93c803e22297e85963c84bd53100020", size = 116501 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/f2/2c/052922a444e32371f08eff4a8095e82cb74ed8a80823a9168079ce27211c/langgraph-0.4.5-py3-none-any.whl", hash = "sha256:73f36caae55137c2bdb2a6c59661f0ae29c1516a0d1f4ad4975ad3862865a979", size = 155297 },
|
||||
{ url = "https://files.pythonhosted.org/packages/75/3c/29ad4508d90bb1d7bd80ca51627bc12d58e866c3f5dcf5d03f8a81afb704/langgraph-0.3.20-py3-none-any.whl", hash = "sha256:b700138e4b75853a76052b06d3f00bbb2336d0cb88482700b9f4974a76e030cd", size = 137937 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-checkpoint"
|
||||
version = "2.0.26"
|
||||
version = "2.0.23"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
{ name = "ormsgpack" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/c5/61/e2518ac9216a4e9f4efda3ac61595e3c9e9ac00833141c9688e8d56bd7eb/langgraph_checkpoint-2.0.26.tar.gz", hash = "sha256:2b800195532d5efb079db9754f037281225ae175f7a395523f4bf41223cbc9d6", size = 37874 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/a3/86/cca9b5d05c0ea64b03cd9e845da6b37f83a22ea13abb7b328ca5dfe48474/langgraph_checkpoint-2.0.23.tar.gz", hash = "sha256:38bd1fe451b569b773fef6e3daecdeb85f3deac2d94f7551bfd20f1818042c8a", size = 37268 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/38/48/d7cec540a3011b3207470bb07294a399e3b94b2e8a602e38cb007ce5bc10/langgraph_checkpoint-2.0.26-py3-none-any.whl", hash = "sha256:ad4907858ed320a208e14ac037e4b9244ec1cb5aa54570518166ae8b25752cec", size = 44247 },
|
||||
{ url = "https://files.pythonhosted.org/packages/ec/8d/e23bc15809c4a29e83efab34e7ff1ffb6dadac26b87aca98242ac6033934/langgraph_checkpoint-2.0.23-py3-none-any.whl", hash = "sha256:e54d070124f685eab095bd87e4df35dc5eca11d1e28553d5803c28c5f571b4e0", size = 41941 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -526,13 +521,13 @@ test = [
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [{ name = "langgraph", specifier = ">=0.4.5" }]
|
||||
requires-dist = [{ name = "langgraph", specifier = ">=0.3.5,<0.4.0" }]
|
||||
|
||||
[package.metadata.requires-dev]
|
||||
dev = [
|
||||
{ name = "langchain", specifier = ">=0.3.20,<0.4.0" },
|
||||
{ name = "langchain-anthropic", specifier = ">=0.3.9,<0.4.0" },
|
||||
{ name = "langchain-sandbox", specifier = ">=0.0.5,<0.1.0" },
|
||||
{ name = "langchain-sandbox", specifier = ">=0.0.3,<0.1.0" },
|
||||
]
|
||||
test = [
|
||||
{ name = "mypy", specifier = ">=1.8.0" },
|
||||
@@ -544,15 +539,15 @@ test = [
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-prebuilt"
|
||||
version = "0.1.8"
|
||||
version = "0.1.7"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "langchain-core" },
|
||||
{ name = "langgraph-checkpoint" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/57/30/f31f0e076c37d097b53e4cff5d479a3686e1991f6c86a1a4727d5d1f5489/langgraph_prebuilt-0.1.8.tar.gz", hash = "sha256:4de7659151829b2b955b6798df6800e580e617782c15c2c5b29b139697491831", size = 24543 }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/fb/ed/e05dc6561aae82b2ec14b0a663f9bfd67a1da169d8895acecbb149cf9898/langgraph_prebuilt-0.1.7.tar.gz", hash = "sha256:5b086fad2ebfabe743bfbfa1e0248ce0d7d10eb0cc939b8bc7a9b41360b9d518", size = 23642 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/36/72/9e092665502f8f52f2708065ed14fbbba3f95d1a1b65d62049b0c5fcdf00/langgraph_prebuilt-0.1.8-py3-none-any.whl", hash = "sha256:ae97b828ae00be2cefec503423aa782e1bff165e9b94592e224da132f2526968", size = 25903 },
|
||||
{ url = "https://files.pythonhosted.org/packages/52/c2/e88798e0c698ae92f30b41966a781e1b28fa48ae4825462062722d1942bb/langgraph_prebuilt-0.1.7-py3-none-any.whl", hash = "sha256:35eff90fb86edd0b026a6744942bc59bad4a2d82a482aa860ff8d081af423956", size = 25021 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
Reference in New Issue
Block a user