-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperplexity_query.py
More file actions
211 lines (167 loc) · 6.78 KB
/
perplexity_query.py
File metadata and controls
211 lines (167 loc) · 6.78 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
This script is used to query Perplexity API for answers to questions and store results in Supabase.
"""
import os
import requests
from datetime import datetime, timedelta
import pytz
from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
from output_models import AI_Agent_Vote
from utils import get_supabase_client
def create_agent_structured_output(llm,
output_structure,):
"""
Create an agent with structured output.
"""
system_message = """You are given predictions from an AI agent. Structure the response to output schema.
Remove any references quoted like [1], [2], etc. in the reasoning.
For winner selection, keep name of the team as in the question. That is abbreviated name of the team.
For margin selection, responsd with A, B, C, or D as in the question.
Give detailed reasoning for the winner selection and margin selection.
Also, do not give your estimated probability of each option for both questions (winner and margin).
"""
prompt = ChatPromptTemplate.from_messages(
[
("system", system_message),
MessagesPlaceholder(variable_name="messages"),
]
)
llm_chain = llm.with_structured_output(output_structure, strict=True)
output = prompt | llm_chain
return output
def insert_vote_to_supabase(match_id, winner_selection, margin_selection, reasoning):
"""
Insert vote data into Supabase AI_VOTES table.
"""
supabase = get_supabase_client()
data = {
'match_id': match_id,
'winner_selection': winner_selection,
'margin_selection': margin_selection,
'reasoning': reasoning
}
votes_data = [{
'match_id': match_id,
'user_email': 'predictorai01@gmail.com',
'user_name': 'AI Predictor',
'poll_type': 'winner',
'option_voted': winner_selection,
'created_timestamp': datetime.now(pytz.UTC).isoformat()
},
{
'match_id': match_id,
'user_email': 'predictorai01@gmail.com',
'user_name': 'AI Predictor',
'poll_type': 'victory_margin',
'option_voted': margin_selection,
'created_timestamp': datetime.now(pytz.UTC).isoformat()
}
]
try:
result = supabase.table('AI_VOTES').insert(data).execute()
for vote in votes_data:
supabase.table('VOTES').insert(vote).execute()
print("Successfully inserted vote into Supabase")
return result
except Exception as e:
print(f"Error inserting into Supabase: {str(e)}")
raise
# Load environment variables
load_dotenv()
def ask_perplexity(question):
"""
Ask Perplexity API for an answer to a question.
"""
# Get API key from environment variable
api_key = os.getenv('PERPLEXITY_API_KEY')
if not api_key:
raise ValueError("Please set the PERPLEXITY_API_KEY environment variable")
# API endpoint
url = "https://api.perplexity.ai/chat/completions"
# Request headers
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Request payload
payload = {
"model": "sonar-reasoning-pro", # You can change the model as needed
"messages": [
{
"role": "user",
"content": question
}
]
}
try:
response = requests.post(url, json=payload, headers=headers, timeout=300)
response.raise_for_status() # Raise an exception for bad status codes
result = response.json()
return result['choices'][0]['message']['content']
except requests.exceptions.RequestException as e:
return f"Error making request: {str(e)}"
def get_next_match_without_ai_perspective():
"""
Get the next match from MATCHES table that doesn't have an AI perspective yet.
"""
supabase = get_supabase_client()
# Get current time in UTC
current_time = datetime.now(pytz.UTC)
# Get time 24 hours from now
future_time = current_time + timedelta(hours=24)
# Query to get matches that:
# 1. Are in the next 24 hours
# 2. Don't have an AI perspective yet
query = supabase.table('MATCHES').select('*').gte('Poll_Close_Time', current_time.isoformat()).lte('Poll_Close_Time', future_time.isoformat())
# Get all matches in this time range
matches = query.execute()
if not matches.data:
raise ValueError("No matches found in the next 24 hours")
# For each match, check if it has an AI perspective
for match in matches.data:
# Check if there's an AI vote for this match
ai_vote = supabase.table('AI_VOTES').select('*').eq('match_id', match['Match_ID']).execute()
if not ai_vote.data:
return match
raise ValueError("No matches found without AI perspective in the next 24 hours")
def main():
# Get the next match that needs AI perspective
match = get_next_match_without_ai_perspective()
match_id = match['Match_ID']
match_teams = f"{match['Team_1']} vs {match['Team_2']}"
match_date = match['Date']
question = f"""Answer two questions related to Match No {match_id}: {match_teams} on {match_date}.
Question 1: who will win the match?
Question 2: What will be the victory margin? Your options are:\n
Option A: 0-12 runs / 6 or less balls remaining
Option B: 13-25 runs / 7-12 balls remaining
Option C: 26-40 runs / 13-22 balls remaining
Option D: 41+ runs / 23+ balls remaining
Take into account the current form of the teams, the pitch, and the weather conditions.
Also, take into account team composition, players experience in IPL and on the ground where match is being played.
Also, think about latest results of the teams in current IPL season.
Think step by step and reason out your answer. For example, for victory margin, think about expected scores of the teams.
Also, tell your estimated probability of each option for both questions (winner and margin).
"""
print("Asking Perplexity:", question)
print("\nResponse:")
response = ask_perplexity(question)
print(response)
openai_llm = ChatOpenAI(model="gpt-4o-mini", temperature=0, max_tokens=8192)
agent = create_agent_structured_output(openai_llm,
output_structure=AI_Agent_Vote)
final_response = agent.invoke({"messages": [{"role": "user", "content":
f"Question: {question}\n"
f"Response from AI Agent: {response}"}]})
print(final_response)
# Insert the response into Supabase
insert_vote_to_supabase(
match_id=match_id,
winner_selection=final_response.winner_selection,
margin_selection=final_response.margin_selection,
reasoning=final_response.reasoning
)
if __name__ == "__main__":
main()