refactor(DALL-E.js): remove unused genAzureEndpoint import and commented code (#506)

feat(DALL-E.js): add getApiKey method to retrieve DALLE_API_KEY from environment variable
This commit is contained in:
Danny Avila 2023-06-13 08:58:09 -04:00 committed by GitHub
parent 72e9828b76
commit ee52533339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
// To use this tool, you must pass in a configured OpenAIApi object. // To use this tool, you must pass in a configured OpenAIApi object.
const fs = require('fs'); const fs = require('fs');
const { Configuration, OpenAIApi } = require('openai'); const { Configuration, OpenAIApi } = require('openai');
const { genAzureEndpoint } = require('../../../utils/genAzureEndpoints'); // const { genAzureEndpoint } = require('../../../utils/genAzureEndpoints');
const { Tool } = require('langchain/tools'); const { Tool } = require('langchain/tools');
const saveImageFromUrl = require('./saveImageFromUrl'); const saveImageFromUrl = require('./saveImageFromUrl');
const path = require('path'); const path = require('path');
@ -11,31 +11,31 @@ class OpenAICreateImage extends Tool {
constructor(fields = {}) { constructor(fields = {}) {
super(); super();
let apiKey = fields.OPENAI_API_KEY || process.env.OPENAI_API_KEY; let apiKey = fields.DALLE_API_KEY || this.getApiKey();
let azureKey = fields.AZURE_OPENAI_API_KEY || process.env.AZURE_OPENAI_API_KEY; // let azureKey = fields.AZURE_OPENAI_API_KEY || process.env.AZURE_OPENAI_API_KEY;
let config = { apiKey }; let config = { apiKey };
if (azureKey) { // if (azureKey) {
apiKey = azureKey; // apiKey = azureKey;
const azureConfig = { // const azureConfig = {
apiKey, // apiKey,
azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME || fields.azureOpenAIApiInstanceName, // azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME || fields.azureOpenAIApiInstanceName,
azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME || fields.azureOpenAIApiDeploymentName, // azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME || fields.azureOpenAIApiDeploymentName,
azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION || fields.azureOpenAIApiVersion // azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION || fields.azureOpenAIApiVersion
}; // };
config = { // config = {
apiKey, // apiKey,
basePath: genAzureEndpoint({ // basePath: genAzureEndpoint({
...azureConfig, // ...azureConfig,
}), // }),
baseOptions: { // baseOptions: {
headers: { 'api-key': apiKey }, // headers: { 'api-key': apiKey },
params: { // params: {
'api-version': azureConfig.azureOpenAIApiVersion // this might change. I got the current value from the sample code at https://oai.azure.com/portal/chat // 'api-version': azureConfig.azureOpenAIApiVersion // this might change. I got the current value from the sample code at https://oai.azure.com/portal/chat
} // }
} // }
}; // };
} // }
this.openaiApi = new OpenAIApi(new Configuration(config)); this.openaiApi = new OpenAIApi(new Configuration(config));
this.name = 'dall-e'; this.name = 'dall-e';
this.description = `You can generate images with 'dall-e'. This tool is exclusively for visual content. this.description = `You can generate images with 'dall-e'. This tool is exclusively for visual content.
@ -46,11 +46,14 @@ Guidelines:
"Subject: [subject], Style: [style], Color: [color], Details: [details], Emotion: [emotion]" "Subject: [subject], Style: [style], Color: [color], Details: [details], Emotion: [emotion]"
- Generate images only once per human query unless explicitly requested by the user`; - Generate images only once per human query unless explicitly requested by the user`;
} }
// "Subject": "Mona Lisa",
// "Style": "Chinese traditional painting", getApiKey() {
// "Color": "Mainly wash tones of ink, with small color blocks in some parts", const apiKey = process.env.DALLE_API_KEY || '';
// "Details": "Mona Lisa should have long hair, a silk dress, holding a fan. The background should have mountains and trees.", if (!apiKey) {
// "Emotion": "Serene and elegant" throw new Error('Missing DALLE_API_KEY environment variable.');
}
return apiKey;
}
replaceUnwantedChars(inputString) { replaceUnwantedChars(inputString) {
return inputString.replace(/\r\n|\r|\n/g, ' ').replace('"', '').trim(); return inputString.replace(/\r\n|\r|\n/g, ' ').replace('"', '').trim();