From 8b4ea79b9a57e08a48e79dcd1a781cac335f7fae Mon Sep 17 00:00:00 2001 From: Sam Kemp Date: Mon, 27 May 2024 12:36:29 +0100 Subject: [PATCH] Switched to config.json instead of environment variable --- .gitignore | 3 ++- config.example.json | 7 +++++++ src/models/claude.js | 7 ++++--- src/models/gemini.js | 7 ++++--- src/models/gpt.js | 12 ++++++------ src/models/replicate.js | 7 ++++--- 6 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 config.example.json diff --git a/.gitignore b/.gitignore index 61f81fd..6e6fd2d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ package-lock.json scratch.js bots/**/action-code/** -bots/**/ \ No newline at end of file +bots/**/ +config.json \ No newline at end of file diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..8e4037c --- /dev/null +++ b/config.example.json @@ -0,0 +1,7 @@ +{ + "OPENAI_API_KEY": "", + "OPENAI_ORG_ID": "", + "GEMINI_API_KEY": "", + "ANTHROPIC_API_KEY": "", + "REPLICATE_API_KEY": "" +} \ No newline at end of file diff --git a/src/models/claude.js b/src/models/claude.js index e189a38..754cd7a 100644 --- a/src/models/claude.js +++ b/src/models/claude.js @@ -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); } diff --git a/src/models/gemini.js b/src/models/gemini.js index 61f4f1a..b0d315b 100644 --- a/src/models/gemini.js +++ b/src/models/gemini.js @@ -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) { diff --git a/src/models/gpt.js b/src/models/gpt.js index 0889c31..9f674b9 100644 --- a/src/models/gpt.js +++ b/src/models/gpt.js @@ -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); } diff --git a/src/models/replicate.js b/src/models/replicate.js index ea5a4a9..1059f0f 100644 --- a/src/models/replicate.js +++ b/src/models/replicate.js @@ -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, }); }