Skip to content

Commit 3814bd1

Browse files
sydney-runkleeyurtsevccurme
authored
partners: Add Perplexity Chat Integration (#30618)
Perplexity's importance in the space has been growing, so we think it's time to add an official integration! Note: following the release of `langchain-perplexity` to `pypi`, we should be able to add `perplexity` as an extra in `libs/langchain/pyproject.toml`, but we're blocked by a circular import for now. --------- Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Chester Curme <chester.curme@gmail.com>
1 parent 87c02a1 commit 3814bd1

28 files changed

+2595
-2
lines changed

.github/workflows/_integration_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ jobs:
7676
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
7777
UPSTAGE_API_KEY: ${{ secrets.UPSTAGE_API_KEY }}
7878
XAI_API_KEY: ${{ secrets.XAI_API_KEY }}
79+
PPLX_API_KEY: ${{ secrets.PPLX_API_KEY }}
7980
run: |
8081
make integration_tests
8182

.github/workflows/_release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ jobs:
327327
FIREWORKS_API_KEY: ${{ secrets.FIREWORKS_API_KEY }}
328328
XAI_API_KEY: ${{ secrets.XAI_API_KEY }}
329329
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
330+
PPLX_API_KEY: ${{ secrets.PPLX_API_KEY }}
330331
run: make integration_tests
331332
working-directory: ${{ inputs.working-directory }}
332333

.github/workflows/scheduled_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ jobs:
145145
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
146146
GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }}
147147
GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }}
148+
PPLX_API_KEY: ${{ secrets.PPLX_API_KEY }}
148149
run: |
149150
cd langchain/${{ matrix.working-directory }}
150151
make integration_tests

