Skip to content

helallao/perplexity-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Important Note 02/03/2024

EVERYONE, military service is mandatory in Turkey and im a Turk, so im going to go to military soon and this repository will not be updated anymore. I'm leaving this repository to community, andbody who wants to use it and capable to update or fix codes can open a pull request (as an update) and others can use these pull requests.




Perplexity.ai

This module is simply just an API Wrapper and account generator. It serves as an API wrapper with free copilots.

How It Works

This module uses emailnator to generate new accounts. As you know, when you create a new account, you will have 5 copilots. That's how it works! This module will generate you new gmails (using @googlemail.com right now) with emailnator and you will have UNLIMITED COPILOTS!

Requirements

Click to expand

Install requirements with:

pip3 install -r requirements.txt

or with single-line command:

pip3 install requests&&pip3 install websocket-client&&pip3 install requests-toolbelt

and aiohttp if you are going to use asynchronous version:

pip3 install aiohttp

How To Use

First thing first, Perplexity.ai is protected by cloudflare, and emailnator too. We need to open this pages manually and get the cookies. Do not forget these cookies are temporary, so you need to renew them continuously. Here how to get your cookies.

import perplexity

perplexity_headers = {
    <your headers here>
}

perplexity_cookies = { 
    <your cookies here>
}

emailnator_headers = { 
    <your headers here>
}

emailnator_cookies = { 
    <your cookies here>
}


# If you're going to use your own account, login to your account and copy headers/cookies (reload the page). Set "own" as True, and do not call "create_account" function. This will deactivate copilot and file upload limit controls
perplexity_cli = perplexity.Client(perplexity_headers, perplexity_cookies, own=False)
perplexity_cli.create_account(emailnator_headers, emailnator_cookies) # Creates a new gmail, so your 5 copilots will be renewed. You can pass this one if you are not going to use "copilot" mode


# takes a string as query, and returns a string as answer.
def my_text_prompt_solver(query):
    return input(f'{query}: ')

# takes a string as description and a dictionary as options. Dictionary consists of ids and values. Example: {1: "Orange", 2: "Banana"}
# returns a list of integers which are ids of selected options. Let's say you selected "Banana", function should return [2]
def my_checkbox_prompt_solver(description, options):
    print(description + '\n' + '\n'.join([str(x) + ' - ' + options[x] for x in options]))
    return [int(input('--> '))]


# modes = ['concise', 'copilot']
# focus = ['internet', 'scholar', 'writing', 'wolfram', 'youtube', 'reddit']
# files = file list, each element of list is tuple like this: (data, filetype) perplexity supports two file types, txt and pdf
# follow_up = last query info for follow-up queries, you can directly pass response json from a query, look at second example below.
# ai_model = ['default', 'experimental', 'gpt-4', 'claude-2.1', 'gemini pro'] only works for own=True clients (perplexity.Client(..., own=True))
# solvers, list of functions to answer questions of ai while using copilot, there are 2 type of solvers, text and checkbox. If you do not define function for a solver, questions in that solver type will be skipped
resp = perplexity_cli.search('Your query here', mode='copilot', focus='internet', files=[(open('myfile.txt', 'rb').read(), 'txt'), (open('myfile2.pdf', 'rb').read(), 'pdf')], ai_model='default', solvers={
    'text': my_text_prompt_solver,
    'checkbox': my_checkbox_prompt_solver
    })
print(resp)

# second example to show how to use follow-up queries
# you can't use file uploads on follow-up queries
# you can pass response json from a query directly like below
resp2 = perplexity_cli.search('Your query here', mode='copilot', focus='internet', follow_up=resp, solvers={
    'text': my_text_prompt_solver,
    'checkbox': my_checkbox_prompt_solver
    })
print(resp2)

# perplexity_cli.create_account(emailnator_headers, emailnator_cookies) # Call this function again when you run out of copilots

Labs

Open the Labs and copy headers/cookies as explained here (reload the page).

import perplexity

labs_headers = {
    <your headers here>
}

labs_cookies = { 
    <your cookies here>
}

labs_cli = perplexity.LabsClient(labs_headers, labs_cookies)

# model = ['pplx-7b-online', 'pplx-70b-online', 'pplx-7b-chat', 'pplx-70b-chat', 'mistral-7b-instruct', 'codellama-34b-instruct', 'codellama-70b-instruct', 'llama-2-70b-chat', 'llava-7b-chat', 'mixtral-8x7b-instruct', 'mistral-medium', 'related']
print(labs_cli.ask('hi', model='pplx-7b-online'))

# this function adds a custom message to conversation
# role = ['user', 'assistant']
labs_cli.add_custom_message('msg', role='assistant')

# this function resets the conversation
labs_cli.clear_history()

Pool

If you don't want to wait while creating account, then pool is what you're looking for. It will simply work on background and create accounts when your current copilot & file upload count is lower than the limit.

import perplexity

perplexity_headers = {
    <your headers here>
}

perplexity_cookies = { 
    <your cookies here>
}

emailnator_headers = { 
    <your headers here>
}

emailnator_cookies = { 
    <your cookies here>
}


def my_text_prompt_solver(query):
    return input(f'{query}: ')

def my_checkbox_prompt_solver(description, options):
    print(description + '\n' + '\n'.join([str(x) + ' - ' + options[x] for x in options]))
    return [int(input('--> '))]

