Ability to connect perplexity API

Ability to connect perplexity API to my pickaxe

Hi @saabia you can now do this using actions

1 Like

It is possible to connect ModelsLab API to Actions?

Hi @Kimi Modellabs has REST APIs so yes you can connect to it through actions

I would love for perplexity to be an option

@lloydpearsoniv you can connect to perplexity with a custom action

Here you go. You can use this code to create the action:

import os
import requests

def perplexity(user_query: str):
“”"
Uses perplexity to search for news on the web

Args:
    user_query (string): Search the web for user's query
Envs:
    PERPLEXITY_API (string): API for perplexity search
"""

url = "https://api.perplexity.ai/chat/completions"

payload = {
    "model": "llama-3.1-sonar-small-128k-online",
    "messages": [
        {
            "role": "system",
            "content": "Be precise and concise."
        },
        {
            "role": "user",
            "content": user_query
        }
    ]
}
headers = {
    "Authorization": f"Bearer {os.environ['PERPLEXITY_API']}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
    return response.json()
except requests.RequestException as e:
    return {"error": f"API request failed: {str(e)}"}

Example usage

result = perplexity(“What are the latest developments in AI?”)

print(result)

1 Like

@andreab Please explain how I can use the action to connect perplexity. I don’t know how to do it, and I don’t know much about coding. Thanks

@stephenasuncion we should make a pre-built perplexity action!

1 Like

I know but I want it to be one of the available models that you can choose from

1 Like

You can also do the same thing for the OpenAI o1 preview and o1 mini models. Create prebuilt actions for them.

1 Like

@saabia describe what you are trying to achieve. You want the pickaxe to send some data to Perplexity I assume. Where does the data come from and what you want to achieve after perplexity returns an answer?

Yes please, a native perplexity action would be great

Hi @zerodot , thanks for this!!

Can you mock up what the code would look like with my perpexity API code?

Here you go.