NodeJS OpenAI API密钥放在哪里

lqfhib0f  于 2023-10-17  发布在  Node.js
关注(0)|答案(2)|浏览(128)

我把一些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
oewdyzsn

oewdyzsn1#

TypeError: Configuration is not a constructor

Configuration在以前的版本中使用。例如

"openai": "^3.3.1"

但随着最新的包裹

"openai": "^4.12.1"

Configuration不存在。这就是为什么你得到类型错误。
如果设置环境变量process.env.OPENAI_API_KEY并加载它们

import { config } from "dotenv";
config();

该env变量将被自动选取。这就足够了

const embeddings = new OpenAIEmbeddings();
ivqmmu1c

ivqmmu1c2#

其中一个可能是你的答案:

import { OpenAIEmbeddings } from "langchain/embeddings/openai";

const embeddings = new OpenAIEmbeddings({
  azureOpenAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY
  azureOpenAIApiVersion: "YOUR-API-VERSION", // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION
  azureOpenAIApiInstanceName: "{MY_INSTANCE_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME
  azureOpenAIApiDeploymentName: "{DEPLOYMENT_NAME}", // In Node.js defaults to process.env.AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME
});

Azure OpenAI Docs
你应该看看openai的API auth文档,似乎是一个有用的资源JS OpenAI Docs
也就是说,请确保您已经根据文档安装并导入了所有内容:

import { Configuration, OpenAIApi } from "openai";
...
const configuration = new Configuration({
    organization: "YOUR_ORG_ID",
    apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.listEngines();
...

(另外,你应该注意process.env.<some_key>的用法。私钥永远不应该在代码本身中定义。它们通常作为环境变量提供。)

相关问题