我把一些langchain代码放到一个托管的node.js服务器上,我不知道把我的OpenAPI API密钥放在哪里
const http = require('http');
const cors = require("cors");
const { OpenAI } = require("langchain/llms/openai");
const { PDFLoader } = require("langchain/document_loaders/fs/pdf");
const { FaissStore } = require("langchain/vectorstores/faiss");
const { OpenAIEmbeddings } = require("langchain/embeddings/openai");
const { RecursiveCharacterTextSplitter } = require('langchain/text_splitter');
const fs = require('fs');
OpenAI.api_key = '12345.....'
const processSteps = async () => {
// Step One
const loader = new PDFLoader("documents/document.pdf");
const docs = await loader.load();
// Step Two
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
});
// Step Three
const docOutput = await textSplitter.splitDocuments(docs)
let vectorStore = await FaissStore.fromDocuments(docOutput, new OpenAIEmbeddings());
// Step Four
const directory = "documents";
await vectorStore.save(directory);
}
http.createServer(async function(req, res) {
var ret = "Start"
try {
await processSteps();
ret = "All steps completed successfully";
} catch (error) {
console.error('An error occurred:', error);
ret = "An error occurred: " + error;
}
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end(ret);
}).listen(process.env.PORT);
当我运行代码时,
An error occurred: Error: OpenAI or Azure OpenAI API key not found
如果我把代码写在
Run OpenAI API document example in Node: where do I put sample code?
const configuration = new Configuration({
apiKey: '12345.....',
});
const openai = new OpenAIApi(configuration);
我得到
TypeError: Configuration is not a constructor
2条答案
按热度按时间oewdyzsn1#
Configuration
在以前的版本中使用。例如但随着最新的包裹
Configuration
不存在。这就是为什么你得到类型错误。如果设置环境变量
process.env.OPENAI_API_KEY
并加载它们该env变量将被自动选取。这就足够了
ivqmmu1c2#
其中一个可能是你的答案:
Azure OpenAI Docs
你应该看看openai的API auth文档,似乎是一个有用的资源JS OpenAI Docs。
也就是说,请确保您已经根据文档安装并导入了所有内容:
(另外,你应该注意
process.env.<some_key>
的用法。私钥永远不应该在代码本身中定义。它们通常作为环境变量提供。)