-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
98 lines (83 loc) · 2.85 KB
/
server.js
File metadata and controls
98 lines (83 loc) · 2.85 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
import express from 'express'
import cors from 'cors'
import { config } from 'dotenv'
config() // Load .env file
const app = express()
app.use(cors())
app.use(express.json())
const PERPLEXITY_API_KEY = process.env.PERPLEXITY_API_KEY
// AI Market Analysis endpoint
app.post('/ai/analyze', async (req, res) => {
const { question, description, currentPrice } = req.body
if (!question) {
return res.status(400).json({ error: 'Market question required' })
}
try {
// Use Perplexity API for search + analysis
if (PERPLEXITY_API_KEY) {
const response = await fetch('https://api.perplexity.ai/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${PERPLEXITY_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'llama-3.1-sonar-small-128k-online',
messages: [
{
role: 'system',
content: `You are a prediction market analyst. Analyze the given market and provide:
1. A brief summary of recent news/developments (2-3 sentences)
2. Key factors that could move this market
3. Current sentiment assessment (bullish/bearish/neutral)
4. Risk factors to watch
Be concise and actionable. Focus on what traders need to know.`
},
{
role: 'user',
content: `Analyze this prediction market:
Question: ${question}
${description ? `Description: ${description}` : ''}
Current YES price: ${currentPrice ? Math.round(currentPrice * 100) + '¢' : 'Unknown'}
Search for the latest news and provide your analysis.`
}
],
max_tokens: 500,
temperature: 0.3,
}),
})
const data = await response.json()
if (data.choices?.[0]?.message?.content) {
return res.json({
analysis: data.choices[0].message.content,
sources: data.citations || [],
model: 'perplexity',
timestamp: new Date().toISOString(),
})
}
}
// Fallback: Return a placeholder indicating API key needed
return res.json({
analysis: null,
error: 'AI analysis requires Perplexity API key. Set PERPLEXITY_API_KEY environment variable.',
model: null,
timestamp: new Date().toISOString(),
})
} catch (error) {
console.error('AI Analysis error:', error)
return res.status(500).json({ error: 'Analysis failed', details: error.message })
}
})
// Health check
app.get('/ai/health', (req, res) => {
res.json({
status: 'ok',
perplexityConfigured: !!PERPLEXITY_API_KEY,
timestamp: new Date().toISOString()
})
})
const PORT = process.env.AI_PORT || 3001
app.listen(PORT, () => {
console.log(`🧠 AI Analysis server running on port ${PORT}`)
console.log(`📡 Perplexity API: ${PERPLEXITY_API_KEY ? 'Configured ✓' : 'Not configured'}`)
})