community: update perplexity documentation (#30450)

This pull request includes updates to the
`docs/docs/integrations/chat/perplexity.ipynb` file to enhance the
documentation for `ChatPerplexity`. The changes focus on demonstrating
the use of Perplexity-specific parameters and supporting structured
outputs for Tier 3+ users.

Enhancements to documentation:

* Added a new markdown cell explaining the use of Perplexity-specific
parameters through the `ChatPerplexity` class, including parameters like
`search_domain_filter`, `return_images`, `return_related_questions`, and
`search_recency_filter` using the `extra_body` parameter.
* Added a new code cell demonstrating how to invoke `ChatPerplexity`
with the `extra_body` parameter to filter search recency.

Support for structured outputs:

* Added a new markdown cell explaining that `ChatPerplexity` supports
structured outputs for Tier 3+ users.
* Added a new code cell demonstrating how to use `ChatPerplexity` with
structured outputs by defining a `BaseModel` class and invoking the chat
with structured output.[Copilot is generating a summary...]Thank you for
contributing to LangChain!

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
David Sánchez Sánchez 2025-03-24 18:49:59 +01:00 committed by GitHub
parent 50ec4a1a4f
commit d7b13e12ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -173,6 +173,41 @@
"response.content"
]
},
{
"cell_type": "markdown",
"id": "a7f8f61b",
"metadata": {},
"source": [
"## Using Perplexity-specific parameters through `ChatPerplexity`\n",
"\n",
"You can also use Perplexity-specific parameters through the ChatPerplexity class. For example, parameters like search_domain_filter, return_images, return_related_questions or search_recency_filter using the extra_body parameter as shown below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73960f51",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"Sure, here's a cat joke for you:\\n\\nWhy are cats bad storytellers?\\n\\nBecause they only have one tale. (Pun alert!)\\n\\nSource: OneLineFun.com [4]\""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat = ChatPerplexity(temperature=0.7, model=\"llama-3.1-sonar-small-128k-online\")\n",
"response = chat.invoke(\n",
" \"Tell me a joke about cats\", extra_body={\"search_recency_filter\": \"week\"}\n",
")\n",
"response.content"
]
},
{
"cell_type": "markdown",
"id": "13d93dc4",
@ -216,13 +251,56 @@
],
"source": [
"chat = ChatPerplexity(temperature=0.7, model=\"llama-3.1-sonar-small-128k-online\")\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [(\"human\", \"Give me a list of famous tourist attractions in Pakistan\")]\n",
")\n",
"chain = prompt | chat\n",
"for chunk in chain.stream({}):\n",
"\n",
"for chunk in chat.stream(\"Give me a list of famous tourist attractions in Pakistan\"):\n",
" print(chunk.content, end=\"\", flush=True)"
]
},
{
"cell_type": "markdown",
"id": "397c43de",
"metadata": {},
"source": [
"## `ChatPerplexity` Supports Structured Outputs for Tier 3+ Users"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1bae9b80-394a-4340-9c30-612c136b742a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AnswerFormat(first_name='Michael', last_name='Jordan', year_of_birth=1963, num_seasons_in_nba=15)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pydantic import BaseModel\n",
"\n",
"\n",
"class AnswerFormat(BaseModel):\n",
" first_name: str\n",
" last_name: str\n",
" year_of_birth: int\n",
" num_seasons_in_nba: int\n",
"\n",
"\n",
"chat = ChatPerplexity(temperature=0.7, model=\"sonar-pro\")\n",
"structured_chat = chat.with_structured_output(AnswerFormat)\n",
"response = structured_chat.invoke(\n",
" \"Tell me about Michael Jordan. Return your answer \"\n",
" \"as JSON with keys first_name (str), last_name (str), \"\n",
" \"year_of_birth (int), and num_seasons_in_nba (int).\"\n",
")\n",
"response"
]
}
],
"metadata": {
@ -241,7 +319,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.10.4"
}
},
"nbformat": 4,