-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Add LangChain middleware integration #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dagardner-nv
wants to merge
51
commits into
NVIDIA:main
Choose a base branch
from
dagardner-nv:david-lcda
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
30cb595
Add deepagents submodule
dagardner-nv 4c491fc
Bump version of deepagents, adding the checkout to gitignore. TODO: t…
dagardner-nv b3e22a6
Bump the version of langchain-core
dagardner-nv 9acab27
Update comment
dagardner-nv 5e65255
Update patch for langchain-core v1.3.3
dagardner-nv 19e154a
Bump langgraph to v1.1.10
dagardner-nv 98623b5
Bump version of langchain-nvidia from pre-release commit to v1.2.1
dagardner-nv 56d420b
Bump langgraph version
dagardner-nv 617b6e8
Pin against release tag for langgraph-prebuilt v1.0.13
dagardner-nv f293efc
Merge branch 'main' of github.com:NVIDIA/NeMo-Flow into david-lcda
dagardner-nv afb3fd6
WIP: langchain-core middleware, currently not working
dagardner-nv bdb40e9
WIP
dagardner-nv 8b8b048
Ignore vscode ssettings
dagardner-nv eeb2b03
WIP
dagardner-nv 76c6e7d
Widen the version dependencies for langchain
dagardner-nv 96ebf5c
Move the run_sync method into the python wrapper for nemo_flow as lik…
dagardner-nv 3cd1741
WIP
dagardner-nv a92e263
Slim down uneeded code
dagardner-nv 6553cc2
Slim down
dagardner-nv 8684819
Slim down
dagardner-nv 88d69f8
Merge branch 'main' of github.com:NVIDIA/NeMo-Flow into david-lcda
dagardner-nv fea8c11
WIP
dagardner-nv 0f91ab0
WIP
dagardner-nv 44afddd
Formatting
dagardner-nv 3a1b175
Set parent scope
dagardner-nv ce88f6e
Formatting
dagardner-nv 07591fa
Add utils to __all__
dagardner-nv 0a26572
Rework infer_codec_from_model
dagardner-nv b38f3ba
Remove tools from llm payloads, this wasn't working mostly because we…
dagardner-nv b744254
Remove unused code, consolidate redundant code
dagardner-nv 10767ce
Reduce imports
dagardner-nv 7d075e5
Cleanup imports
dagardner-nv 702d003
Formatting
dagardner-nv a88e76a
Add langchain/langgraph deps to ignore list
dagardner-nv 677643d
Move things up one dir level
dagardner-nv de4c16e
Adding tests
dagardner-nv ad918fc
Set workspace dep for nemo-flow
dagardner-nv d528085
Fetch the version from the Cargo.toml file, we can't use maturin as t…
dagardner-nv 36c3f53
Ignore ty unresolved-import check
dagardner-nv 2d0ef6d
Explicitly import from utils
dagardner-nv 430e2e7
Fix ty ignores
dagardner-nv bc97266
Add tests to CI
dagardner-nv e89169d
Add a readme to the integration
dagardner-nv 76d237d
Update docs
dagardner-nv a62d92e
Fix spelling error
dagardner-nv b250da4
Mark llm_execute and llm_stream_execute as private, per WK feedback
dagardner-nv 28f76ac
Infer the version based on the git tag
dagardner-nv e87e188
Linting fixes
dagardner-nv fbf672c
Create a single ThreadPoolExecutor instance
dagardner-nv a28fc87
Fix tests
dagardner-nv e91f84c
Update python/nemo_flow/utils.py
dagardner-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| --> | ||
|
|
||
| # NeMo Flow LangChain Integration | ||
|
|
||
| This directory contains the `langchain_nemo_flow` package which provides observability integration for LangChain. | ||
|
|
||
| The intent of this project is to enable as much NeMo Flow functionality as possible using public LangChain APIs without requiring changes to LangChain itself. | ||
|
|
||
| For an alternate approach refer to [the patch-based integration in `third_party/langchain`](../../third_party/README-langchain.md). | ||
|
|
||
| ## Setup | ||
|
|
||
| ```bash | ||
| uv venv .venv | ||
| . .venv/bin/activate | ||
| uv pip install -e . | ||
| uv pip install -e integrations/langchain | ||
| ``` | ||
|
|
||
| Optionally install the langchain-nvidia-ai-endpoints package. | ||
|
|
||
| ```bash | ||
| uv pip install langchain-nvidia-ai-endpoints | ||
| ``` | ||
|
|
||
| ## Usage Example | ||
|
|
||
| ```python | ||
| import asyncio | ||
|
|
||
| import nemo_flow | ||
| from langchain.agents import create_agent | ||
| from langchain_core.tools import tool | ||
| from langchain_nemo_flow import NemoFlowCallbackHandler, NemoFlowMiddleware | ||
|
|
||
| @tool | ||
| def get_weather(location: str) -> str: | ||
| """Get the current weather for a location.""" | ||
| return f"The weather in {location} is sunny and 72 degrees." | ||
|
|
||
| agent = create_agent( | ||
| model="nvidia:nvidia/nemotron-3-nano-30b-a3b", | ||
| tools=[get_weather], | ||
| middleware=[NemoFlowMiddleware()], | ||
| system_prompt="Use tools when they are relevant. Keep the final answer brief.", | ||
| ) | ||
|
|
||
| input_payload = { | ||
| "messages": [ | ||
| { | ||
| "role": "user", | ||
| "content": "What is the weather in San Francisco?", | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| with nemo_flow.scope.scope("langchain-request", nemo_flow.ScopeType.Agent): | ||
| result = asyncio.run(agent.ainvoke(input_payload, config={"callbacks": [NemoFlowCallbackHandler()]})) | ||
|
|
||
| final_message = result["messages"][-1] | ||
| print(f"Final response: {final_message.content}") | ||
| ``` | ||
|
|
||
| ## Validation | ||
|
|
||
| Run tests for the `langchain_nemo_flow` package to validate the integration: | ||
|
|
||
| ```bash | ||
| cd integrations/langchain | ||
| uv run --group test pytest -q | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| [build-system] | ||
| build-backend = "setuptools.build_meta" | ||
| requires = ["setuptools>=64", "setuptools-scm>=8", "setuptools_dynamic_dependencies>=1.0.0"] | ||
|
|
||
| [tool.setuptools.packages.find] | ||
| where = ["src"] | ||
|
|
||
| [tool.setuptools_scm] | ||
| git_describe_command = "git describe --long --first-parent" | ||
| root = "../.." | ||
|
|
||
| [project] | ||
| name = "langchain-nemo-flow" | ||
| dynamic = ["version", "dependencies"] | ||
| description = "NeMo Flow middleware and callback integrations for LangChain." | ||
| requires-python = ">=3.11" | ||
| license = "Apache-2.0" | ||
| authors = [ | ||
| { name = "NVIDIA Corporation & Affiliates" }, | ||
| ] | ||
| keywords = [ | ||
| "agents", | ||
| "langchain", | ||
| "middleware", | ||
| "nemo-flow", | ||
| "observability", | ||
| ] | ||
| classifiers = [ | ||
| "Development Status :: 4 - Beta", | ||
| "Intended Audience :: Developers", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| ] | ||
|
|
||
| [tool.setuptools_dynamic_dependencies] | ||
| dependencies = [ | ||
| # Minimum version to support they hooks we are using | ||
| "langchain>=1.2.9,<2.0.0", | ||
| # Allow the langchain to dictate the version of langchain-core, not strictly needed | ||
| # but we do import from langchain_core | ||
| "langchain-core", | ||
| "nemo-flow == {version}", | ||
| ] | ||
|
dagardner-nv marked this conversation as resolved.
|
||
|
|
||
| [dependency-groups] | ||
| test = [ | ||
| "pytest>=8", | ||
| ] | ||
|
|
||
| [project.urls] | ||
| Documentation = "https://nvidia.github.io/NeMo-Flow/" | ||
| Homepage = "https://github.com/NVIDIA/NeMo-Flow" | ||
| Issues = "https://github.com/NVIDIA/NeMo-Flow/issues" | ||
| Repository = "https://github.com/NVIDIA/NeMo-Flow" | ||
|
|
||
| [tool.ruff] | ||
| line-length = 120 | ||
| target-version = "py311" | ||
|
|
||
| [tool.ruff.format] | ||
| quote-style = "double" | ||
|
|
||
| [tool.ruff.lint] | ||
| select = ["E", "F", "W", "I"] | ||
|
|
||
| [tool.uv] | ||
| default-groups = ["test"] | ||
|
|
||
| [tool.uv.sources] | ||
| nemo-flow = { path = "../..", editable = true } | ||
12 changes: 12 additions & 0 deletions
12
integrations/langchain/src/langchain_nemo_flow/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """NeMo Flow integrations for LangChain.""" | ||
|
|
||
| from langchain_nemo_flow.callbacks import NemoFlowCallbackHandler | ||
| from langchain_nemo_flow.middleware import NemoFlowMiddleware | ||
|
|
||
| __all__ = [ | ||
| "NemoFlowCallbackHandler", | ||
| "NemoFlowMiddleware", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.