supermemoryai / opensearch-ai

SearchGPT / Perplexity clone, but personalised for you.
https://opensearch-ai.pages.dev
944 stars 127 forks source link

Add Checks for Missing API Keys and Improve Error Logging #23

Closed Arindam200 closed 3 months ago

Arindam200 commented 3 months ago

Description:

Enhance API integration by checking for missing API keys and improving error logging for better issue identification and graceful failure.

Improvements:

  1. Check for Missing API Keys:

    • Ensure required API keys are present before making requests.
    • Provide clear error messages when keys are missing.
  2. Improve Error Logging:

    • Include detailed error information.
    • Log errors in a structured format.

Improved Code:

const openaiApiKey = process.env.OPENAI_API_KEY;
const mem0ApiKey = process.env.MEM0_API_KEY;

if (!openaiApiKey || !mem0ApiKey) {
  console.error('Missing API keys:', {
    openaiApiKey: !!openaiApiKey,
    mem0ApiKey: !!mem0ApiKey,
  });
  return new Response('Missing API keys', { status: 500 });
}

const mem0Response = await fetch('https://api.mem0.ai/v1/memories/search/', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Token ${mem0ApiKey}`,
  },
  body: JSON.stringify({
    query: `What do you know about ${body.input}`,
    user_id: user.user.email,
  }),
});

if (!mem0Response.ok) {
  const errorText = await mem0Response.text();
  console.error('Error fetching memories:', {
    status: mem0Response.status,
    statusText: mem0Response.statusText,
    errorText: errorText,
  });
  return new Response('Error fetching memories', { status: 500 });
}

Note:

I want to work on this