Spaces:
Running
Running
🍌🐒
commited on
Commit
·
02031df
1
Parent(s):
d7fcbbf
Add: Perplexity AI support with multiple models
Browse files- app.py +16 -1
- requirements.txt +2 -1
- src/agents.py +28 -0
- src/config.py +6 -1
app.py
CHANGED
|
@@ -81,7 +81,22 @@ def create_gradio_interface():
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
model_dropdown = gr.Dropdown(
|
| 84 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
value="gpt-3.5-turbo",
|
| 86 |
label="Model"
|
| 87 |
)
|
|
|
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
model_dropdown = gr.Dropdown(
|
| 84 |
+
choices=[
|
| 85 |
+
# OpenAI Models
|
| 86 |
+
"gpt-3.5-turbo",
|
| 87 |
+
"gpt-4",
|
| 88 |
+
# Anthropic Models
|
| 89 |
+
"claude-3-opus",
|
| 90 |
+
# Perplexity Current Models
|
| 91 |
+
"sonar-reasoning-pro",
|
| 92 |
+
"sonar-reasoning",
|
| 93 |
+
"sonar-pro",
|
| 94 |
+
"sonar",
|
| 95 |
+
# Perplexity Legacy Models
|
| 96 |
+
"llama-3.1-sonar-small-128k-online",
|
| 97 |
+
"llama-3.1-sonar-large-128k-online",
|
| 98 |
+
"llama-3.1-sonar-huge-128k-online"
|
| 99 |
+
],
|
| 100 |
value="gpt-3.5-turbo",
|
| 101 |
label="Model"
|
| 102 |
)
|
requirements.txt
CHANGED
|
@@ -22,4 +22,5 @@ openai>=1.12.0
|
|
| 22 |
anthropic>=0.18.1
|
| 23 |
duckduckgo-search>=4.4.3
|
| 24 |
orjson>=3.9.15
|
| 25 |
-
python-dotenv>=1.0.0
|
|
|
|
|
|
| 22 |
anthropic>=0.18.1
|
| 23 |
duckduckgo-search>=4.4.3
|
| 24 |
orjson>=3.9.15
|
| 25 |
+
python-dotenv>=1.0.0
|
| 26 |
+
perplexity-python>=0.1.1
|
src/agents.py
CHANGED
|
@@ -3,6 +3,7 @@ from typing import Optional
|
|
| 3 |
import openai
|
| 4 |
import anthropic
|
| 5 |
from duckduckgo_search import DDGS
|
|
|
|
| 6 |
|
| 7 |
class Agent:
|
| 8 |
def __init__(self, base_model: str = "gpt-3.5-turbo", search_engine: str = "duckduckgo"):
|
|
@@ -14,6 +15,8 @@ class Agent:
|
|
| 14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
elif "claude" in base_model:
|
| 16 |
self.claude = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def execute(self, message: str, project_name: str) -> Optional[str]:
|
| 19 |
try:
|
|
@@ -22,6 +25,8 @@ class Agent:
|
|
| 22 |
response = self._process_with_gpt(message)
|
| 23 |
elif "claude" in self.base_model:
|
| 24 |
response = self._process_with_claude(message)
|
|
|
|
|
|
|
| 25 |
else:
|
| 26 |
response = "Unsupported model selected"
|
| 27 |
|
|
@@ -50,6 +55,29 @@ class Agent:
|
|
| 50 |
)
|
| 51 |
return response.content[0].text
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def _search_web(self, query: str, num_results: int = 5) -> list:
|
| 54 |
if self.search_engine == "duckduckgo":
|
| 55 |
with DDGS() as ddgs:
|
|
|
|
| 3 |
import openai
|
| 4 |
import anthropic
|
| 5 |
from duckduckgo_search import DDGS
|
| 6 |
+
from perplexity import Perplexity
|
| 7 |
|
| 8 |
class Agent:
|
| 9 |
def __init__(self, base_model: str = "gpt-3.5-turbo", search_engine: str = "duckduckgo"):
|
|
|
|
| 15 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 16 |
elif "claude" in base_model:
|
| 17 |
self.claude = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 18 |
+
elif "sonar" in base_model:
|
| 19 |
+
self.perplexity = Perplexity(api_key=os.getenv("PERPLEXITY_API_KEY"))
|
| 20 |
|
| 21 |
def execute(self, message: str, project_name: str) -> Optional[str]:
|
| 22 |
try:
|
|
|
|
| 25 |
response = self._process_with_gpt(message)
|
| 26 |
elif "claude" in self.base_model:
|
| 27 |
response = self._process_with_claude(message)
|
| 28 |
+
elif "sonar" in self.base_model:
|
| 29 |
+
response = self._process_with_perplexity(message)
|
| 30 |
else:
|
| 31 |
response = "Unsupported model selected"
|
| 32 |
|
|
|
|
| 55 |
)
|
| 56 |
return response.content[0].text
|
| 57 |
|
| 58 |
+
def _process_with_perplexity(self, message: str) -> str:
|
| 59 |
+
# Map model names to Perplexity models
|
| 60 |
+
model_mapping = {
|
| 61 |
+
# Current models
|
| 62 |
+
"sonar-reasoning-pro": "sonar-reasoning-pro", # 127k context, 8k output
|
| 63 |
+
"sonar-reasoning": "sonar-reasoning", # 127k context
|
| 64 |
+
"sonar-pro": "sonar-pro", # 200k context, 8k output
|
| 65 |
+
"sonar": "sonar", # 127k context
|
| 66 |
+
# Legacy models (will be deprecated after 2/22/2025)
|
| 67 |
+
"llama-3.1-sonar-small-128k-online": "llama-3.1-sonar-small-128k-online",
|
| 68 |
+
"llama-3.1-sonar-large-128k-online": "llama-3.1-sonar-large-128k-online",
|
| 69 |
+
"llama-3.1-sonar-huge-128k-online": "llama-3.1-sonar-huge-128k-online"
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
# Use the mapped model or default to sonar
|
| 73 |
+
model = model_mapping.get(self.base_model, "sonar")
|
| 74 |
+
|
| 75 |
+
response = self.perplexity.chat.create(
|
| 76 |
+
model=model,
|
| 77 |
+
messages=[{"role": "user", "content": message}]
|
| 78 |
+
)
|
| 79 |
+
return response.choices[0].message.content
|
| 80 |
+
|
| 81 |
def _search_web(self, query: str, num_results: int = 5) -> list:
|
| 82 |
if self.search_engine == "duckduckgo":
|
| 83 |
with DDGS() as ddgs:
|
src/config.py
CHANGED
|
@@ -19,10 +19,12 @@ class Config:
|
|
| 19 |
"BING": os.getenv("BING_API_KEY", ""),
|
| 20 |
"GOOGLE_SEARCH": os.getenv("GOOGLE_API_KEY", ""),
|
| 21 |
"GOOGLE_SEARCH_ENGINE_ID": os.getenv("GOOGLE_SEARCH_ENGINE_ID", ""),
|
|
|
|
| 22 |
},
|
| 23 |
"API_ENDPOINTS": {
|
| 24 |
"BING": "https://api.bing.microsoft.com/v7.0/search",
|
| 25 |
"GOOGLE": "https://www.googleapis.com/customsearch/v1",
|
|
|
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
|
@@ -41,4 +43,7 @@ class Config:
|
|
| 41 |
return self.config["API_KEYS"]["GOOGLE_SEARCH"]
|
| 42 |
|
| 43 |
def get_google_search_engine_id(self):
|
| 44 |
-
return self.config["API_KEYS"]["GOOGLE_SEARCH_ENGINE_ID"]
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"BING": os.getenv("BING_API_KEY", ""),
|
| 20 |
"GOOGLE_SEARCH": os.getenv("GOOGLE_API_KEY", ""),
|
| 21 |
"GOOGLE_SEARCH_ENGINE_ID": os.getenv("GOOGLE_SEARCH_ENGINE_ID", ""),
|
| 22 |
+
"PERPLEXITY": os.getenv("PERPLEXITY_API_KEY", ""),
|
| 23 |
},
|
| 24 |
"API_ENDPOINTS": {
|
| 25 |
"BING": "https://api.bing.microsoft.com/v7.0/search",
|
| 26 |
"GOOGLE": "https://www.googleapis.com/customsearch/v1",
|
| 27 |
+
"PERPLEXITY": "https://api.perplexity.ai",
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
|
|
|
| 43 |
return self.config["API_KEYS"]["GOOGLE_SEARCH"]
|
| 44 |
|
| 45 |
def get_google_search_engine_id(self):
|
| 46 |
+
return self.config["API_KEYS"]["GOOGLE_SEARCH_ENGINE_ID"]
|
| 47 |
+
|
| 48 |
+
def get_perplexity_api_key(self):
|
| 49 |
+
return self.config["API_KEYS"]["PERPLEXITY"]
|