2024-01-05 02:38:03 +00:00
|
|
|
import { OPENAI_API_BASE_URL } from '$lib/constants';
|
2024-04-14 21:04:24 +00:00
|
|
|
import { promptTemplate } from '$lib/utils';
|
2024-01-05 02:38:03 +00:00
|
|
|
|
2024-05-17 17:30:22 +00:00
|
|
|
export const getOpenAIConfig = async (token: string = '') => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/config`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateOpenAIConfig = async (token: string = '', enable_openai_api: boolean) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/config/update`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
enable_openai_api: enable_openai_api
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
export const getOpenAIUrls = async (token: string = '') => {
|
2024-01-05 02:38:03 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/urls`, {
|
2024-01-05 02:38:03 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
return res.OPENAI_API_BASE_URLS;
|
2024-01-05 02:38:03 +00:00
|
|
|
};
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
export const updateOpenAIUrls = async (token: string = '', urls: string[]) => {
|
2024-01-05 02:38:03 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/urls/update`, {
|
2024-01-05 02:38:03 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2024-03-07 00:13:25 +00:00
|
|
|
urls: urls
|
2024-01-05 02:38:03 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
return res.OPENAI_API_BASE_URLS;
|
2024-01-05 02:38:03 +00:00
|
|
|
};
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
export const getOpenAIKeys = async (token: string = '') => {
|
2024-01-05 02:38:03 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/keys`, {
|
2024-01-05 02:38:03 +00:00
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
return res.OPENAI_API_KEYS;
|
2024-01-05 02:38:03 +00:00
|
|
|
};
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
export const updateOpenAIKeys = async (token: string = '', keys: string[]) => {
|
2024-01-05 02:38:03 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/keys/update`, {
|
2024-01-05 02:38:03 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2024-03-07 00:13:25 +00:00
|
|
|
keys: keys
|
2024-01-05 02:38:03 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
} else {
|
|
|
|
error = 'Server connection failed';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:13:25 +00:00
|
|
|
return res.OPENAI_API_KEYS;
|
2024-01-05 02:38:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getOpenAIModels = async (token: string = '') => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/models`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const models = Array.isArray(res) ? res : res?.data ?? null;
|
|
|
|
|
|
|
|
return models
|
|
|
|
? models
|
2024-05-09 12:25:30 +00:00
|
|
|
.map((model) => ({
|
|
|
|
id: model.id,
|
|
|
|
name: model.name ?? model.id,
|
|
|
|
external: true,
|
|
|
|
custom_info: model.custom_info ?? {}
|
|
|
|
}))
|
2024-01-05 02:38:03 +00:00
|
|
|
.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
})
|
|
|
|
: models;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getOpenAIModelsDirect = async (
|
2023-12-26 19:32:22 +00:00
|
|
|
base_url: string = 'https://api.openai.com/v1',
|
|
|
|
api_key: string = ''
|
|
|
|
) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${base_url}/models`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${api_key}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-01-03 22:33:57 +00:00
|
|
|
const models = Array.isArray(res) ? res : res?.data ?? null;
|
2023-12-26 19:32:22 +00:00
|
|
|
|
|
|
|
return models
|
2024-02-22 12:12:26 +00:00
|
|
|
.map((model) => ({ id: model.id, name: model.name ?? model.id, external: true }))
|
2024-01-04 07:46:49 +00:00
|
|
|
.filter((model) => (base_url.includes('openai') ? model.name.includes('gpt') : true))
|
|
|
|
.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
2023-12-26 19:32:22 +00:00
|
|
|
};
|
2024-01-05 02:54:00 +00:00
|
|
|
|
2024-02-22 12:12:26 +00:00
|
|
|
export const generateOpenAIChatCompletion = async (
|
|
|
|
token: string = '',
|
|
|
|
body: object,
|
|
|
|
url: string = OPENAI_API_BASE_URL
|
2024-04-20 20:11:02 +00:00
|
|
|
): Promise<[Response | null, AbortController]> => {
|
|
|
|
const controller = new AbortController();
|
2024-01-05 02:54:00 +00:00
|
|
|
let error = null;
|
|
|
|
|
2024-02-22 12:12:26 +00:00
|
|
|
const res = await fetch(`${url}/chat/completions`, {
|
2024-04-20 20:11:02 +00:00
|
|
|
signal: controller.signal,
|
2024-01-05 02:54:00 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
}).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
error = err;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-04-20 20:11:02 +00:00
|
|
|
return [res, controller];
|
2024-01-05 02:54:00 +00:00
|
|
|
};
|
2024-02-06 06:51:08 +00:00
|
|
|
|
|
|
|
export const synthesizeOpenAISpeech = async (
|
|
|
|
token: string = '',
|
|
|
|
speaker: string = 'alloy',
|
2024-05-06 07:23:27 +00:00
|
|
|
text: string = '',
|
|
|
|
model: string = 'tts-1'
|
2024-02-06 06:51:08 +00:00
|
|
|
) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/audio/speech`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2024-05-06 07:23:27 +00:00
|
|
|
model: model,
|
2024-02-06 06:51:08 +00:00
|
|
|
input: text,
|
|
|
|
voice: speaker
|
|
|
|
})
|
|
|
|
}).catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
error = err;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
2024-03-26 07:59:57 +00:00
|
|
|
|
|
|
|
export const generateTitle = async (
|
|
|
|
token: string = '',
|
|
|
|
template: string,
|
|
|
|
model: string,
|
|
|
|
prompt: string,
|
|
|
|
url: string = OPENAI_API_BASE_URL
|
|
|
|
) => {
|
|
|
|
let error = null;
|
|
|
|
|
2024-04-14 21:04:24 +00:00
|
|
|
template = promptTemplate(template, prompt);
|
2024-03-26 07:59:57 +00:00
|
|
|
|
|
|
|
console.log(template);
|
|
|
|
|
|
|
|
const res = await fetch(`${url}/chat/completions`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${token}`
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
model: model,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
role: 'user',
|
|
|
|
content: template
|
|
|
|
}
|
|
|
|
],
|
2024-05-06 06:46:50 +00:00
|
|
|
stream: false,
|
|
|
|
// Restricting the max tokens to 50 to avoid long titles
|
2024-05-06 19:58:24 +00:00
|
|
|
max_tokens: 50
|
2024-03-26 07:59:57 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(async (res) => {
|
|
|
|
if (!res.ok) throw await res.json();
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
if ('detail' in err) {
|
|
|
|
error = err.detail;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2024-05-06 23:59:54 +00:00
|
|
|
return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? 'New Chat';
|
2024-03-26 07:59:57 +00:00
|
|
|
};
|