Switched to config.json instead of environment variable

This commit is contained in:
Sam Kemp 2024-05-27 12:36:29 +01:00
parent ab440ea176
commit 8b4ea79b9a
6 changed files with 27 additions and 16 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@ node_modules/
package-lock.json
scratch.js
bots/**/action-code/**
bots/**/
bots/**/
config.json

7
config.example.json Normal file
View file

@ -0,0 +1,7 @@
{
"OPENAI_API_KEY": "",
"OPENAI_ORG_ID": "",
"GEMINI_API_KEY": "",
"ANTHROPIC_API_KEY": "",
"REPLICATE_API_KEY": ""
}

View file

@ -1,4 +1,5 @@
import Anthropic from '@anthropic-ai/sdk';
import configJson from "../../config.json" assert { type: "json" };
export class Claude {
@ -8,10 +9,10 @@ export class Claude {
let config = {};
if (url)
config.baseURL = url;
if (process.env.ANTHROPIC_API_KEY)
config.apiKey = process.env["ANTHROPIC_API_KEY"];
if (configJson.ANTHROPIC_API_KEY)
config.apiKey = configJson.ANTHROPIC_API_KEY;
else
throw new Error('Anthropic API key missing! Make sure you set your ANTHROPIC_API_KEY environment variable.');
throw new Error('Anthropic API key missing! Make sure you set your ANTHROPIC_API_KEY in your config.json.');
this.anthropic = new Anthropic(config);
}

View file

@ -1,15 +1,16 @@
import { GoogleGenerativeAI } from '@google/generative-ai';
import { toSinglePrompt } from '../utils/text.js';
import configJson from "../../config.json" assert { type: "json" };
export class Gemini {
constructor(model_name, url) {
this.model_name = model_name;
this.url = url;
if (!process.env.GEMINI_API_KEY) {
throw new Error('Gemini API key missing! Make sure you set your GEMINI_API_KEY environment variable.');
if (!configJson.GEMINI_API_KEY) {
throw new Error('Gemini API key missing! Make sure you set your GEMINI_API_KEY in your config.json.');
}
this.genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
this.genAI = new GoogleGenerativeAI(configJson.GEMINI_API_KEY);
}
async sendRequest(turns, systemMessage) {

View file

@ -1,5 +1,5 @@
import OpenAIApi from 'openai';
import configJson from "../../config.json" assert { type: "json" };
export class GPT {
constructor(model_name, url) {
@ -8,12 +8,12 @@ export class GPT {
let config = {};
if (url)
config.baseURL = url;
if (process.env.OPENAI_ORG_ID)
config.organization = process.env.OPENAI_ORG_ID
if (process.env.OPENAI_API_KEY)
config.apiKey = process.env.OPENAI_API_KEY
if (configJson.OPENAI_ORG_ID)
config.organization = configJson.OPENAI_ORG_ID;
if (configJson.OPENAI_API_KEY)
config.apiKey = configJson.OPENAI_API_KEY;
else
throw new Error('OpenAI API key missing! Make sure you set your OPENAI_API_KEY environment variable.');
throw new Error('OpenAI API key missing! Make sure you set your OPENAI_API_KEY in your config.json.');
this.openai = new OpenAIApi(config);
}

View file

@ -1,5 +1,6 @@
import Replicate from 'replicate';
import { toSinglePrompt } from '../utils/text.js';
import configJson from "../../config.json" assert { type: "json" };
// llama, mistral
export class ReplicateAPI {
@ -11,12 +12,12 @@ export class ReplicateAPI {
console.warn('Replicate API does not support custom URLs. Ignoring provided URL.');
}
if (!process.env.REPLICATE_API_KEY) {
throw new Error('Replicate API key missing! Make sure you set your REPLICATE_API_KEY environment variable.');
if (!configJson.REPLICATE_API_KEY) {
throw new Error('Replicate API key missing! Make sure you set your REPLICATE_API_KEY in your config.json.');
}
this.replicate = new Replicate({
auth: process.env.REPLICATE_API_KEY,
auth: configJson.REPLICATE_API_KEY,
});
}