docs/src/theme/ChatModelTabs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ ${llmVarName} = ChatWatsonx(
218218
apiKeyName: "XAI_API_KEY",
219219
packageName: "langchain-xai",
220220
},
221+
{
222+
value: "perplexity",
223+
label: "Perplexity",
224+
model: "llama-3.1-sonar-small-128k-online",
225+
apiKeyName: "PPLX_API_KEY",
226+
packageName: "langchain-perplexity",
227+
}
221228
].map((item) => ({
222229
...item,
223230
...overrideParams?.[item.value],

docs/src/theme/FeatureTables.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ const FEATURE_TABLES = {
237237
"local": false,
238238
"apiLink": "https://python.langchain.com/api_reference/xai/chat_models/langchain_xai.chat_models.ChatXAI.html"
239239
},
240+
{
241+
"name": "ChatPerplexity",
242+
"package": "langchain-perplexity",
243+
"link": "perplexity",
244+
"structured_output": true,
245+
"tool_calling": false,
246+
"json_mode": true,
247+
"multimodal": true,
248+
"local": false,
249+
"apiLink": "https://python.langchain.com/api_reference/perplexity/chat_models/langchain_perplexity.chat_models.ChatPerplexity.html"
250+
}
240251
],
241252
},
242253
llms: {

libs/core/langchain_core/load/load.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"langchain_fireworks",
2929
"langchain_xai",
3030
"langchain_sambanova",
31+
"langchain_perplexity",
3132
]
3233
# Namespaces for which only deserializing via the SERIALIZABLE_MAPPING is allowed.
3334
# Load by path is not allowed.

libs/langchain/langchain/chat_models/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def init_chat_model(
125125
- 'ibm' -> langchain-ibm
126126
- 'nvidia' -> langchain-nvidia-ai-endpoints
127127
- 'xai' -> langchain-xai
128+
- 'perplexity' -> langchain-perplexity
128129
129130
Will attempt to infer model_provider from model if not specified. The
130131
following providers will be inferred based on these model prefixes:
@@ -453,6 +454,11 @@ def _init_chat_model_helper(
453454
from langchain_xai import ChatXAI
454455

455456
return ChatXAI(model=model, **kwargs)
457+
elif model_provider == "perplexity":
458+
_check_pkg("langchain_perplexity")
459+
from langchain_perplexity import ChatPerplexity
460+
461+
return ChatPerplexity(model=model, **kwargs)
456462
else:
457463
supported = ", ".join(_SUPPORTED_PROVIDERS)
458464
raise ValueError(
@@ -481,6 +487,7 @@ def _init_chat_model_helper(
481487
"deepseek",
482488
"ibm",
483489
"xai",
490+
"perplexity",
484491
}
485492

486493

libs/partners/perplexity/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

libs/partners/perplexity/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) 2023 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/perplexity/Makefile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.PHONY: all format lint test tests integration_tests docker_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+
12+
integration_test integration_tests: TEST_FILE=tests/integration_tests/
13+
14+
test tests:
15+
uv run --group test pytest --disable-socket --allow-unix-socket $(TEST_FILE)
16+
17+
test_watch:
18+
uv run --group test ptw --snapshot-update --now . -- -vv $(TEST_FILE)
19+
20+
integration_test integration_tests:
21+
uv run --group test --group test_integration pytest $(TEST_FILE)
22+
23+
######################
24+
# LINTING AND FORMATTING
25+
######################
26+
27+
# Define a variable for Python and notebook files.
28+
PYTHON_FILES=.
29+
MYPY_CACHE=.mypy_cache
30+
lint format: PYTHON_FILES=.
31+
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/partners/perplexity --name-only --diff-filter=d master | grep -E '\.py$$|\.ipynb$$')
32+
lint_package: PYTHON_FILES=langchain_perplexity
33+
lint_tests: PYTHON_FILES=tests
34+
lint_tests: MYPY_CACHE=.mypy_cache_test
35+
36+
lint lint_diff lint_package lint_tests:
37+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff check $(PYTHON_FILES)
38+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff format $(PYTHON_FILES) --diff
39+
[ "$(PYTHON_FILES)" = "" ] || mkdir -p $(MYPY_CACHE) && uv run --all-groups mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)
40+
41+
format format_diff:
42+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff format $(PYTHON_FILES)
43+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff check --select I --fix $(PYTHON_FILES)
44+
45+
spell_check:
46+
uv run --all-groups codespell --toml pyproject.toml
47+
48+
spell_fix:
49+
uv run --all-groups codespell --toml pyproject.toml -w
50+
51+
check_imports: $(shell find langchain_perplexity -name '*.py')
52+
uv run --all-groups python ./scripts/check_imports.py $^
53+
54+
######################
55+
# HELP
56+
######################
57+
58+
help:
59+
@echo '----'
60+
@echo 'check_imports - check imports'
61+
@echo 'format - run code formatters'
62+
@echo 'lint - run linters'
63+
@echo 'test - run unit tests'
64+
@echo 'tests - run unit tests'
65+
@echo 'test TEST_FILE=<test_file> - run all tests in file'

libs/partners/perplexity/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# langchain-perplexity
2+
3+
This package contains the LangChain integration with Perplexity.
4+
5+
## Installation
6+
7+
```bash
8+
pip install -U langchain-perplexity
9+
```
10+
11+
And you should [configure your perplexity credentials](https://docs.perplexity.ai/guides/getting-started)
12+
and then set the `PPLX_API_KEY` environment variable.
13+
14+
## Usage
15+
16+
This package contains the `ChatPerplexity` class, which is the recommended way to interface with Perplexity chat models.
17+
18+
```python
19+
import getpass
20+
import os
21+
22+
if not os.environ.get("PPLX_API_KEY"):
23+
os.environ["PPLX_API_KEY"] = getpass.getpass("Enter API key for Perplexity: ")
24+
25+
from langchain.chat_models import init_chat_model
26+
27+
llm = init_chat_model("llama-3.1-sonar-small-128k-online", model_provider="perplexity")
28+
llm.invoke("Hello, world!")
29+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""This package provides the Perplexity integration for LangChain."""
2+
3+
from langchain_perplexity.chat_models import ChatPerplexity
4+
5+
__all__ = ["ChatPerplexity"]

0 commit comments

Comments
 (0)