Spaces:
Runtime error
Runtime error
Upload pages/api/ai/chat.js with huggingface_hub
Browse files- pages/api/ai/chat.js +89 -0
pages/api/ai/chat.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
+
|
| 3 |
+
export default async function handler(req, res) {
|
| 4 |
+
if (req.method !== 'POST') {
|
| 5 |
+
return res.status(405).json({ error: 'Method not allowed' });
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
const {
|
| 9 |
+
messages,
|
| 10 |
+
spotifyContext,
|
| 11 |
+
apiKey,
|
| 12 |
+
provider,
|
| 13 |
+
model
|
| 14 |
+
} = req.body;
|
| 15 |
+
|
| 16 |
+
if (!apiKey) {
|
| 17 |
+
return res.status(401).json({ error: 'Missing API Key' });
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
// Construct system prompt with Spotify Data
|
| 21 |
+
const systemPrompt = `
|
| 22 |
+
You are an expert music recommendation AI with deep knowledge of all genres, eras, and artists.
|
| 23 |
+
You have access to the user's Spotify data. Use this data to personalize your recommendations.
|
| 24 |
+
|
| 25 |
+
User's Spotify Data:
|
| 26 |
+
Profile: ${JSON.stringify(spotifyContext.profile)}
|
| 27 |
+
Top Tracks: ${JSON.stringify(spotifyContext.topTracks?.map(t => ({name: t.name, artist: t.artists[0].name, album: t.album.name})))}
|
| 28 |
+
Top Artists: ${JSON.stringify(spotifyContext.topArtists?.map(a => a.name))}
|
| 29 |
+
|
| 30 |
+
Instructions:
|
| 31 |
+
1. Analyze the user's taste based on their top tracks and artists.
|
| 32 |
+
2. Provide specific song or artist recommendations that align with their taste but also introduce variety.
|
| 33 |
+
3. If asked for insights, analyze their listening patterns (genres, energy, era).
|
| 34 |
+
4. Be conversational, helpful, and concise.
|
| 35 |
+
5. If you recommend a song, format it like this: **Song Name** by Artist.
|
| 36 |
+
6. Do NOT recommend songs that are already in their "Top Tracks" list unless they ask for similar ones.
|
| 37 |
+
`;
|
| 38 |
+
|
| 39 |
+
try {
|
| 40 |
+
let apiUrl = '';
|
| 41 |
+
let headers = {
|
| 42 |
+
'Content-Type': 'application/json',
|
| 43 |
+
'Authorization': `Bearer ${apiKey}`
|
| 44 |
+
};
|
| 45 |
+
let body = {};
|
| 46 |
+
|
| 47 |
+
if (provider === 'openai' || provider === 'custom') {
|
| 48 |
+
apiUrl = provider === 'openai'
|
| 49 |
+
? 'https://api.openai.com/v1/chat/completions'
|
| 50 |
+
: model; // Assuming model field holds the endpoint for custom
|
| 51 |
+
|
| 52 |
+
body = {
|
| 53 |
+
model: provider === 'custom' ? 'model-name-placeholder' : model,
|
| 54 |
+
messages: [
|
| 55 |
+
{ role: 'system', content: systemPrompt },
|
| 56 |
+
...messages
|
| 57 |
+
],
|
| 58 |
+
temperature: 0.7,
|
| 59 |
+
};
|
| 60 |
+
} else if (provider === 'anthropic') {
|
| 61 |
+
apiUrl = 'https://api.anthropic.com/v1/messages';
|
| 62 |
+
headers['anthropic-version'] = '2023-06-01';
|
| 63 |
+
body = {
|
| 64 |
+
model: model,
|
| 65 |
+
max_tokens: 1024,
|
| 66 |
+
system: systemPrompt,
|
| 67 |
+
messages: messages
|
| 68 |
+
};
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
const response = await axios.post(apiUrl, body, { headers });
|
| 72 |
+
|
| 73 |
+
let content = '';
|
| 74 |
+
if (provider === 'anthropic') {
|
| 75 |
+
content = response.data.content[0].text;
|
| 76 |
+
} else {
|
| 77 |
+
content = response.data.choices[0].message.content;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
res.status(200).json({ content });
|
| 81 |
+
|
| 82 |
+
} catch (error) {
|
| 83 |
+
console.error('AI API Error:', error.response?.data || error.message);
|
| 84 |
+
res.status(500).json({
|
| 85 |
+
error: 'Failed to get AI response',
|
| 86 |
+
details: error.response?.data?.error?.message || error.message
|
| 87 |
+
});
|
| 88 |
+
}
|
| 89 |
+
}
|