[英]Await in a loop. (again)
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.该项目是使用 gpt3 训练一个聊天机器人,我正在尝试嵌入。
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.根据我的测试,getEmbeddings() 似乎返回了一个值。 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);
})
}
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.要么使用async
/ await
,要么使用 Promises 的原生then
模式,但尽量避免同时使用这两种模式——它会让人感到困惑,这就是你出错的地方。 Your return
statement returns from the then
method, but not from the outer getEmbedding
method itself.您的return
语句从then
方法返回,而不是从外部getEmbedding
方法本身返回。
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有两种方法可以解决这个问题 - 一种是返回axios.post
的结果,但另一种更容易阅读的方法始终使用async
/ await
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.