# copilots = minimum needed copilot count, when current copilot count is lower than this, a new account will be created in the background
# file_uploads = minimum needed file upload count, when current file upload count is lower than this, a new account will be created in the background
# threads = how many accounts will be created in the background at the same time, if you're not going to use 50+ copilots in a minute, don't increase this
pool = perplexity.Pool(perplexity_headers, perplexity_cookies, emailnator_headers, emailnator_cookies, copilots=10, file_uploads=5, threads=1)

# everything is same
resp = pool.search('Your query here', mode='copilot', focus='internet', files=[(open('myfile.txt', 'rb').read(), 'txt'), (open('myfile2.pdf', 'rb').read(), 'pdf')], solvers={
    'text': my_text_prompt_solver,
    'checkbox': my_checkbox_prompt_solver
    })
print(resp)

Asynchronous Version

import perplexity_async
import asyncio

perplexity_headers = { 
    <your headers here>
}

perplexity_cookies = { 
    <your cookies here> 
}

emailnator_headers = { 
    <your headers here>
}

emailnator_cookies = { 
    <your cookies here>
}


# takes a string as query, and returns a string as answer.
async def my_text_prompt_solver(query):
    return input(f'{query}: ')

# takes a string as description and a dictionary as options. Dictionary consists of ids and values. Example: {1: "Orange", 2: "Banana"}
# returns a list of integers which are ids of selected options. Let's say you selected "Banana", function should return [2]
async def my_checkbox_prompt_solver(description, options):
    print(description + '\n' + '\n'.join([str(x) + ' - ' + options[x] for x in options]))
    return [int(input('--> '))]


async def test():
    # If you're going to use your own account, login to your account and copy headers/cookies (reload the page). Set "own" as True, and do not call "create_account" function. This will deactivate copilot and file upload limit controls
    perplexity_cli = await perplexity_async.Client(perplexity_headers, perplexity_cookies, own=False)
    await perplexity_cli.create_account(emailnator_headers, emailnator_cookies) # Creates a new gmail, so your 5 copilots will be renewed. You can pass this one if you are not going to use "copilot" mode

    # modes = ['concise', 'copilot']
    # focus = ['internet', 'scholar', 'writing', 'wolfram', 'youtube', 'reddit']
    # files = file list, each element of list is tuple like this: (data, filetype) perplexity supports two file types, txt and pdf
    # follow_up = last query info for follow-up queries, you can directly pass response json from a query, look at second example below.
    # ai_model = ['default', 'experimental', 'gpt-4', 'claude-2.1', 'gemini pro'] only works for own=True clients (perplexity_async.Client(..., own=True))
    # solvers, list of functions to answer questions of ai while using copilot, there are 2 type of solvers, text and checkbox. If you do not define function for a solver, questions in that solver type will be skipped
    resp = await perplexity_cli.search('Your query here', mode='copilot', focus='internet', files=[(open('myfile.txt', 'rb').read(), 'txt'), (open('myfile2.pdf', 'rb').read(), 'pdf')], ai_model='default', solvers={
        'text': my_text_prompt_solver,
        'checkbox': my_checkbox_prompt_solver
    })
    print(resp)

    # second example to show how to use follow-up queries
    # you can't use file uploads on follow-up queries
    # you can pass response json from a query directly like below
    resp2 = await perplexity_cli.search('Your query here', mode='copilot', focus='internet', follow_up=resp, solvers={
        'text': my_text_prompt_solver,
        'checkbox': my_checkbox_prompt_solver
    })
    print(resp2)

    # await perplexity_cli.create_account(emailnator_headers, emailnator_cookies) # Call this function again when you're out of copilots

asyncio.run(test())

Asynchronous Labs

Open the Labs and copy headers/cookies as explained here (reload the page).

import perplexity_async

labs_headers = {
    <your headers here>
}

labs_cookies = { 
    <your cookies here>
}

async def test():
    labs_cli = await perplexity_async.LabsClient(labs_headers, labs_cookies)

    # model = ['pplx-7b-online', 'pplx-70b-online', 'pplx-7b-chat', 'pplx-70b-chat', 'mistral-7b-instruct', 'codellama-34b-instruct', 'codellama-70b-instruct', 'llama-2-70b-chat', 'llava-7b-chat', 'mixtral-8x7b-instruct', 'mistral-medium', 'related']
    print(await labs_cli.ask('hi', model='pplx-7b-online'))

    # this function adds a custom message to conversation
    # role = ['user', 'assistant']
    labs_cli.add_custom_message('msg', role='assistant')

    # this function resets the conversation
    labs_cli.clear_history()

asyncio.run(test())

How To Get The Cookies

Do not forget these cookies are temporary, so you need to renew them continuously.

  1. Open emailnator website. Click F12 or Ctrl+Shift+I to open inspector. Go to the "Network" tab in the inspector, check "Preserve log" button, click the "Go !" button in the website, right click the "message-list" in the Network Tab and hover on "Copy" and click to "Copy as cURL (bash)". Now go to the curlconverter, paste your code here. The header and cookies dictionary will appear, copy and use them in your codes.

  1. Open Perplexity.ai website. Sign out if you're signed in. Click F12 or Ctrl+Shift+I to open inspector. Go to the "Network" tab in the inspector, check "Preserve log" button, click the "Sign Up" button in the website, enter a random email and click "Continue with Email" button, right click the "email" in the Network Tab and hover on "Copy" and click to "Copy as cURL (bash)". Now go to the curlconverter, paste your code here. The header and cookies dictionary will appear, copy them and use in your codes.

  1. Don't confuse the headers and cookies for emailnator and Perplexity.ai, look at How To Use to learn how to use them.

Thanks To