-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (81 loc) · 3.48 KB
/
main.py
File metadata and controls
103 lines (81 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import requests
import time
import re
from src.agent.capability import MatchingCapability
from src.main import AgentWorker
from src.agent.capability_worker import CapabilityWorker
class PerplexityWebSearchCapability(MatchingCapability):
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
#{{register capability}}
async def give_advice(self):
# Introduce the web search assistant
msg = await self.capability_worker.wait_for_complete_transcription()
self.worker.editor_logging_handler.error(f"User said: {msg}")
api_key = "YOUR_API_KEY"
INTRO_PROMPT = "Let me check that for you real quick"
await self.capability_worker.speak(INTRO_PROMPT)
# Create the request payload (matches your cURL example)
payload = {
"model": "sonar-pro", # sonar or sonar-pro both work
"temperature": 0.2,
"disable_runs": False,
"top_p": 0.9,
"max_tokens": 150,
"messages": [
{
"role": "system",
"content": (
"""
Give a short, clear answer in simple spoken language.
Do not use symbols, citations, or abbreviations.
"""
)
},
{
"role": "user",
"content": f"{msg}"
}
]
}
start_time = time.time()
# Send the request to Perplexity
response = requests.post(
"https://api.perplexity.ai/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json=payload
)
# ✅ End API timer
end_time = time.time()
response_time = round(end_time - start_time, 3)
# ✅ Log API response time
self.worker.editor_logging_handler.info(f"⏱️ API Response Time: {response_time} seconds")
# Log response status and raw text
self.worker.editor_logging_handler.info(f"📡 Response Status: {response.status_code}")
self.worker.editor_logging_handler.info(f"🧾 Raw Response Text:\n{response.text}")
# Parse JSON response safely
try:
result = response.json()
self.worker.editor_logging_handler.info(f"✅ Parsed JSON Response:\n{json.dumps(result, indent=2)}")
except Exception as e:
self.worker.editor_logging_handler.info(f"❌ Failed to parse JSON: {e}")
result = {}
# Extract the assistant’s message (final summary)
search_result = result.get("choices", [{}])[0].get("message", {}).get("content", "Sorry, I couldn't find anything.")
search_result = re.sub(r"\[\d+\]", "", search_result)
search_result = search_result.replace(" ", " ").strip()
# Speak the final summarized result
await self.capability_worker.speak("Here's what I found:")
await self.capability_worker.speak(search_result)
# Resume the normal workflow
self.capability_worker.resume_normal_flow()
def call(self, worker: AgentWorker):
# Initialize the worker and capability worker
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
# Start the advisor functionality
self.worker.session_tasks.create(self.give_advice())