This will help you get started with PerplexitySearchRetrieverretrieval. For detailed documentation of all PerplexitySearchRetriever features and configurations head to the API reference.
You will need to populate a PERPLEXITY_API_KEY environment variable with your Perplexity API key, or pass it into the constructor as apiKey. Get a key from the Perplexity API key dashboard.
process.env.PERPLEXITY_API_KEY = "your-api-key";
If you want to get automated tracing from individual queries, you can also set your LangSmith API key by uncommenting below:
// process.env.LANGSMITH_API_KEY = "<YOUR API KEY HERE>";// process.env.LANGSMITH_TRACING = "true";
import { PerplexitySearchRetriever } from "@langchain/perplexity";const retriever = new PerplexitySearchRetriever({ maxResults: 5, // searchDomainFilter: ["wikipedia.org"], // searchRecencyFilter: "week",});
Constructor options include maxResults, country, searchDomainFilter, searchRecencyFilter, searchAfterDate, and searchBeforeDate. See the Perplexity Search API parameters for details.
Like other retrievers, PerplexitySearchRetriever can be incorporated into LLM applications via chains.
import { ChatPromptTemplate } from "@langchain/core/prompts";import { ChatAnthropic } from "@langchain/anthropic";import { RunnablePassthrough, RunnableSequence,} from "@langchain/core/runnables";import { StringOutputParser } from "@langchain/core/output_parsers";import { Document } from "@langchain/core/documents";const prompt = ChatPromptTemplate.fromTemplate(`Answer the question based only on the context provided.Context: {context}Question: {question}`);const llm = new ChatAnthropic({ model: "claude-3-5-haiku-latest" });const formatDocs = (docs: Document[]) => docs.map((doc) => doc.pageContent).join("\n\n");const ragChain = RunnableSequence.from([ { context: retriever.pipe(formatDocs), question: new RunnablePassthrough(), }, prompt, llm, new StringOutputParser(),]);await ragChain.invoke("Who won the most recent FIFA World Cup?");