Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

refactor: use a consistent API with grouping when performing validation #101

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/service-provider/gpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const fetchGpt = async (
tabInfo: TabInfo,
types: string[]
) => {
// See https://platform.openai.com/docs/api-reference/chat/create
const apiURL =
(await getStorage("apiURL")) ||
"https://api.openai.com/v1/chat/completions";
Expand Down
52 changes: 31 additions & 21 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export async function handleOneTab(
return type;
}

// TODO merge this to service-provider
/**
* This function will show a toast!
*/
Expand All @@ -87,34 +88,43 @@ export const validateApiKey = async (
}),
}
);
if (response.status === 200) {
if (response.ok) {
return true;
} else {
const txt = await response.text();
toast.error("Invalid Gemini Key: " + response.status + " " + txt);
return false;
}
} else {
const response = await fetch(
"https://api.openai.com/v1/engines/davinci/completions",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
prompt: "This is a test",
max_tokens: 5,
temperature: 0.5,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["\n"],
}),
}
);
if (response.status === 200) {
const apiURL =
(await getStorage("apiURL")) ||
"https://api.openai.com/v1/chat/completions";
const model = (await getStorage("model")) || "gpt-3.5-turbo";

// https://platform.openai.com/docs/api-reference/chat/create
const response = await fetch(apiURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model,
messages: [
{
role: "system",
content: "ping",
},
],
max_tokens: 1,
temperature: 0.5,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["\n"],
}),
});
if (response.ok) {
toast.success("Valid OpenAI Key");
return true;
} else {
Expand Down