-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperplexity_ai.py
More file actions
132 lines (116 loc) · 4.2 KB
/
Copy pathperplexity_ai.py
File metadata and controls
132 lines (116 loc) · 4.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import json
import openai
from datetime import datetime, timedelta
def analyze_data(products):
if len(products) < 2:
raise ValueError("Not enough data to analyze")
analysis_data = {
"total_records": len(products),
"date_range": {
"earliest": products[-1]["timestamp"],
"latest": products[0]["timestamp"]
},
"price_history": []
}
for product in reversed(products):
analysis_data["price_history"].append({
"timestamp": product["timestamp"],
"price": product["price"],
"currency": product["currency"]
})
prices = [p["price"] for p in analysis_data["price_history"]]
analysis_data["statistics"] = {
"min_price": min(prices),
"max_price": max(prices),
"current_price": prices[-1],
"price_range": max(prices) - min(prices),
"price_variance": sum((p - sum(prices)/len(prices))**2 for p in prices) / len(prices)
}
prompt = f"""
Analyze the following product price data and provide insights in JSON format:
Product Data:
{json.dumps(analysis_data, indent=2)}
Please analyze this data and return a JSON response with the following structure:
{{
"anomalies": [
{{
"type": "price_spike|price_drop|unusual_pattern",
"description": "Description of the anomaly",
"severity": "low|medium|high",
"timestamp": "When it occurred",
"confidence": 0.85
}}
],
"trends": [
{{
"type": "increasing|decreasing|stable|volatile",
"description": "Summary of the trend",
"percentage_change": -5.2,
"time_period": "last_week|last_month|overall",
"confidence": 0.90
}}
],
"patterns": [
{{
"type": "seasonal|weekly|daily|random",
"description": "Description of the pattern",
"frequency": "daily|weekly|monthly",
"confidence": 0.75
}}
],
"recommendations": [
"Actionable recommendation based on analysis"
]
}}
Focus on:
1. Detect unusual price changes (anomalies) - look for sudden spikes or drops
2. Identify trends - overall price direction and percentage changes
3. Classify patterns - seasonal, weekly, or other recurring patterns
4. Provide actionable insights
Return only valid JSON, no additional text.
"""
try:
client = openai.OpenAI(api_key="<perplexity.ai API KEY>", base_url="https://api.perplexity.ai")
response = client.chat.completions.create(
model="sonar-pro",
messages=[
{
"role": "system",
"content": "You are a data analyst specializing in price analysis. Provide accurate, data-driven insights in JSON format."
},
{
"role": "user",
"content": prompt
}
],
temperature=0.1,
max_tokens=2000
)
analysis_result = json.loads(response.choices[0].message.content)
analysis_result["metadata"] = {
"analysis_timestamp": datetime.now().isoformat(),
"data_points_analyzed": analysis_data["total_records"],
"model_used": "sonar-pro"
}
return analysis_result
except openai.AuthenticationError:
return {
"error": "OpenAI API authentication failed. Please check your API key.",
"anomalies": [],
"trends": [],
"patterns": []
}
except json.JSONDecodeError:
return {
"error": "Failed to parse OpenAI response. The model may have returned invalid JSON.",
"anomalies": [],
"trends": [],
"patterns": []
}
except Exception as e:
return {
"error": f"Analysis failed: {str(e)}",
"anomalies": [],
"trends": [],
"patterns": []
}