LangChain provides first-class integrations for Perplexity in both Python (langchain-perplexity) and JavaScript/TypeScript (@langchain/community). Both packages let you build LLM applications with real-time web search, citations, and Perplexity’s Pro Search reasoning.
LangChain is a popular Python framework for building applications powered by large language models. It provides composable components for chains, agents, and retrieval-augmented generation (RAG). Learn more at langchain.com.
The integration includes:
ChatPerplexity - Chat model with Pro Search, streaming, and search controls
PerplexitySearchRetriever - Retriever for RAG applications
PerplexitySearchResults - Tool for LangChain agents
Use ChatPerplexity for conversational AI with web search:
from langchain_perplexity import ChatPerplexitychat = ChatPerplexity(model="sonar")response = chat.invoke("What breakthroughs in fusion energy have been announced this year?")print(response.content)
from langchain_perplexity import ChatPerplexity, WebSearchOptionschat = ChatPerplexity( model="sonar-pro", web_search_options=WebSearchOptions(search_type="pro"))response = chat.invoke("How does the electoral college work?")# Access reasoning stepsif reasoning := response.additional_kwargs.get("reasoning_steps"): for step in reasoning: print(f"Thought: {step['thought']}")
from langchain_core.prompts import ChatPromptTemplatefrom langchain_core.runnables import RunnablePassthroughfrom langchain_core.output_parsers import StrOutputParserfrom langchain_perplexity import ChatPerplexity, PerplexitySearchRetrieverllm = ChatPerplexity(model="sonar")retriever = PerplexitySearchRetriever(k=3)template = """Answer based on the following context:{context}Question: {question}"""prompt = ChatPromptTemplate.from_template(template)def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs)rag_chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser())answer = rag_chain.invoke("What is the current status of ITER?")print(answer)
The JavaScript integration ships in the @langchain/community package as ChatPerplexity. It is an OpenAI-compatible chat model that talks to https://api.perplexity.ai.
import { ChatPerplexity } from "@langchain/community/chat_models/perplexity";const llm = new ChatPerplexity({ model: "sonar", temperature: 0, maxRetries: 2,});const aiMsg = await llm.invoke([ { role: "system", content: "You are a helpful assistant that answers with web-grounded citations.", }, { role: "user", content: "What breakthroughs in fusion energy were announced this year?" },]);console.log(aiMsg.content);// Citations and other metadataconsole.log(aiMsg.additional_kwargs.citations);
const stream = await llm.stream("Explain quantum computing in two paragraphs.");for await (const chunk of stream) { process.stdout.write(chunk.content as string);}
import { ChatPromptTemplate } from "@langchain/core/prompts";const prompt = ChatPromptTemplate.fromMessages([ ["system", "You translate English into {language}."], ["human", "{input}"],]);const chain = prompt.pipe(llm);const res = await chain.invoke({ language: "French", input: "I love programming.",});console.log(res.content);