Elliott-Chong/chatpdf-yt

Cannot read properties of undefined (reading '0')

msimoni18 opened this issue · 4 comments

Around 3:24:00, we test the app to make sure it is working. At this point, we've integrated embedding the document we upload so it can be used in chatGPT.

One the initial pdf upload, I am getting the following error from embeddings.ts:

TypeError: Cannot read properties of undefined (reading '0')
import { OpenAIApi, Configuration } from "openai-edge";

const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(config);

export async function getEmbeddings(text: string) {
  try {
    const response = await openai.createEmbedding({
      model: "text-embedding-ada-002",
      input: text.replace("/\n/g", " "),
    });

    const result = await response.json();
    return result.data[0].embedding as number[];
  } catch (error) {
    console.log("Error calling OpenAI embeddings api", error);
    throw error;
  }
}

As far as I can tell, my file is the same was what is on the repo. After the initial load, result.data is no longer undefined:

{
  object: 'list',
  data: [ { object: 'embedding', index: 0, embedding: [Array] } ],
  model: 'text-embedding-ada-002-v2',
  usage: { prompt_tokens: 187, total_tokens: 187 }
}

How can I fix this issue?

getting the same error

In my case, this was due to insufficient funds in my openai account. Your trial funds might have expired and therefore need to add more funds to your account.

If that solution does not work. Try to console log the response status and the result / response.json() to see if it contains some more information. The error might also contain useful info. Here is my configured embeddings.ts file:

import { OpenAIApi, Configuration } from 'openai-edge';

const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(config);

export async function getEmbeddings(text: string) {
  try {
    const response = await openai.createEmbedding({
      model: 'text-embedding-ada-002',
      input: text.replace(/\n/g, ' '),
    });
    console.log('API Response Status:', response.status);
    const result = await response.json();
    console.log('API Response:', result);

    if (result.error) {
      console.error('OpenAI API Error:', result.error);
      throw new Error('OpenAI API Error');
    }

    return result.data[0].embedding as number[];
  } catch (error) {
    console.log('error calling openai embeddings api', error);
    throw error;
  }
}

In my case, this was due to insufficient funds in my openai account. Your trial funds might have expired and therefore need to add more funds to your account.

If that solution does not work. Try to console log the response status and the result / response.json() to see if it contains some more information. The error might also contain useful info. Here is my configured embeddings.ts file:

import { OpenAIApi, Configuration } from 'openai-edge';

const config = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(config);

export async function getEmbeddings(text: string) {
  try {
    const response = await openai.createEmbedding({
      model: 'text-embedding-ada-002',
      input: text.replace(/\n/g, ' '),
    });
    console.log('API Response Status:', response.status);
    const result = await response.json();
    console.log('API Response:', result);

    if (result.error) {
      console.error('OpenAI API Error:', result.error);
      throw new Error('OpenAI API Error');
    }

    return result.data[0].embedding as number[];
  } catch (error) {
    console.log('error calling openai embeddings api', error);
    throw error;
  }
}

@Mariuble

Thank you for the answer! That was my issue as well.

THIS IS DUE TO INSUFFICIENT CREDIT BALANCE IN YOUR OPENAI ACCOUNT