1

This problem seems like a rerun of a nightmare for me. The project is to train a chatbot using gpt3 and I'm experimenting with embeddings.

I've got document, and I'm trying to create embeddings for the different sections. getEmbeddings() appears to return a value according to my testing. However, when my loop ends, I don't seem to have an embedding on my section (there's only one... it costs money to make embeddings so I'm only running the loop once until I get it working).

What am I doing wrong?

begin();

async function begin(){
    let sections = [];

    let inputFileName = "sourceDocument.jsonl";
    let filecontents = fs.readFileSync(inputFileName,{encoding:'utf-8'})
    let inputRows = [] = String(filecontents).split(/\r?\n/);

        for(let i = 0; i < inputRows.length; i++) {
            let row = inputRows[i];
            if(row.trim().length == 0) continue;
            let dataObject = JSON.parse(row);
            
            let text = dataObject.completion;
            let section = new Section();
            let titleend =  text.indexOf("'>")
            
            //console.log(dataObject);
    
            section.title = text.substring(1,titleend);
            section.text = String(text).substring( (titleend + 2), (text.length - 10) );
            section.embedding = await getEmbedding(section.text);
            sections.push(section);
            
            break;
        }

    console.log(sections[0]);   
}

async function getEmbedding(textSample){
    const embeddingModel = "text-search-davinci-doc-001";

    if(DEBUGGING){
        console.log(`DEBUG: getEmbedding(${textSample})`);
    }

    const OPENAI_REQEST_HEADERS = {
        "Content-Type":"application/json",
        "Authorization":`Bearer ${OPENAI_API_KEY}`
    }

    let data = {
        "input"         :   textSample,
        "model"         :   embeddingModel
    };

    let res;
    await axios.post(EMBEDDINGS, data, {headers:OPENAI_REQEST_HEADERS})
    .then((response) => {
        if(DEBUGGING) {
            console.log("   Embedding:  " + response.data.data[0].embedding);
        }
        let embedding = response.data.data[0].embedding;
        return embedding;
    })
    .catch((error) => {
        console.log("Error posting to OpenAI:");
        console.log(error.response.data);
    })
}
  • What do you get when you try to `console.log(sections)` just before the `break;`? – Sinan Yaman Aug 09 '22 at 11:54
  • why json is separated in new lines inside "sourceDocument.jsonl" file ? just put those in a top level array so you can easily parse it like this ```const arrayofObjects = require("./sourceDocument.json")``` – bogdanoff Aug 09 '22 at 11:57
  • I get an array of 1 section, but the embedding is undefined. – user1672528 Aug 09 '22 at 12:32
  • Well `await getEmbedding(...)` returns undefined then, you should debug that section. Nothing wrong with javascript AFAIK. – Sinan Yaman Aug 09 '22 at 12:36
  • 1
    Youve simply forgotten to return the result of `axios.post` from `getEmbedding` - your return is from a `then` not the method itself! – Jamiec Aug 09 '22 at 12:45
  • I added the get embedding code there... am I returning from it improperly or something? I can log the embedding from there. – user1672528 Aug 09 '22 at 12:49

1 Answers1

1

Either use async/await or use the native then pattern of Promises, but try to avoid using both - it gets confusing and thats where you've gone wrong. Your return statement returns from the then method, but not from the outer getEmbedding method itself.

There are 2 ways to solve this - one is to return the result of axios.post but the other, somewhat easier to read method uses async/await throughout

async function getEmbedding(textSample){
    const embeddingModel = "text-search-davinci-doc-001";

    if(DEBUGGING){
        console.log(`DEBUG: getEmbedding(${textSample})`);
    }

    const OPENAI_REQEST_HEADERS = {
        "Content-Type":"application/json",
        "Authorization":`Bearer ${OPENAI_API_KEY}`
    }

    let data = {
        "input"         :   textSample,
        "model"         :   embeddingModel
    };

    try{
        const response = await axios.post(EMBEDDINGS, data, {headers:OPENAI_REQEST_HEADERS});
        if(DEBUGGING) {
            console.log("   Embedding:  " + response.data.data[0].embedding);
        }
        return response.data.data[0].embedding;
    }
    catch(error){
       console.log("Error posting to OpenAI:");
       console.log(error.response.data);
    }
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193