refactored into key reader

This commit is contained in:
MaxRobinsonTheGreat 2024-05-30 18:00:48 -05:00
parent e623a6925c
commit 24a6370332
6 changed files with 38 additions and 38 deletions

View file

@ -14,7 +14,7 @@ This project allows an AI model to write/execute code on your computer that may
## Installation
Rename `keys.example.json` to `keys.json` and fill in the desired API keys
Rename `keys.example.json` to `keys.json` and fill in your API keys, and you can set the desired model in `andy.json` or other profiles.
| API | Config Variable | Example Model name | Docs |
|------|------|------|------|
| OpenAI | `OPENAI_API_KEY` | `gpt-3.5-turbo` | [docs](https://platform.openai.com/docs/models) | (optionally add `OPENAI_ORG_ID`)

View file

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

View file

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

View file

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

View file

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

24
src/utils/keys.js Normal file
View file

@ -0,0 +1,24 @@
import { readFileSync } from 'fs';
let keys = {};
try {
const data = readFileSync('./keys.json', 'utf8');
keys = JSON.parse(data);
} catch (err) {
console.warn('keys.json not found. Defaulting to environment variables.'); // still works with local models
}
export function getKey(name) {
let key = keys[name];
if (!key) {
key = process.env[name];
}
if (!key) {
throw new Error(`API key "${name}" not found in keys.json or environment variables!`);
}
return keys[name];
}
export function hasKey(name) {
return keys[name] || process.env[name];
}