Skip to content

Commit 1e5a612

Browse files
committed
feat(openrouter): add openrouter provider(#33643)
1 parent ab0677c commit 1e5a612

File tree

20 files changed

+3382
-0
lines changed

20 files changed

+3382
-0
lines changed

libs/langchain/langchain_classic/chat_models/base.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def init_chat_model(
127127
- `nvidia` -> [`langchain-nvidia-ai-endpoints`](https://docs.langchain.com/oss/python/integrations/providers/nvidia)
128128
- `xai` -> [`langchain-xai`](https://docs.langchain.com/oss/python/integrations/providers/xai)
129129
- `perplexity` -> [`langchain-perplexity`](https://docs.langchain.com/oss/python/integrations/providers/perplexity)
130+
- `openrouter` -> [`langchain-openrouter`](https://docs.langchain.com/oss/python/integrations/providers/openrouter)
130131
131132
Will attempt to infer `model_provider` from model if not specified. The
132133
following providers will be inferred based on these model prefixes:
@@ -489,6 +490,11 @@ def _init_chat_model_helper(
489490
from langchain_perplexity import ChatPerplexity
490491

491492
return ChatPerplexity(model=model, **kwargs)
493+
if model_provider == "openrouter":
494+
_check_pkg("langchain_openrouter")
495+
from langchain_openrouter import ChatOpenRouter
496+
497+
return ChatOpenRouter(model=model, **kwargs) # type: ignore[call-arg,unused-ignore]
492498
supported = ", ".join(_SUPPORTED_PROVIDERS)
493499
msg = (
494500
f"Unsupported {model_provider=}.\n\nSupported model providers are: {supported}"
@@ -517,6 +523,7 @@ def _init_chat_model_helper(
517523
"ibm",
518524
"xai",
519525
"perplexity",
526+
"openrouter",
520527
}
521528

522529

@@ -541,6 +548,8 @@ def _attempt_infer_model_provider(model_name: str) -> str | None:
541548
return "xai"
542549
if model_name.startswith("sonar"):
543550
return "perplexity"
551+
if "openrouter" in model_name.lower() or "openrouter" in model_name:
552+
return "openrouter"
544553
return None
545554

546555

libs/langchain/langchain_classic/embeddings/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"mistralai": "langchain_mistralai",
1515
"ollama": "langchain_ollama",
1616
"openai": "langchain_openai",
17+
"openrouter": "langchain_openrouter",
1718
}
1819

1920

@@ -159,6 +160,7 @@ def init_embeddings(
159160
- `huggingface` -> [`langchain-huggingface`](https://docs.langchain.com/oss/python/integrations/providers/huggingface)
160161
- `mistraiai` -> [`langchain-mistralai`](https://docs.langchain.com/oss/python/integrations/providers/mistralai)
161162
- `ollama` -> [`langchain-ollama`](https://docs.langchain.com/oss/python/integrations/providers/ollama)
163+
- `openrouter` -> [`langchain-openrouter`](https://docs.langchain.com/oss/python/integrations/providers/openrouter)
162164
163165
**kwargs: Additional model-specific parameters passed to the embedding model.
164166
These vary by provider, see the provider-specific documentation for details.
@@ -231,6 +233,10 @@ def init_embeddings(
231233
from langchain_ollama import OllamaEmbeddings
232234

233235
return OllamaEmbeddings(model=model_name, **kwargs)
236+
if provider == "openrouter":
237+
from langchain_openrouter import OpenRouterEmbeddings
238+
239+
return OpenRouterEmbeddings(model=model_name, **kwargs)
234240
msg = (
235241
f"Provider '{provider}' is not supported.\n"
236242
f"Supported providers and their required packages:\n"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

libs/partners/openrouter/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 LangChain, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libs/partners/openrouter/Makefile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
.PHONY: all format lint test tests integration_tests help extended_tests
2+
3+
# Default target executed when no arguments are given to make.
4+
all: help
5+
6+
.EXPORT_ALL_VARIABLES:
7+
UV_FROZEN = true
8+
9+
# Define a variable for the test file path.
10+
TEST_FILE ?= tests/unit_tests/
11+
integration_test integration_tests: TEST_FILE = tests/integration_tests/
12+
13+
14+
# unit tests are run with the --disable-socket flag to prevent network calls
15+
test tests:
16+
uv run --group test pytest --disable-socket --allow-unix-socket $(TEST_FILE)
17+
18+
test_watch:
19+
uv run --group test ptw --snapshot-update --now . -- -vv $(TEST_FILE)
20+
21+
# integration tests are run without the --disable-socket flag to allow network calls
22+
integration_test integration_tests:
23+
uv run --group test --group test_integration pytest --timeout=30 $(TEST_FILE)
24+
25+
######################
26+
# LINTING AND FORMATTING
27+
######################
28+
29+
# Define a variable for Python and notebook files.
30+
PYTHON_FILES=.
31+
MYPY_CACHE=.mypy_cache
32+
lint format: PYTHON_FILES=.
33+
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/partners/deepseek --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
34+
lint_package: PYTHON_FILES=langchain_deepseek
35+
lint_tests: PYTHON_FILES=tests
36+
lint_tests: MYPY_CACHE=.mypy_cache_test
37+
38+
lint lint_diff lint_package lint_tests:
39+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff check $(PYTHON_FILES)
40+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff format $(PYTHON_FILES) --diff
41+
[ "$(PYTHON_FILES)" = "" ] || mkdir -p $(MYPY_CACHE) && uv run --all-groups mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)
42+
43+
format format_diff:
44+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff format $(PYTHON_FILES)
45+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff check --fix $(PYTHON_FILES)
46+
47+
check_imports: $(shell find langchain_deepseek -name '*.py')
48+
uv run --all-groups python ./scripts/check_imports.py $^
49+
50+
######################
51+
# HELP
52+
######################
53+
54+
help:
55+
@echo '----'
56+
@echo 'check_imports - check imports'
57+
@echo 'format - run code formatters'
58+
@echo 'lint - run linters'
59+
@echo 'test - run unit tests'
60+
@echo 'tests - run unit tests'
61+
@echo 'test TEST_FILE=<test_file> - run all tests in file'

libs/partners/openrouter/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# langchain-openrouter
2+
3+
[![PyPI - Version](https://img.shields.io/pypi/v/langchain-openrouter?label=%20)](https://pypi.org/project/langchain-openrouter/#history)
4+
[![PyPI - License](https://img.shields.io/pypi/l/langchain-openrouter)](https://opensource.org/licenses/MIT)
5+
[![PyPI - Downloads](https://img.shields.io/pepy/dt/langchain-openrouter)](https://pypistats.org/packages/langchain-openrouter)
6+
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/langchainai.svg?style=social&label=Follow%20%40LangChainAI)](https://twitter.com/langchainai)
7+
8+
Looking for the JS/TS version? Check out [LangChain.js](https://github.com/langchain-ai/langchainjs).
9+
10+
## Quick Install
11+
12+
```bash
13+
pip install langchain-openrouter
14+
```
15+
16+
## 🤔 What is this?
17+
18+
This package contains the LangChain integration with OpenRouter.
19+
20+
## 📖 Documentation
21+
22+
View the [documentation](https://docs.langchain.com/oss/python/integrations/providers/openrouter) for more details.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""LangChain OpenRouter integration."""
2+
3+
from importlib import metadata
4+
5+
from langchain_openrouter.chat_models import ChatOpenRouter
6+
from langchain_openrouter.embeddings import OpenRouterEmbeddings
7+
8+
try:
9+
__version__ = metadata.version(__package__)
10+
except metadata.PackageNotFoundError:
11+
# Case where package metadata is not available.
12+
__version__ = ""
13+
del metadata # optional, avoids polluting the results of dir(__package__)
14+
15+
__all__ = [
16+
"ChatOpenRouter",
17+
"OpenRouterEmbeddings",
18+
"__version__",
19+
]

0 commit comments

Comments
 (0)