CoolFace
Apppublic

Lokrosh/chatty-mcbotface

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
api.js34 linesDownload Raw Back to js
1```javascript2export async function getAIResponse(message) {3    const API_URL = 'https://api.openai.com/v1/chat/completions';4    const API_KEY = 'YOUR_API_KEY'; // Replace with actual key5    6    try {7        const response = await fetch(API_URL, {8            method: 'POST',9            headers: {10                'Content-Type': 'application/json',11                'Authorization': `Bearer ${API_KEY}`12            },13            body: JSON.stringify({14                model: "gpt-3.5-turbo",15                messages: [{16                    role: "system",17                    content: "You are a helpful assistant named Chatty McBotFace."18                }, {19                    role: "user",20                    content: message21                }],22                temperature: 0.723            })24        });25 26        if (!response.ok) throw new Error(`API error: ${response.status}`);27        const data = await response.json();28        return data.choices[0].message.content.trim();29    } catch (error) {30        console.error('API Error:', error);31        throw error;32    }33}34```