Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/perplexity-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Perplexity AI Command Setup

This document explains how to set up and use the Perplexity AI command in Experimental-Bell.

## Setup Instructions

### 1. Get a Perplexity API Key

You need to obtain an API key from Perplexity AI:

1. Visit [Perplexity AI](https://www.perplexity.ai/)
2. Create an account or log in
3. Navigate to the API section and generate an API key

### 2. Configure the API Key

You have two options to configure the API key:

#### Option 1: Environment Variable

Set the `PERPLEXITY_API_KEY` environment variable before starting the bot:

```bash
export PERPLEXITY_API_KEY="your-api-key-here"
npm start
```

#### Option 2: API Configuration

Add the Perplexity API key to your API configuration file:

```javascript
// In your API configuration file
module.exports = {
// Other API configurations...
perplexity: {
key: "your-api-key-here"
}
};
```

## Usage

Once configured, you can use the Perplexity AI command with:

```
.perplexity [your question or prompt]
```

or the shorter alias:

```
.pplx [your question or prompt]
```

## Features

- Uses Perplexity's "sonar-medium-online" model
- Provides up-to-date information through Perplexity's search capabilities
- Generates detailed, well-researched responses

## Troubleshooting

If you encounter an error message stating that the API key is not configured, make sure you've correctly set up the API key using one of the methods above.

If you experience other issues, check the console logs for more detailed error messages.
27 changes: 27 additions & 0 deletions helpers/Events/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,33 @@ ${loraText}
}
);

// Import the perplexity function
const { perplexity } = await (fol[2] + 'perplexity.js').r();

ev.on(
{
cmd: ['perplexity', 'pplx'],
tag: 'ai',
args: infos.ai.isQuery,
listmenu: ['perplexity'],
energy: 7,
},
async () => {
// Check if API key is configured
if (!process.env.PERPLEXITY_API_KEY && !api.perplexity?.key) {
return cht.reply(
"[ PERPLEXITY AI ]\nAPI key not configured. Please set the PERPLEXITY_API_KEY environment variable or add 'perplexity: { key: "your-api-key" }' to your API configuration."
);
}

let res = await perplexity(cht.q);
cht.reply('[ PERPLEXITY AI ]\n' + res.response, {
ai: true,
replyAi: false,
});
}
);

ev.on(
{
cmd: ['bell', 'autoai', 'aichat', 'ai_interactive'],
Expand Down
45 changes: 45 additions & 0 deletions machine/perplexity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export async function perplexity(prompt) {
try {
// Use the API key from environment variables or configuration
// The API key should be configured in the bot's environment
const API_KEY = process.env.PERPLEXITY_API_KEY || api.perplexity?.key;

if (!API_KEY) {
return {
response: "Error: Perplexity API key not configured. Please set the PERPLEXITY_API_KEY environment variable or add it to your API configuration.",
};
}

const response = await fetch("https://api.perplexity.ai/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`
},
body: JSON.stringify({
model: "sonar-medium-online",
messages: [
{
role: "user",
content: prompt
}
],
max_tokens: 1024,
temperature: 0.7,
stream: false
})
});

const data = await response.json();
return {
response: data.choices[0].message.content,
usage: data.usage
};
} catch (e) {
console.error('Error in perplexity.js: ' + e.message);
return {
response: "Sorry, I encountered an error while processing your request.",
error: e.message
};
}
}