Skip to content

Commit 3305ba9

Browse files
anadi45jacoblee93
andauthored
feat(community): Perplexity integration (#7817)
Co-authored-by: jacoblee93 <[email protected]>
1 parent bac72ef commit 3305ba9

File tree

8 files changed

+843
-2
lines changed

8 files changed

+843
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "raw",
5+
"id": "afaf8039",
6+
"metadata": {
7+
"vscode": {
8+
"languageId": "raw"
9+
}
10+
},
11+
"source": [
12+
"---\n",
13+
"sidebar_label: Perplexity\n",
14+
"---"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "e49f1e0d",
20+
"metadata": {},
21+
"source": [
22+
"# ChatPerplexity\n",
23+
"\n",
24+
"This guide will help you getting started with Perplexity [chat models](/docs/concepts/#chat-models). For detailed documentation of all `ChatPerplexity` features and configurations head to the [API reference](https://api.js.langchain.com/classes/_langchain_community.chat_models_perplexity.ChatPerplexity.html).\n",
25+
"\n",
26+
"## Overview\n",
27+
"### Integration details\n",
28+
"\n",
29+
"| Class | Package | Local | Serializable | [PY support](https://python.langchain.com/docs/integrations/chat/perplexity/) | Package downloads | Package latest |\n",
30+
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
31+
"| [`ChatPerplexity`](https://api.js.langchain.com/classes/_langchain_community.chat_models_perplexity.ChatPerplexity.html) | [`@langchain/community`](https://npmjs.com/@langchain/community) | ❌ | beta | ✅ | ![NPM - Downloads](https://img.shields.io/npm/dm/@langchain/community?style=flat-square&label=%20&) | ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |\n",
32+
"\n",
33+
"### Model features\n",
34+
"\n",
35+
"See the links in the table headers below for guides on how to use specific features.\n",
36+
"\n",
37+
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
38+
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
39+
"| ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ❌ | \n",
40+
"\n",
41+
"Note that at the time of writing, Perplexity only supports structured outputs on certain usage tiers.\n",
42+
"\n",
43+
"## Setup\n",
44+
"\n",
45+
"To access Perplexity models you'll need to create a Perplexity account, get an API key, and install the `@langchain/community` integration package.\n",
46+
"\n",
47+
"### Credentials\n",
48+
"\n",
49+
"Head to https://perplexity.ai to sign up for Perplexity and generate an API key. Once you've done this set the `PERPLEXITY_API_KEY` environment variable:\n",
50+
"\n",
51+
"```bash\n",
52+
"export PERPLEXITY_API_KEY=\"your-api-key\"\n",
53+
"```\n",
54+
"\n",
55+
"If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:\n",
56+
"\n",
57+
"```bash\n",
58+
"# export LANGSMITH_TRACING=\"true\"\n",
59+
"# export LANGSMITH_API_KEY=\"your-api-key\"\n",
60+
"```\n",
61+
"\n",
62+
"### Installation\n",
63+
"\n",
64+
"The LangChain Perplexity integration lives in the `@langchain/community` package:\n",
65+
"\n",
66+
"```{=mdx}\n",
67+
"import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n",
68+
"import Npm2Yarn from \"@theme/Npm2Yarn\";\n",
69+
"\n",
70+
"<IntegrationInstallTooltip></IntegrationInstallTooltip>\n",
71+
"\n",
72+
"<Npm2Yarn>\n",
73+
" @langchain/community @langchain/core\n",
74+
"</Npm2Yarn>\n",
75+
"\n",
76+
"```"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
82+
"metadata": {},
83+
"source": [
84+
"## Instantiation\n",
85+
"\n",
86+
"Now we can instantiate our model object and generate chat completions:"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 1,
92+
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"import { ChatPerplexity } from \"@langchain/community/chat_models/perplexity\"\n",
97+
"\n",
98+
"const llm = new ChatPerplexity({\n",
99+
" model: \"sonar\",\n",
100+
" temperature: 0,\n",
101+
" maxTokens: undefined,\n",
102+
" timeout: undefined,\n",
103+
" maxRetries: 2,\n",
104+
" // other params...\n",
105+
"})"
106+
]
107+
},
108+
{
109+
"cell_type": "markdown",
110+
"id": "2b4f3e15",
111+
"metadata": {},
112+
"source": [
113+
"## Invocation"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 2,
119+
"id": "62e0dbc3",
120+
"metadata": {
121+
"tags": []
122+
},
123+
"outputs": [
124+
{
125+
"name": "stdout",
126+
"output_type": "stream",
127+
"text": [
128+
"AIMessage {\n",
129+
" \"id\": \"run-71853938-aa30-4861-9019-f12323c09f9a\",\n",
130+
" \"content\": \"J'adore la programmation.\",\n",
131+
" \"additional_kwargs\": {\n",
132+
" \"citations\": [\n",
133+
" \"https://careersatagoda.com/blog/why-we-love-programming/\",\n",
134+
" \"https://henrikwarne.com/2012/06/02/why-i-love-coding/\",\n",
135+
" \"https://forum.freecodecamp.org/t/i-love-programming-but/497502\",\n",
136+
" \"https://ilovecoding.org\",\n",
137+
" \"https://thecodinglove.com\"\n",
138+
" ]\n",
139+
" },\n",
140+
" \"response_metadata\": {\n",
141+
" \"tokenUsage\": {\n",
142+
" \"promptTokens\": 20,\n",
143+
" \"completionTokens\": 9,\n",
144+
" \"totalTokens\": 29\n",
145+
" }\n",
146+
" },\n",
147+
" \"tool_calls\": [],\n",
148+
" \"invalid_tool_calls\": []\n",
149+
"}\n"
150+
]
151+
}
152+
],
153+
"source": [
154+
"const aiMsg = await llm.invoke([\n",
155+
" {\n",
156+
" role: \"system\",\n",
157+
" content: \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
158+
" },\n",
159+
" {\n",
160+
" role: \"user\",\n",
161+
" content: \"I love programming.\",\n",
162+
" },\n",
163+
"])\n",
164+
"aiMsg"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": 3,
170+
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705",
171+
"metadata": {},
172+
"outputs": [
173+
{
174+
"name": "stdout",
175+
"output_type": "stream",
176+
"text": [
177+
"J'adore la programmation.\n"
178+
]
179+
}
180+
],
181+
"source": [
182+
"console.log(aiMsg.content)"
183+
]
184+
},
185+
{
186+
"cell_type": "markdown",
187+
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8",
188+
"metadata": {},
189+
"source": [
190+
"## Chaining\n",
191+
"\n",
192+
"We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": 4,
198+
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
199+
"metadata": {},
200+
"outputs": [
201+
{
202+
"name": "stdout",
203+
"output_type": "stream",
204+
"text": [
205+
"AIMessage {\n",
206+
" \"id\": \"run-a44dc452-4a71-423d-a4ee-50a2d7c90abd\",\n",
207+
" \"content\": \"**English to German Translation:**\\n\\n\\\"I love programming\\\" translates to **\\\"Ich liebe das Programmieren.\\\"**\\n\\nIf you'd like to express your passion for programming in more detail, here are some additional translations:\\n\\n- **\\\"Programming is incredibly rewarding and fulfilling.\\\"** translates to **\\\"Das Programmieren ist unglaublich lohnend und erfüllend.\\\"**\\n- **\\\"I enjoy solving problems through coding.\\\"** translates to **\\\"Ich genieße es, Probleme durch Codieren zu lösen.\\\"**\\n- **\\\"I find the process of creating something from nothing very satisfying.\\\"** translates to **\\\"Ich finde den Prozess, etwas aus dem Nichts zu schaffen, sehr befriedigend.\\\"**\",\n",
208+
" \"additional_kwargs\": {\n",
209+
" \"citations\": [\n",
210+
" \"https://careersatagoda.com/blog/why-we-love-programming/\",\n",
211+
" \"https://henrikwarne.com/2012/06/02/why-i-love-coding/\",\n",
212+
" \"https://dev.to/dvddpl/coding-is-boring-why-do-you-love-coding-cl0\",\n",
213+
" \"https://forum.freecodecamp.org/t/i-love-programming-but/497502\",\n",
214+
" \"https://ilovecoding.org\"\n",
215+
" ]\n",
216+
" },\n",
217+
" \"response_metadata\": {\n",
218+
" \"tokenUsage\": {\n",
219+
" \"promptTokens\": 15,\n",
220+
" \"completionTokens\": 149,\n",
221+
" \"totalTokens\": 164\n",
222+
" }\n",
223+
" },\n",
224+
" \"tool_calls\": [],\n",
225+
" \"invalid_tool_calls\": []\n",
226+
"}\n"
227+
]
228+
}
229+
],
230+
"source": [
231+
"import { ChatPromptTemplate } from \"@langchain/core/prompts\"\n",
232+
"\n",
233+
"const prompt = ChatPromptTemplate.fromMessages(\n",
234+
" [\n",
235+
" [\n",
236+
" \"system\",\n",
237+
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
238+
" ],\n",
239+
" [\"human\", \"{input}\"],\n",
240+
" ]\n",
241+
")\n",
242+
"\n",
243+
"const chain = prompt.pipe(llm);\n",
244+
"await chain.invoke(\n",
245+
" {\n",
246+
" input_language: \"English\",\n",
247+
" output_language: \"German\",\n",
248+
" input: \"I love programming.\",\n",
249+
" }\n",
250+
")"
251+
]
252+
},
253+
{
254+
"cell_type": "markdown",
255+
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
256+
"metadata": {},
257+
"source": [
258+
"## API reference\n",
259+
"\n",
260+
"For detailed documentation of all ChatPerplexity features and configurations head to the API reference: https://api.js.langchain.com/classes/_langchain_community.chat_models_perplexity.ChatPerplexity.html"
261+
]
262+
}
263+
],
264+
"metadata": {
265+
"kernelspec": {
266+
"display_name": "TypeScript",
267+
"language": "typescript",
268+
"name": "tslab"
269+
},
270+
"language_info": {
271+
"codemirror_mode": {
272+
"mode": "typescript",
273+
"name": "javascript",
274+
"typescript": true
275+
},
276+
"file_extension": ".ts",
277+
"mimetype": "text/typescript",
278+
"name": "typescript",
279+
"version": "3.7.2"
280+
}
281+
},
282+
"nbformat": 4,
283+
"nbformat_minor": 5
284+
}

libs/langchain-community/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ chat_models/ollama.cjs
586586
chat_models/ollama.js
587587
chat_models/ollama.d.ts
588588
chat_models/ollama.d.cts
589+
chat_models/perplexity.cjs
590+
chat_models/perplexity.js
591+
chat_models/perplexity.d.ts
592+
chat_models/perplexity.d.cts
589593
chat_models/portkey.cjs
590594
chat_models/portkey.js
591595
chat_models/portkey.d.ts

libs/langchain-community/langchain.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export const config = {
8080
"embeddings/gradient_ai": "embeddings/gradient_ai",
8181
"embeddings/hf": "embeddings/hf",
8282
"embeddings/hf_transformers": "embeddings/hf_transformers",
83-
"embeddings/huggingface_transformers": "embeddings/huggingface_transformers",
83+
"embeddings/huggingface_transformers":
84+
"embeddings/huggingface_transformers",
8485
"embeddings/ibm": "embeddings/ibm",
8586
"embeddings/jina": "embeddings/jina",
8687
"embeddings/llama_cpp": "embeddings/llama_cpp",
@@ -185,6 +186,7 @@ export const config = {
185186
"chat_models/moonshot": "chat_models/moonshot",
186187
"chat_models/novita": "chat_models/novita",
187188
"chat_models/ollama": "chat_models/ollama",
189+
"chat_models/perplexity": "chat_models/perplexity",
188190
"chat_models/portkey": "chat_models/portkey",
189191
"chat_models/premai": "chat_models/premai",
190192
"chat_models/tencent_hunyuan": "chat_models/tencent_hunyuan/index",

libs/langchain-community/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,15 @@
20462046
"import": "./chat_models/ollama.js",
20472047
"require": "./chat_models/ollama.cjs"
20482048
},
2049+
"./chat_models/perplexity": {
2050+
"types": {
2051+
"import": "./chat_models/perplexity.d.ts",
2052+
"require": "./chat_models/perplexity.d.cts",
2053+
"default": "./chat_models/perplexity.d.ts"
2054+
},
2055+
"import": "./chat_models/perplexity.js",
2056+
"require": "./chat_models/perplexity.cjs"
2057+
},
20492058
"./chat_models/portkey": {
20502059
"types": {
20512060
"import": "./chat_models/portkey.d.ts",
@@ -3826,6 +3835,10 @@
38263835
"chat_models/ollama.js",
38273836
"chat_models/ollama.d.ts",
38283837
"chat_models/ollama.d.cts",
3838+
"chat_models/perplexity.cjs",
3839+
"chat_models/perplexity.js",
3840+
"chat_models/perplexity.d.ts",
3841+
"chat_models/perplexity.d.cts",
38293842
"chat_models/portkey.cjs",
38303843
"chat_models/portkey.js",
38313844
"chat_models/portkey.d.ts",

0 commit comments

Comments
 (0)