📊 We use cookies & analytics to improve your experience. Learn more
Latest

Perplexity Mastery — Research With Sources You Can Check Module 3 · Lesson 10 of 15

0% Exit to course

Module 3 · Files, tools and your own data Lesson 10 of 15

The Perplexity API and your first script

Learning objective

Perplexity exposes its search-backed models over an API that returns citations alongside the answer, so a script can build a sourced brief rather than an opinion. You do not need this to finish the course, and you do not need it for a fresher job either — but an afternoon here is what turns "I have used Perplexity" into "I have built with it", which is a different sentence in an interview.

The concept

Perplexity exposes its search-backed models over an API that returns citations alongside the answer, so a script can build a sourced brief rather than an opinion. The idea is simple: the same model you have been chatting with also answers a web request, so you can put it inside a script, a spreadsheet or a page. A key identifies you; a request sends the text; a response comes back as data.

The reason to try it once, even if you never build anything: it makes the chat version less mysterious. You see that the whole conversation is text in and text out, that your instructions are literally lines of a request, and that "the model" is one parameter among several.

A first call looks like this:

Python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_PPLX_KEY",
    base_url="https://api.perplexity.ai",
)

r = client.chat.completions.create(
    model="sonar",
    messages=[{"role": "user", "content": "What did [company] announce in the last six months? Cite sources."}],
)

print(r.choices[0].message.content)
# the response also carries a citations list — keep it, it is the point

Common mistakes

The mistake is throwing away the citations array. Half the value of this API over the others is that it tells you where the answer came from, and a script that discards that is a script that cannot be audited.

Worked example

A script that takes a list of twenty companies and returns a sourced three-line brief on each is the classic use, and it is the shape of many real analyst tasks.

Your practice task

Get a key, run one request that works, and change one word in it to see the answer change. That is the whole of the first afternoon.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

Perplexity exposes its search-backed models over an API that returns citations alongside the answer, so a script can build a sourced brief rather than an opinion.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task — do them and the final project brief writes itself.