Skip to content

Improvements to ChatPerplexity Integration #30802

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
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

jamesliounis
Copy link

@jamesliounis jamesliounis commented Apr 12, 2025

TL;DR:

  1. Add enable_search_classifier parameter

  2. Update model default to sonar-pro

  3. Modify example notebook to reflect best practices

  4. Updates the Perplexity API wrapper (ChatPerplexity) by adding a new parameter, enable_search_classifier, with a default value of True. This enables the use of a classifier to determine if search is necessary for a given API call. Additionally, the default model name has been updated from the deprecated "llama-3.1-sonar-small-128k-online" to "sonar-pro", keeping the integration current with the Perplexity API. The change allows users to both modify this new flag and rely on sensible defaults while preserving the flexibility of passing any additional parameters via model_kwargs.

  5. Updates example notebook with best practices around using Perplexity and correct links to documentation.

Dependencies:
No additional dependencies are introduced with this change.

Twitter handle:
(@LiounisJames, @PPLXDevs)

Additional steps performed:
• Ran make format, make lint, and make test to ensure that the changes adhere to the code quality and testing guidelines.

Thank you for reviewing this contribution!

Copy link

vercel bot commented Apr 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchain ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 22, 2025 7:56pm

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. community Related to langchain-community labels Apr 12, 2025
@jamesliounis jamesliounis changed the title community: add enable_search_classifier parameter and update model default to sonar-pro Add enable_search_classifier parameter | update model default to sonar-pro | modify example notebook to reflect best practices Apr 12, 2025
@jamesliounis jamesliounis changed the title Add enable_search_classifier parameter | update model default to sonar-pro | modify example notebook to reflect best practices Perplexity Integration: Add enable_search_classifier parameter | update model default to sonar-pro | modify example notebook to reflect best practices Apr 12, 2025
@jamesliounis jamesliounis changed the title Perplexity Integration: Add enable_search_classifier parameter | update model default to sonar-pro | modify example notebook to reflect best practices Improvements to ChatPerplexity Integration Apr 12, 2025
Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Are we still intending to add the tools to langchain-community? Should we remove those?

@ccurme ccurme self-assigned this Apr 13, 2025
@jamesliounis
Copy link
Author

@ccurme Removed the tool integration. Thanks for pointing this out.

@james-pplx
Copy link

@ccurme anything else I can do to help with this?

@@ -168,6 +168,8 @@ class StructuredOutput(BaseModel):
"""Whether to stream the results or not."""
max_tokens: Optional[int] = None
"""Maximum number of tokens to generate."""
enable_search_classifier: bool = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run this I get

TypeError: Completions.create() got an unexpected keyword argument 'enable_search_classifier'

Does this need to be passed in through extra_body, as with other Perplexity-specific params not supported by the OpenAI SDK? (see examples in docs).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct, it needs to be passed in the extra body. Forgot to push the commit. Fixing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed these comments in this commit. I removed the enable_search_classifier param, it's better to add it to the Perplexity documentation directly and let users pass it themselves if needed.

@@ -147,9 +147,9 @@ class StructuredOutput(BaseModel):
""" # noqa: E501

client: Any = None #: :meta private:
model: str = "llama-3.1-sonar-small-128k-online"
model: str = "sonar-pro"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal to change default models and temperatures, as it changes behavior (and costs?) out from under users without warning.

Ideally we'd actually remove these defaults and just rely on whatever defaults Perplexity uses server-side. I'd be supportive of doing this in a v0.2 release.

Copy link

@james-pplx james-pplx Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the default model because the one that was there (llama-3.1-sonar-small-128k-online) has been deprecated, we do not support it anymore. sonar-pro is our best performing model for a regular web search so it will most likely result in higher quality outputs, but if cost is a concern then we can use our cheapest alternative which is sonar. These are all of our currently supported models.

I changed the inference parameters because the ones that I added are our defaults. If we remove them altogether this would then use what we have server-side which is also ok (exactly the same thing). But specifying a model is required. Additional info here in our API ref.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's preferable to have the following:

  • model has no default, users must pass it in on init
  • temperature defaults to null and is only included in request payloads if it is explicitly specified.

That is, we mirror usage of the API.

I don't feel strongly about this if you'd prefer to maintain defaults in the LangChain integration. In either case, if we are changing the defaults we probably should bump the minor version from 0.1 to 0.2, though.

Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to get this in but there are still some issues:

  1. Code does not run. Please try running tests locally:
    • make test will run unit tests
    • PPLX_API_KEY=pplx-... make integration_test will run integration tests (with real HTTP requests)
  2. We are (inadvertently?) undoing multiple PRs here (see comments)
    • Run make format and make lint locally to run the auto-formatter and linter, which will catch some of the errors.
  3. Docs need to be updated to reflect most recent changes.

Comment on lines +390 to +391
additional_kwargs = {"citations": response.citations}
for attr in ["images", "related_questions"]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we un-doing #30805?

@@ -39,8 +50,8 @@
from typing_extensions import Self

_BM = TypeVar("_BM", bound=BaseModel)
_DictOrPydanticClass = Union[dict[str, Any], type[_BM], type]
_DictOrPydantic = Union[dict, _BM]
_DictOrPydanticClass = Union[Dict[str, Any], Type[_BM], Type]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we un-doing #30781?

@@ -147,9 +147,9 @@ class StructuredOutput(BaseModel):
""" # noqa: E501

client: Any = None #: :meta private:
model: str = "llama-3.1-sonar-small-128k-online"
model: str = "sonar-pro"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's preferable to have the following:

  • model has no default, users must pass it in on init
  • temperature defaults to null and is only included in request payloads if it is explicitly specified.

That is, we mirror usage of the API.

I don't feel strongly about this if you'd prefer to maintain defaults in the LangChain integration. In either case, if we are changing the defaults we probably should bump the minor version from 0.1 to 0.2, though.

"""Get the default parameters for calling PerplexityChat API."""
return {
"max_tokens": self.max_tokens,
"stream": self.streaming,
"temperature": self.temperature,
"enable_search_classifier": self.enable_search_classifier,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is now broken.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Removed it.

" decide whether to execute a full web search or bypass it, depending on the query.\n",
" This helps to reduce unnecessary searches while still delivering accurate responses.\n",
" \"\"\"\n",
" llm = ChatPerplexity(model=\"sonar\", enable_search_classifier=True)\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears out of date.

feat(chat): bump ChatPerplexity to v0.2, require model on init, conditional params

- Remove default for `model` and add a pre-init validator that errors (with model‑cards URL) if it’s not provided
- Make `temperature`, `max_tokens`, `stream`, and `timeout` only appear in the request payload when explicitly set
- Bump class docstring and version reference to v0.2
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Apr 22, 2025
chore(chat_models): restore `_DictOrPydantic` alias and fix citations handling in `_generate`

- Re‑introduce the `_DictOrPydantic` type alias alongside `_DictOrPydanticClass`  
- In `_generate`, initialize `additional_kwargs` with `{"citations": response.citations}` and always append `images` and `related_questions`
@jamesliounis
Copy link
Author

I restored the undone PRs (which occurred because branch is now outdated) and have ran all the tests.

Screenshot 2025-04-22 at 3 25 00 PM Screenshot 2025-04-22 at 3 25 08 PM Screenshot 2025-04-22 at 3 25 16 PM

Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still seeing tests failing on CI, undoing previous PRs and other unrelated changes.


See full list of supported init args and their descriptions in the params section.

Instantiate:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete these? These are standard components of our API references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Related to langchain-community size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants