2023-02-08 15:46:34 +00:00
|
|
|
/**
|
|
|
|
* Module with functions used for AI formula assistance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {delay} from 'app/common/delay';
|
|
|
|
import log from 'app/server/lib/log';
|
2023-03-15 08:52:17 +00:00
|
|
|
import fetch from 'node-fetch';
|
2023-02-08 15:46:34 +00:00
|
|
|
|
2023-03-15 08:52:17 +00:00
|
|
|
export const DEPS = { fetch };
|
2023-02-08 15:46:34 +00:00
|
|
|
|
|
|
|
export async function sendForCompletion(prompt: string): Promise<string> {
|
|
|
|
let completion: string|null = null;
|
2023-03-15 08:52:17 +00:00
|
|
|
let retries: number = 0;
|
2023-03-23 18:22:28 +00:00
|
|
|
const openApiKey = process.env.OPENAI_API_KEY;
|
|
|
|
const model = process.env.COMPLETION_MODEL || "text-davinci-002";
|
|
|
|
|
2023-03-15 08:52:17 +00:00
|
|
|
while(retries++ < 3) {
|
|
|
|
try {
|
2023-03-23 18:22:28 +00:00
|
|
|
if (openApiKey) {
|
|
|
|
completion = await sendForCompletionOpenAI(prompt, openApiKey, model);
|
2023-03-15 08:52:17 +00:00
|
|
|
}
|
|
|
|
if (process.env.HUGGINGFACE_API_KEY) {
|
|
|
|
completion = await sendForCompletionHuggingFace(prompt);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} catch(e) {
|
|
|
|
await delay(1000);
|
|
|
|
}
|
2023-02-08 15:46:34 +00:00
|
|
|
}
|
|
|
|
if (completion === null) {
|
|
|
|
throw new Error("Please set OPENAI_API_KEY or HUGGINGFACE_API_KEY (and optionally COMPLETION_MODEL)");
|
|
|
|
}
|
|
|
|
log.debug(`Received completion:`, {completion});
|
|
|
|
completion = completion.split(/\n {4}[^ ]/)[0];
|
|
|
|
return completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-23 18:22:28 +00:00
|
|
|
async function sendForCompletionOpenAI(prompt: string, apiKey: string, model = "text-davinci-002") {
|
2023-02-08 15:46:34 +00:00
|
|
|
if (!apiKey) {
|
|
|
|
throw new Error("OPENAI_API_KEY not set");
|
|
|
|
}
|
2023-03-15 08:52:17 +00:00
|
|
|
const response = await DEPS.fetch(
|
2023-02-08 15:46:34 +00:00
|
|
|
"https://api.openai.com/v1/completions",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${apiKey}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
prompt,
|
|
|
|
max_tokens: 150,
|
|
|
|
temperature: 0,
|
|
|
|
// COMPLETION_MODEL of `code-davinci-002` may be better if you have access to it.
|
2023-03-23 18:22:28 +00:00
|
|
|
model,
|
2023-02-08 15:46:34 +00:00
|
|
|
stop: ["\n\n"],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (response.status !== 200) {
|
|
|
|
log.error(`OpenAI API returned ${response.status}: ${await response.text()}`);
|
|
|
|
throw new Error(`OpenAI API returned status ${response.status}`);
|
|
|
|
}
|
|
|
|
const result = await response.json();
|
|
|
|
const completion = result.choices[0].text;
|
|
|
|
return completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendForCompletionHuggingFace(prompt: string) {
|
|
|
|
const apiKey = process.env.HUGGINGFACE_API_KEY;
|
|
|
|
if (!apiKey) {
|
|
|
|
throw new Error("HUGGINGFACE_API_KEY not set");
|
|
|
|
}
|
|
|
|
// COMPLETION_MODEL values I've tried:
|
|
|
|
// - codeparrot/codeparrot
|
|
|
|
// - NinedayWang/PolyCoder-2.7B
|
|
|
|
// - NovelAI/genji-python-6B
|
|
|
|
let completionUrl = process.env.COMPLETION_URL;
|
|
|
|
if (!completionUrl) {
|
|
|
|
if (process.env.COMPLETION_MODEL) {
|
|
|
|
completionUrl = `https://api-inference.huggingface.co/models/${process.env.COMPLETION_MODEL}`;
|
|
|
|
} else {
|
|
|
|
completionUrl = 'https://api-inference.huggingface.co/models/NovelAI/genji-python-6B';
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 08:52:17 +00:00
|
|
|
|
|
|
|
const response = await DEPS.fetch(
|
|
|
|
completionUrl,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Authorization": `Bearer ${apiKey}`,
|
|
|
|
"Content-Type": "application/json",
|
2023-02-08 15:46:34 +00:00
|
|
|
},
|
2023-03-15 08:52:17 +00:00
|
|
|
body: JSON.stringify({
|
|
|
|
inputs: prompt,
|
|
|
|
parameters: {
|
|
|
|
return_full_text: false,
|
|
|
|
max_new_tokens: 50,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (response.status === 503) {
|
|
|
|
log.error(`Sleeping for 10s - HuggingFace API returned ${response.status}: ${await response.text()}`);
|
|
|
|
await delay(10000);
|
2023-02-08 15:46:34 +00:00
|
|
|
}
|
|
|
|
if (response.status !== 200) {
|
|
|
|
const text = await response.text();
|
|
|
|
log.error(`HuggingFace API returned ${response.status}: ${text}`);
|
|
|
|
throw new Error(`HuggingFace API returned status ${response.status}: ${text}`);
|
|
|
|
}
|
|
|
|
const result = await response.json();
|
|
|
|
const completion = result[0].generated_text;
|
|
|
|
return completion.split('\n\n')[0];
|
|
|
|
}
|