对我来说,这个问题似乎是一个噩梦的重演。该项目是使用gpt3训练聊天机器人,我正在试验嵌入式。
我有文档,我正在尝试为不同的部分创建嵌入。根据我的测试,getEmbeddings()似乎返回了一个值。然而,当我的循环结束时,我似乎没有嵌入到我的部分(只有一个.做嵌入要花很多钱,所以我只运行一次循环,直到它开始工作为止)。
我做错了什么?
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);
})
}发布于 2022-08-09 12:50:24
要么使用async/await,要么使用承诺的原生then模式,但尽量避免使用这两种方式--这会让人感到困惑,而这正是您出错的地方。return语句从then方法返回,而不是从外部getEmbedding方法本身返回。
解决这个问题的方法有两种:一种是返回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);
}
}https://stackoverflow.com/questions/73291289
复制相似问题