Skip to content
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

Community/Partners: Add support for Perplexity AI #17024

Merged
merged 33 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f816626
Add Perplexity boilerplate
atherfawaz Feb 4, 2024
994af05
Sanitize
atherfawaz Feb 4, 2024
e0d467f
Add tests
atherfawaz Feb 4, 2024
c011593
Lint
atherfawaz Feb 4, 2024
9cf23c1
Complete unit tests
atherfawaz Feb 4, 2024
cf30da5
Remove unused fn
atherfawaz Feb 4, 2024
d808172
Update perplexties
atherfawaz Feb 5, 2024
7e9642d
Merge branch 'master' into master
atherfawaz Feb 5, 2024
cb6b5cc
cr
baskaryan Feb 5, 2024
78a1ee8
cr
baskaryan Feb 5, 2024
9464b3c
fmt
baskaryan Feb 5, 2024
5577809
Merge branch 'master' into atherfawaz/master
baskaryan Feb 6, 2024
ea246ca
fmt
baskaryan Feb 6, 2024
e44a0c5
fmt
baskaryan Feb 6, 2024
11d5c02
nit
baskaryan Feb 6, 2024
3e2c640
Add docs
atherfawaz Feb 6, 2024
7ba1a7f
Merge branch 'master' into master
atherfawaz Feb 6, 2024
62239b2
Lint
atherfawaz Feb 7, 2024
a54ebe0
Merge branch 'master' into master
atherfawaz Feb 8, 2024
28150d3
Merge branch 'master' into atherfawaz/master
efriis Feb 10, 2024
346c03b
PerplexityLLM
efriis Feb 10, 2024
ec1995e
LLM
efriis Feb 10, 2024
84aeda5
Merge branch 'master' into master
atherfawaz Feb 19, 2024
29fd27d
Move module import inside
atherfawaz Feb 21, 2024
6669968
Merge branch 'master' into master
atherfawaz Feb 21, 2024
fc43713
Merge branch 'master' into master
atherfawaz Feb 23, 2024
faf4110
Merge branch 'master' into master
atherfawaz Feb 24, 2024
809725d
Remove PerplexityLLM
atherfawaz Feb 24, 2024
0079edd
Lint, remove LLM notebook
atherfawaz Feb 26, 2024
d59c62d
Merge branch 'master' into master
atherfawaz Feb 26, 2024
3f67e0e
Lint, remove LLM notebook
atherfawaz Feb 26, 2024
003822b
Merge branch 'master' into master
atherfawaz Mar 1, 2024
c323b7c
Remove unused import
atherfawaz Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 229 additions & 0 deletions docs/docs/integrations/chat/perplexity.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
"cells": [
{
"cell_type": "raw",
"id": "a016701c",
"metadata": {},
"source": [
"---\n",
"sidebar_label: Perplexity\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "bf733a38-db84-4363-89e2-de6735c37230",
"metadata": {},
"source": [
"# ChatPerplexity\n",
"\n",
"This notebook covers how to get started with Perplexity chat models."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-19T11:25:00.590587Z",
"start_time": "2024-01-19T11:25:00.127293Z"
},
"tags": []
},
"outputs": [],
"source": [
"from langchain_community.chat_models import ChatPerplexity\n",
"from langchain_core.prompts import ChatPromptTemplate"
]
},
{
"cell_type": "markdown",
"id": "97a8ce3a",
"metadata": {},
"source": [
"The code provided assumes that your PPLX_API_KEY is set in your environment variables. If you would like to manually specify your API key and also choose a different model, you can use the following code:\n",
"\n",
"```python\n",
"chat = ChatPerplexity(temperature=0, pplx_api_key=\"YOUR_API_KEY\", model=\"pplx-70b-online\")\n",
"```\n",
"\n",
"You can check a list of available models [here](https://docs.perplexity.ai/docs/model-cards). For reproducibility, we can set the API key dynamically by taking it as an input in this notebook."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d3e49d78",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from getpass import getpass\n",
"\n",
"PPLX_API_KEY = getpass()\n",
"os.environ[\"PPLX_API_KEY\"] = PPLX_API_KEY"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-19T11:25:04.349676Z",
"start_time": "2024-01-19T11:25:03.964930Z"
},
"tags": []
},
"outputs": [],
"source": [
"chat = ChatPerplexity(temperature=0, model=\"pplx-70b-online\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-19T11:25:07.274418Z",
"start_time": "2024-01-19T11:25:05.898031Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'The Higgs Boson is an elementary subatomic particle that plays a crucial role in the Standard Model of particle physics, which accounts for three of the four fundamental forces governing the behavior of our universe: the strong and weak nuclear forces, electromagnetism, and gravity. The Higgs Boson is important for several reasons:\\n\\n1. **Final Elementary Particle**: The Higgs Boson is the last elementary particle waiting to be discovered under the Standard Model. Its detection helps complete the Standard Model and further our understanding of the fundamental forces in the universe.\\n\\n2. **Mass Generation**: The Higgs Boson is responsible for giving mass to other particles, a process that occurs through its interaction with the Higgs field. This mass generation is essential for the formation of atoms, molecules, and the visible matter we observe in the universe.\\n\\n3. **Implications for New Physics**: While the detection of the Higgs Boson has confirmed many aspects of the Standard Model, it also opens up new possibilities for discoveries beyond the Standard Model. Further research on the Higgs Boson could reveal insights into the nature of dark matter, supersymmetry, and other exotic phenomena.\\n\\n4. **Advancements in Technology**: The search for the Higgs Boson has led to significant advancements in technology, such as the development of artificial intelligence and machine learning algorithms used in particle accelerators like the Large Hadron Collider (LHC). These advancements have not only contributed to the discovery of the Higgs Boson but also have potential applications in various other fields.\\n\\nIn summary, the Higgs Boson is important because it completes the Standard Model, plays a crucial role in mass generation, hints at new physics phenomena beyond the Standard Model, and drives advancements in technology.\\n'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"system = \"You are a helpful assistant.\"\n",
"human = \"{input}\"\n",
"prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
"\n",
"chain = prompt | chat\n",
"response = chain.invoke({\"input\": \"Why is the Higgs Boson important?\"})\n",
"response.content"
]
},
{
"cell_type": "markdown",
"id": "de6d8d5a",
"metadata": {},
"source": [
"You can format and structure the prompts like you would typically. In the following example, we ask the model to tell us a joke about cats."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-19T11:25:10.448733Z",
"start_time": "2024-01-19T11:25:08.866277Z"
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'Here\\'s a joke about cats:\\n\\nWhy did the cat want math lessons from a mermaid?\\n\\nBecause it couldn\\'t find its \"core purpose\" in life!\\n\\nRemember, cats are unique and fascinating creatures, and each one has its own special traits and abilities. While some may see them as mysterious or even a bit aloof, they are still beloved pets that bring joy and companionship to their owners. So, if your cat ever seeks guidance from a mermaid, just remember that they are on their own journey to self-discovery!\\n'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat = ChatPerplexity(temperature=0, model=\"pplx-70b-online\")\n",
"prompt = ChatPromptTemplate.from_messages([(\"human\", \"Tell me a joke about {topic}\")])\n",
"chain = prompt | chat\n",
"response = chain.invoke({\"topic\": \"cats\"})\n",
"response.content"
]
},
{
"cell_type": "markdown",
"id": "13d93dc4",
"metadata": {},
"source": [
"## `ChatPerplexity` also supports streaming functionality:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "025be980-e50d-4a68-93dc-c9c7b500ce34",
"metadata": {
"ExecuteTime": {
"end_time": "2024-01-19T11:25:24.438696Z",
"start_time": "2024-01-19T11:25:14.687480Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is a list of some famous tourist attractions in Pakistan:\n",
"\n",
"1. **Minar-e-Pakistan**: A 62-meter high minaret in Lahore that represents the history of Pakistan.\n",
"2. **Badshahi Mosque**: A historic mosque in Lahore with a capacity of 10,000 worshippers.\n",
"3. **Shalimar Gardens**: A beautiful garden in Lahore with landscaped grounds and a series of cascading pools.\n",
"4. **Pakistan Monument**: A national monument in Islamabad representing the four provinces and three districts of Pakistan.\n",
"5. **National Museum of Pakistan**: A museum in Karachi showcasing the country's cultural history.\n",
"6. **Faisal Mosque**: A large mosque in Islamabad that can accommodate up to 300,000 worshippers.\n",
"7. **Clifton Beach**: A popular beach in Karachi offering water activities and recreational facilities.\n",
"8. **Kartarpur Corridor**: A visa-free border crossing and religious corridor connecting Gurdwara Darbar Sahib in Pakistan to Gurudwara Sri Kartarpur Sahib in India.\n",
"9. **Mohenjo-daro**: An ancient Indus Valley civilization site in Sindh, Pakistan, dating back to around 2500 BCE.\n",
"10. **Hunza Valley**: A picturesque valley in Gilgit-Baltistan known for its stunning mountain scenery and unique culture.\n",
"\n",
"These attractions showcase the rich history, diverse culture, and natural beauty of Pakistan, making them popular destinations for both local and international tourists.\n"
]
}
],
"source": [
"chat = ChatPerplexity(temperature=0.7, model=\"pplx-70b-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",
" print(chunk.content, end=\"\", flush=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
143 changes: 143 additions & 0 deletions docs/docs/integrations/llms/perplexity.ipynb
atherfawaz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "9597802c",
"metadata": {},
"source": [
"# Perplexity\n",
"\n",
"This example goes over how to use LangChain to interact with `Perplexity` [models](https://docs.perplexity.ai/docs/model-cards).\n",
"\n",
"The code provided assumes that your PPLX_API_KEY is set in your environment variables. If you would like to manually specify your API key and also choose a different model, you can use the following code:\n",
"\n",
"```python\n",
"llm = PerplexityLLM(model=\"pplx-70b-online\", pplx_api_key=\"YOUR_API_KEY\")\n",
"```\n",
"\n",
"For reproducibility, we can set the API key dynamically by taking it as an input in this notebook."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5472a7cd-af26-48ca-ae9b-5f6ae73c74d2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"from getpass import getpass\n",
"\n",
"PPLX_API_KEY = getpass()\n",
"os.environ[\"PPLX_API_KEY\"] = PPLX_API_KEY"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6fb585dd",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain.chains import LLMChain\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain_community.llms.perplexity import PerplexityLLM"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "035dea0f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"\n",
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3f3458d9",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"llm = PerplexityLLM(model=\"pplx-70b-online\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a641dbd9",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9f844993",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'Justin Bieber was born on March 1, 1994. The Super Bowl that took place in the year Justin Bieber was born was Super Bowl XXVIII. The game was played on January 30, 1994, and it was between the Dallas Cowboys and the Buffalo Bills. The Dallas Cowboys won the game with a score of 30-13, and their wide receiver, Michael Irvin, was named the MVP (Most Valuable Player) of the game.\\n'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"\n",
"llm_chain.run(question)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.11.1 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
},
"vscode": {
"interpreter": {
"hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 2 additions & 0 deletions libs/community/langchain_community/chat_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from langchain_community.chat_models.ollama import ChatOllama
from langchain_community.chat_models.openai import ChatOpenAI
from langchain_community.chat_models.pai_eas_endpoint import PaiEasChatEndpoint
from langchain_community.chat_models.perplexity import ChatPerplexity
from langchain_community.chat_models.promptlayer_openai import PromptLayerChatOpenAI
from langchain_community.chat_models.sparkllm import ChatSparkLLM
from langchain_community.chat_models.tongyi import ChatTongyi
Expand Down Expand Up @@ -98,5 +99,6 @@
"GPTRouter",
"ChatYuan2",
"ChatZhipuAI",
"ChatPerplexity",
"ChatKinetica",
]
Loading
Loading