Skip to content

Commit b0f1cda

Browse files
authored
Merge pull request #46 from AgentOps-AI/perplexity-tool
perplexity tool
2 parents fccd435 + 1592dcc commit b0f1cda

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
3+
import requests
4+
from crewai_tools import tool
5+
6+
from dotenv import load_dotenv
7+
load_dotenv()
8+
9+
url = "https://api.perplexity.ai/chat/completions"
10+
api_key = os.getenv("PERPLEXITY_API_KEY")
11+
12+
@tool
13+
def query_perplexity(query: str):
14+
"""
15+
Use Perplexity to concisely search the internet and answer a query with up-to-date information.
16+
"""
17+
18+
payload = {
19+
"model": "llama-3.1-sonar-small-128k-online",
20+
"messages": [
21+
{
22+
"role": "system",
23+
"content": "Be precise and concise."
24+
},
25+
{
26+
"role": "user",
27+
"content": query
28+
}
29+
],
30+
# "max_tokens": "Optional",
31+
"temperature": 0.2,
32+
"top_p": 0.9,
33+
"return_citations": True,
34+
"search_domain_filter": ["perplexity.ai"],
35+
"return_images": False,
36+
"return_related_questions": False,
37+
"search_recency_filter": "month",
38+
"top_k": 0,
39+
"stream": False,
40+
"presence_penalty": 0,
41+
"frequency_penalty": 1
42+
}
43+
headers = {
44+
"Authorization": f"Bearer {api_key}",
45+
"Content-Type": "application/json"
46+
}
47+
48+
response = requests.request("POST", url, json=payload, headers=headers)
49+
if response.status_code == 200 and response.text:
50+
return response.text
51+
else:
52+
print(f"{response.status_code} - {response.text}")
53+
return "Failed to query perplexity"

agentstack/tools/perplexity.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "perplexity",
3+
"package": "",
4+
"env": "PERPLEXITY_API_KEY=pplx-...",
5+
"tools": ["query_perplexity"]
6+
}

agentstack/tools/tools.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@
4242
"web-retrieval": [{
4343
"name": "exa",
4444
"url": "https://exa.ai"
45+
}],
46+
"search": [{
47+
"name": "perplexity",
48+
"url": "https://perplexity.ai"
4549
}]
4650
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "agentstack"
7-
version = "0.1.8-dev9"
7+
version = "0.1.9"
88
description = "The fastest way to build robust AI agents"
99
authors = [
1010
{ name="Braelyn Boynton", email="[email protected]" }

0 commit comments

Comments
 (0)