2024-01-05 02:38:03 +00:00
|
|
|
import { OPENAI_API_BASE_URL } from '$lib/constants';
|
|
|
|
|
|
|
|
export const getOpenAIUrl = async (token: string = '') => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/url`, {
|
|
|
|
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.OPENAI_API_BASE_URL;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateOpenAIUrl = async (token: string = '', url: string) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/url/update`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
url: url
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.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.OPENAI_API_BASE_URL;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getOpenAIKey = async (token: string = '') => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/key`, {
|
|
|
|
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.OPENAI_API_KEY;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateOpenAIKey = async (token: string = '', key: string) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/key/update`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
...(token && { authorization: `Bearer ${token}` })
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
key: key
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.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.OPENAI_API_KEY;
|
|
|
|
};
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
console.log(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
|
|
|
|
.map((model) => ({ name: model.id, external: true }))
|
|
|
|
.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
|
|
|
|
.map((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
|
|
|
|
|
|
|
export const generateOpenAIChatCompletion = async (token: string = '', body: object) => {
|
|
|
|
let error = null;
|
|
|
|
|
|
|
|
const res = await fetch(`${OPENAI_API_BASE_URL}/chat/completions`, {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|