Spaces:
Configuration error

File size: 1,259 Bytes
420bd16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const API_URL = 'https://api.openai.com/v1/chat/completions';

export const generateMindMap = async (prompt, model = 'gpt-3.5-turbo', temperature = 0.7) => {
  const openaiKey = process.env.REACT_APP_OPENAI_API_KEY;
  if (!openaiKey) {
    throw new Error('OpenAI API key is not configured');
  }

  try {
    const response = await fetch(API_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${openaiKey}`
      },
      body: JSON.stringify({
        model: model,
        messages: [
          {
            role: 'system',
            content: 'You are an expert at creating mind maps. Generate a mind map structure for the given topic.'
          },
          {
            role: 'user',
            content: prompt
          }
        ],
        temperature: temperature,
        max_tokens: 2000
      })
    });

    if (!response.ok) {
      throw new Error(`API request failed: ${response.statusText}`);
    }

    const data = await response.json();
    return {
      mindmapText: data.choices[0].message.content,
      visualization: data.choices[0].message.content
    };
  } catch (error) {
    console.error('Error generating mind map:', error);
    throw error;
  }
};